i have dynamic buttons how can i add mouse click event
eg. i create buttons with groupbox and when users clik right or left do someting
thenks...
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jan 10, 2014, 10:12 AM
Orhan SALURPosted Jan 9, 2014, 8:38 PM
but i cant add to my project how can i add this method
bool[,] Mayinlar; // mayinlarimizi tutacagimiz bool tipinde matrisimiz
const byte MayinSayisi = 10; // maksimum mayin sayisi
private void button1_Click(object sender, EventArgs e)
{
MayinlariBelirle();
MayinlariYerlestir();
timer1.Start();
textBox1.Text = "0";
}
void MayinlariBelirle()
{
Mayinlar = new bool[9, 9]; // matrisi sifirla yeniden olustur
Random rnd = new Random(); // rastgele koordinatlar için random
byte mayin = 0; // olusturulan mayin sayisi
int x = 0, y = 0; // matrisde rastgele yerlesim için kullanacagimiz koordinatlar
while (mayin < MayinSayisi) // istenen MayinSayisina kadar dön
{
x = rnd.Next(9);
y = rnd.Next(9);
if (!Mayinlar[x, y]) // Belirlenen koordinatda zaten mayin yoksa
{
Mayinlar[x, y] = true; // Belirlenen koordinata mayini yerlestir
++mayin; // mayin sayisini artir
}
}
}
void MayinlariYerlestir()
{
grpMayinlar.Controls.Clear();
Button btn = null; // mayinimiz
Size size = new Size(20, 20); // butonun boyutu
Point loc = new Point(10, 10); // butonun konumu
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
btn = new Button(); // butonu olustur
btn.Name = string.Format("{0}-{1}", i, j); // butonun ismine koordinatlarini yaz
btn.Size = size; // butonun boyutu belirt
btn.Location = loc; // koordinatini belirt
loc.X += size.Width + 2; // sonraki butonun yana kaymasi için x degerini artir
btn.Click += new EventHandler(btn_Click); // tüm butonlari tek bir event da düzenlemek için click evetini belirle
grpMayinlar.Controls.Add(btn); // butonu grpMayinlar groupbox ina yerlestir
}
loc.Y += size.Height + 2; // yatay konumda basa al
loc.X = 10; // bir alta kaydir
btn.MouseClick += dynButton_MouseClick; // add handler
grpMayinlar.Controls.Add(btn);
}
}
public void dynButton_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// do something
}
else if (e.Button == MouseButtons.Right)
{
btn.text = "?";
}
}
private void btn_Click(object sender, EventArgs e)
{
Button btn = sender as Button; // tiklanan buton hangisiyse onu degiskene ata
string[] Koordinat = btn.Name.Split('-'); // 1-4 gibi formatta olan ismi 1 ve 4 den olusan string dizisine dönüstür
byte x = byte.Parse(Koordinat[0]), y = byte.Parse(Koordinat[1]); // dizideki x ve y koordinatlarini degiskene aktar
if (Mayinlar[x, y]) // tiklanan butonun belirttigi koordinatda mayin varsa
{
btn.Text = "X";
timer1.Stop();
MessageBox.Show("Patladiniz! BOMM!!!");
return;
}
int mayinsayisi = 0;
if (Mayinlar[x, y + 1]) // saginda mayin varsa
++mayinsayisi;
if (Mayinlar[x, y - 1]) // solunda mayin varsa
++mayinsayisi;
if (Mayinlar[x + 1, y]) // altinda mayin varsa
if (Mayinlar[x - 1, y]) // üstünde varsa
++mayinsayisi;
//---------------------------------------------------
if (Mayinlar[x + 1, y + 1]) // alt-sag
++mayinsayisi;
if (Mayinlar[x + 1, y - 1]) // alt-sol
++mayinsayisi;
if (Mayinlar[x - 1, y + 1]) // üst-sag
++mayinsayisi;
if (Mayinlar[x - 1, y - 1]) // üst-sol
++mayinsayisi;
btn.Text = mayinsayisi.ToString(); // etrafindaki mayin sayisini butona yaz
if (mayinsayisi==0)
btn.BackColor = Color.Green;
if (mayinsayisi == 1)
btn.BackColor = Color.Yellow;
if (mayinsayisi == 2)
btn.BackColor = Color.Orange;
if (mayinsayisi == 3)
btn.BackColor = Color.Red;
}
//public void OyunuBitir()
//{
// if (Mayinlar[x, y])
// {
// }
//}
private void timer1_Tick_1(object sender, EventArgs e)
{
textBox1.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) + 1);
}
}
}
VulpesPosted Jan 9, 2014, 3:39 PM
Button dynButton = new Button();
// set button properties
dynButton.MouseClick += dynButton_MouseClick; // add handler
groupBox1.Controls.Add(dynButton);
You now need to add the eventhandler manually to your form: