how to know which index of clicked in array of button

Hi experts,

I have created one runtime array of buttons

And i wish to know which button i have clicked i.e what is the index of button .

how to get particular index ?

Thanks in advance.
Who is Participating?
dstanley9Connect With a Mentor Commented:
Cast the sender parameter to a Button and look at it's Id (or Name if WinForms)

Button button = sendar as Button
if (button != null)
  string buttonName = button.Id;

If you want the actual numeric index from the array I would name the buttons with the index number (e.g. MyButton1, MyButton2) and parse it in the event handler.  Or, if it's a Windows app, you could use the Tag property.
avinash_takaleAuthor Commented:
and one more thing if i want to set some click event on runtime checkbox
Then how do i set this ?

means i will read some value before start of application and i want to some check box to true in a array of checkbox which i have created at runtime
If you have a method already defined:

public void MyClick(object sender, EventArge args)
  // Do something

then use

CheckBox checkBox = new CheckBox();
/// set properties
checkBox.Click += new EventHandler(MyCLick);
avinash_takaleAuthor Commented:

suppose there are 5 checkbox are created at runtime
and i want to set 2nd and 4th to true at the form startup
How do i set this true this cheakbox.

Thanks in advance
checkBox.Checked = true;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.