Təşəkkürlər, uzun uzadı kod hissəsini bir neçə sətirlə əvəz elədim :)
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.
[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ə?
Ə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]