Solved

how to know which index of clicked in array of button

Posted on 2006-07-06
5
234 Views
Last Modified: 2011-09-20
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.
-Avinash
0
Comment
Question by:avinash_takale
  • 3
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
dstanley9 earned 400 total points
ID: 17050660
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.
0
 

Author Comment

by:avinash_takale
ID: 17051090
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
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 17051123
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);
0
 

Author Comment

by:avinash_takale
ID: 17051235


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
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 17051479
checkBox.Checked = true;
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question