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)
2
Ə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.
1
using System;
2
using System.Windows.Forms;
3
4
namespace cavablarNet
5
{
6
public partial class Form1 : Form
7
{
8
public Form1()
9
{
10
InitializeComponent();
11
this.button1 = new System.Windows.Forms.Button();
12
this.button2 = new System.Windows.Forms.Button();
13
//
14
// button1
15
//
16
this.button1.Location = new System.Drawing.Point(29, 121);
17
this.button1.Name = "button1";
18
this.button1.Size = new System.Drawing.Size(75, 23);
19
this.button1.TabIndex = 0;
20
this.button1.Text = "button1";
21
this.button1.UseVisualStyleBackColor = true;
22
this.button1.Click += new System.EventHandler(this.button1_Click);
23
//
24
// button2
25
//
26
this.button2.Location = new System.Drawing.Point(156, 121);
27
this.button2.Name = "button2";
28
this.button2.Size = new System.Drawing.Size(75, 23);
29
this.button2.TabIndex = 1;
30
this.button2.Text = "button2";
31
this.button2.UseVisualStyleBackColor = true;
32
33
// button1`dən başqa klik olunarsa
34
/***/
35
this.button2.Click += new System.EventHandler(this.button1_Click);
36
/****/
37
this.Controls.Add(this.button2);
38
this.Controls.Add(this.button1);
39
}
40
41
private void button1_Click(object sender, EventArgs e)
42
{
43
Button btn = sender as Button; //get the button that was clicked
44
MessageBox.Show("Bu button klikləndi" + " " + btn.Text);
45
}
46
47
48
49
public Button button1 { get; set; }
50
51
public Button button2 { get; set; }
52
}
53
}
0
1
using System;
2
using System.Windows.Forms;
3
4
namespace winOwn
5
{
6
public partial class Form1 : Form
7
{
8
public Form1()
9
{
10
InitializeComponent();
11
}
12
13
private void Form1_Load(object sender, EventArgs e)
14
{
15
b1.Text = "A.A";
16
b2.Text = "A.A";
17
}
18
int s = 0 ;
19
int k = 1;
20
private void b1_Click(object sender, EventArgs e)
21
{
22
if(k==1)
23
{
24
b1.Text="E";
25
}
26
else if (k == 2)
27
{
28
b1.Text = "S";
29
}
30
else
31
{ b1.Text = "X"; }
32
}
33
private void b2_Click(object sender, EventArgs e)
34
{
35
if (k == 1)
36
{
37
b2.Text = "E";
38
}
39
else if (k == 2)
40
{
41
b2.Text = "S";
42
}
43
else
44
{ b2.Text = "X"; }
45
}
46
}
burada iki button deyil 30 yaxın button olarsa necə?
2
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
1
public Form1()
2
{
3
InitializeComponent();
4
this.button2.Click += new System.EventHandler(this.button1_Click);
5
this.button3.Click += new System.EventHandler(this.button1_Click);
6
this.button1.Click += new System.EventHandler(this.button1_Click);
7
8
}
9
10
private void button1_Click(object sender, EventArgs e)
11
{
12
Button btn = sender as Button;
13
textBox1.Text = btn.Text; // mən textbox`a mənimsətdim nəticə görünsün diyə
14
15
}
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.
0
Təşəkkürlər, uzun uzadı kod hissəsini bir neçə sətirlə əvəz elədim
Sual verin
Cavab verin