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?

4 Years
Discussion Span
Last Post by Tony_8

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.