events for array of buttons.  
Author Message
pessi





PostPosted: Visual C# Language, events for array of buttons. Top

dear all

I have an array of buttons pbuttonsIdea (nearly 40 of them).
I want to have click events for these buttons

can someone tell me how to do that for a array of buttons

thanks in advance,
prasad..



Visual C#1  
 
 
ahmedilyas





PostPosted: Visual C# Language, events for array of buttons. Top

you could make 1 event handler and do it that way.

Example:

Button[] myButtons = new Button[10];

for(int counter = 0; counter < myButtons.Length; counter++)

{

   myButtons[counter] = new Button();

   myButtons[counter].Name = "btn" + counter.ToString();

   myButtons[counter] += new EventHandler(MyButton_Click);

}

 

private void MyButton_Click(object sender, EventArgs e)

{

   if (sender != null)

   {

      MessageBox.Show((Button)sender).Name + " pressed");

   }

}



 
 
pessi





PostPosted: Visual C# Language, events for array of buttons. Top

you are a gem. that worked just perfect I just forgot the casting of sender as button.

I am learning slowly for sure...:)