I am dynamically creating a Form Popup, and populating it with a variable amount of buttons. (it reads a directory and creates a button for each folder in that directory). I have assigned the button.Name of each button to be the name of the directory. My code to do all this is working perfect. Additionally I assigned each of the buttons a Click Event handler that calls the same handler function. What I want is to figure out which button is calling this function. However, I don't seem to be able to get that information from the arguments: object sender, and EventArgs e?

Is there a way to do this, or is there some way to dynamically create an individual handler for each button I create?

Recommended Answers

All 2 Replies

Yes, try this

private void MyBtn_Click(object sender, EventArgs e)
{
   // make a Button point to the sender object, you know it must be a button!
   Button MyBtn = sender as Button;

   // now use MyBtn.Name, it will be the directory for the button you clicked on.

Thank you very much! I'm still fairly new to how all the objects work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.