Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic button click event handler

Tags:

vb.net

I've 100 buttons created dynamically in a form. How can I an add event handler to them?

like image 466
Debasish Sahu Avatar asked Sep 10 '11 22:09

Debasish Sahu


People also ask

How do I add a button with click event dynamically runtime in Windows app?

Simply add the eventhandler to the button when creating it. button. Click += new EventHandler(this. button_Click); void button_Click(object sender, System.

How do you call a button click event?

How can I call SubGraphButton_Click(object sender, RoutedEventArgs args) from another method? private void SubGraphButton_Click(object sender, RoutedEventArgs args) { } private void ChildNode_Click(object sender, RoutedEventArgs args) { // call SubGraphButton-Click(). }

How do you call a button click event in code behind?

To simulate the button click in code, you simply call the event handler: Button1_Click(object sender, EventArgs e). protected void Page_Load(object sender, EventArgs e) { //This simulates the button click from within your code. Button1_Click(Button1, EventArgs.


4 Answers

You can use AddHandler to add a handler for any event.

For example, this might be:

AddHandler theButton.Click, AddressOf Me.theButton_Click
like image 132
Reed Copsey Avatar answered Oct 08 '22 08:10

Reed Copsey


Just to round out Reed's answer, you can either get the Button objects from the Form or other container and add the handler, or you could create the Button objects programmatically.
If you get the Button objects from the Form or other container, then you can iterate over the Controls collection of the Form or other container control, such as Panel or FlowLayoutPanel and so on. You can then just add the click handler with
AddHandler ctrl.Click, AddressOf Me.Button_Click (variables as in the code below),
but I prefer to check the type of the Control and cast to a Button so as I'm not adding click handlers for any other controls in the container (such as Labels). Remember that you can add handlers for any event of the Button at this point using AddHandler.
Alternatively, you can create the Button objects programmatically, as in the second block of code below.
Then, of course, you have to write the handler method, as in the third code block below.

Here is an example using Form as the container, but you're probably better off using a Panel or some other container control.

Dim btn as Button = Nothing
For Each ctrl As Control in myForm.Controls
    If TypeOf ctrl Is Button Then
        btn = DirectCast(ctrl, Button)
        AddHandler btn.Click, AddressOf Me.Button_Click   ' From answer by Reed.
    End If
 Next

Alternatively creating the Buttons programmatically, this time adding to a Panel container.

Dim Panel1 As new Panel()
For i As Integer = 1 to 100
    btn = New Button()
    ' Set Button properties or call a method to do so.
    Panel1.Controls.Add(btn)  ' Add Button to the container.
    AddHandler btn.Click, AddressOf Me.Button_Click   ' Again from the answer by Reed.
Next

Then your handler will look something like this

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' Handle your Button clicks here
End Sub
like image 36
Jason S Avatar answered Oct 08 '22 08:10

Jason S


@Debasish Sahu, your answer is an answer to another question, namely: how to know which button (or any other control) was clicked when there is a common handler for a couple of controls? So I'm giving an answer to this question how I usually do it, almost the same as yours, but note that also works without type conversion when it handles the same type of Controls:

Private Sub btn_done_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim selectedBtn As Button = sender
    MsgBox("you have clicked button " & selectedBtn.Name)
End Sub
like image 4
monami Avatar answered Oct 08 '22 07:10

monami


I needed a common event handler in which I can show from which button it is called without using switch case... and done like this..

  Private Sub btn_done_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)

    MsgBox.Show("you have clicked button " & CType(CType(sender,  _
    System.Windows.Forms.Button).Tag, String))

  End Sub
like image 3
Debasish Sahu Avatar answered Oct 08 '22 07:10

Debasish Sahu