Aynıları Patlat
public void ButtonEkle(string name, int x, int y, int width, int height,string text)
{
Button newButton = new Button();
this.Controls.Add(newButton);
newButton.Location = new Point(x, y);
newButton.Size = new Size(width, height);
newButton.Name = name;
newButton.Tag = text;
newButton.Click += Aynimi;
}
public string bitincibutton = "";
public string bitincitext = "";
public string ikincibutton = "";
public string ikincitext = "";
private void Aynimi(object sender, EventArgs e)
{
Button button = (Button)sender;
if(bitincibutton == "")
{
bitincibutton = button.Name;
bitincitext = button.Tag.ToString();
button.Text = button.Tag.ToString();
button.Enabled = false;
}
else if (ikincibutton == "")
{
ikincibutton = button.Name;
ikincitext = button.Tag.ToString();
button.Text = button.Tag.ToString();
button.Enabled = false;
}
else
{
Control[] matches = this.Controls.Find(bitincibutton, true);
Button b = matches.Length > 0 ? matches[0] as Button : null;
b.Text = "";
b.Enabled = true;
bitincibutton = "";
Control[] matches1 = this.Controls.Find(ikincibutton, true);
Button b2 = matches1.Length > 0 ? matches1[0] as Button : null;
b2.Text = "";
b2.Enabled = true;
ikincibutton = "";
bitincibutton = button.Name;
bitincitext = button.Tag.ToString();
button.Text = button.Tag.ToString();
button.Enabled = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
int x = 100;
int y = 100;
int width = 100;
int height = 100;
ArrayList sayilar = new ArrayList();
for(int i = 0;i<=7;i++)
{
sayilar.Add(i + 1);
sayilar.Add(i + 1);
}
Random rastgele = new Random();
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 4; j++)
{
int sayi = rastgele.Next(sayilar.Count);
ButtonEkle("Buton" + i, x * i, y * j, width, height , sayilar[sayi].ToString());
sayilar.RemoveAt(sayi);
}
}
}
Yorumlar
Yorum Gönder