Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I've created 100 buttons dynamically. But how to add a common event handler to this..

also need to know which button is clicked..

Thanks
Posted

1 solution

VB
Dim buttons As New List(Of Button)()
for(int i = 0; i<100; i++)
{
    Dim myButton As New Button()
    AddHandler myButton.Click , AddressOf MyClickHandler
    buttons.Add(myButton)
}


VB
Private Sub MyClickHandler(sender As Object, e As System.EventArgs)
End Sub

Thats the best I can do without more insight, like whether you're using ASP.NET, Winforms, WPF..


Updated the answer there was some C# intermingled with the VB.NET
 
Share this answer
 
v3
Comments
DEB4u 10-Sep-11 20:11pm    
i'm asking for a procedure by which I can handle all the 100 button click event in 1 function without using switch case.
Simon Bang Terkildsen 10-Sep-11 20:13pm    
From what you just said I still see my answer as a solution to your question, you'll need to elaborate more.
Simon Bang Terkildsen 10-Sep-11 20:15pm    
As for which button is clicked the sender parameter is the instance of the button that were clicked. E.g.
Dim myButton As Button = TryCast(sender, Button)
DEB4u 10-Sep-11 20:17pm    
ya, thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900