I need to create 5 buttons dynamically on windows form and each button should respond to click event. I tried it but all buttons are responding to same event.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
I assume you're in a loop and do something like this?
You're registering the same method for all buttons. You'll need individual methods for each button. Alternatively, you can assign each button a different identifying property and in your handler, check to see which button was the sender.
From there you can take appropriate action.
This is what Nick is talking about are your two options (You should be able to run this code and see both options):
Guessing what you might have tried: Yes, all buttons fire their events to the same method, but the
sender
-parameter of your callback method contains a reference to the button that actually caused the specific event.