C#-da 1 neçə button üçün yazılmış button

Bir neçə button eyni işi görür her defe click`lerine onu yazdıqda çox uzun olur buttonların sayı artıdqca. necə bir metod yaza bilerem ki, hər button klik olunduqda özünəməxsus yerinə yetirsin metodu. sadə misalla izah edim fikrimiki aydın olsun: məsələn adicə calculator yazsaq reqemlere gore deyishir button`lar, amma toplamada çıxmada eyni funksiyanı yerində yetirir. bir metod yazıb hər rəqəm üçün onu çağırmaq olar?

Verilmiş cavablar və yazılan şərhlər (4 cavab var)

Felix (2012-12-03 16:33:38)
Təşəkkürlər, uzun uzadı kod hissəsini bir neçə sətirlə əvəz elədim :)

Onar Alili (2012-12-03 10:22:35)
Siz hansı button`a clikc olunduğunu nəzərə alıb yuxarda yazdığıvız if-else bolkunu istifadə edə bilərsiz. Form`a 3 dənə button və 1 dənə textbox atdıq daha sonra aşağdakı kodları əlavə et [code] public Form1() { InitializeComponent(); this.button2.Click += new System.EventHandler(this.button1_Click); this.button3.Click += new System.EventHandler(this.button1_Click); this.button1.Click += new System.EventHandler(this.button1_Click); } private void button1_Click(object sender, EventArgs e) { Button btn = sender as Button; textBox1.Text = btn.Text; // mən textbox`a mənimsətdim nəticə görünsün diyə } [/code] Button1 click etdikdə textbox.text`inə hansı buttona click`lənibsə onun adı gələcək. Əgər btn.Text=button1(button2,button3 və s.) onda hər buttona ait kodları işlət.

Felix (2012-12-03 09:30:31)
[code]using System; using System.Windows.Forms; namespace winOwn { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { b1.Text = "A.A"; b2.Text = "A.A"; } int s = 0 ; int k = 1; private void b1_Click(object sender, EventArgs e) { if(k==1) { b1.Text="E"; } else if (k == 2) { b1.Text = "S"; } else { b1.Text = "X"; } } private void b2_Click(object sender, EventArgs e) { if (k == 1) { b2.Text = "E"; } else if (k == 2) { b2.Text = "S"; } else { b2.Text = "X"; } } }[/code] burada iki button deyil 30 yaxın button olarsa necə?

Onar Alili (2012-11-27 17:12:41)
Əgər sualıvı düzgün başa düşdümsə bu cür edə bilərsən. Bu cür aydın olmadısa kodları upload edə bilərəm. [code] using System; using System.Windows.Forms; namespace cavablarNet { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); // // button1 // this.button1.Location = new System.Drawing.Point(29, 121); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(156, 121); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 1; this.button2.Text = "button2"; this.button2.UseVisualStyleBackColor = true; // button1`dən başqa klik olunarsa /***/ this.button2.Click += new System.EventHandler(this.button1_Click); /****/ this.Controls.Add(this.button2); this.Controls.Add(this.button1); } private void button1_Click(object sender, EventArgs e) { Button btn = sender as Button; //get the button that was clicked MessageBox.Show("Bu button klikləndi" + " " + btn.Text); } public Button button1 { get; set; } public Button button2 { get; set; } } } [/code]

Mövzu üzrə bənzər suallara da baxa bilərsiniz.

Access specifier

İki ədədin əbob-unun c++ da yazılması.

Çoxölçülü massivdə bərabər sətirləri tapmaq.c/c++/c#

Gb C, D disk yaddaşı

C++ üçün proqram axtarıram

programlaşdırmanı kurslarda öyrənmək yoxsa özbaşına öyrənmək?