Hi, as you can understand from the name, I am new in VB. So I have 2 questions regarding buttons:

1:
I have an array of buttons created like this in the code:

Dim buttons(10) As System.Windows.Forms.Button

I need whenever each one of these buttons is clicked to invoke the same method. Then I will go into the method, figure out which button was clicked and act accordingly. The reason I am doing this is because the length of the array will be created dynamically. So I don't know how many buttons there will be when the form loads, but I want when one of the buttons is clicked to call the same method

And 2:

I have written this code:

Dim button As System.Windows.Forms.Button = New System.Windows.Forms.Button()

Private Sub button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button.Click

    End Sub

And I get the error:

Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

Then I change it to this:

Friend WithEvents button As System.Windows.Forms.Button

Private Sub button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button.Click

    End Sub

And when I run it I get a NullReferenceException

Recommended Answers

All 5 Replies

NullReferenceException

I think you used an button without new it. and you have to because it's reference type.
[out of topic]
someone professional in Java, the next step should be C# not VB.NET:)

,,,someone professional in Java, the next step should be C# not VB.NET:)

I couldn't agree more.

To answer your question, however: AddHandler :)

commented: I like his behavior in VB.NET forum +6
commented: Exactly what I was looking for +5

My friend I don't mean it's weak, I just concentrate on syntax similarities:)
And thanks for your great answer :)

Try this:
Double click on your button. This will take you to the code page showing the click event.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Now all we do is add each button click to the back of this.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button3.Click, Button2.Click

And to find out which button was clicked check the sender object.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button3.Click, Button2.Click
        Dim btn As Button = sender
        MessageBox.Show(btn.Name.ToString & " pressed.")
    End Sub

Try this:
Double click on your button. This will take you to the code page showing the click event.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Now all we do is add each button click to the back of this.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button3.Click, Button2.Click

And to find out which button was clicked check the sender object.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button3.Click, Button2.Click
        Dim btn As Button = sender
        MessageBox.Show(btn.Name.ToString & " pressed.")
    End Sub

I already knew how to do this, and even though I am new to VB, I know that this is not a good advice because you hardcode the buttons at the method Button1_Click.

If you have read closely, I have an array of buttons which is dynamically generated. So I cannot go and add at the end of the method the buttons, because I want the "handling" to take place at runtime.

I used Comatose's suggestion and did this:

For i As Integer = 0 To length - 1
  AddHandler Me.buttonEdit(i).Click, AddressOf viewEdit_Click
Next

Public Sub viewEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim buttonPressed As System.Windows.Forms.Button = sender
        MsgBox(buttonPressed.Name)
End Sub

By the way. Thanks Comatose. It was exactly what I was looking for.
To the rest of you thanks for your help.

And RamyMahrous the reason I am trying to write VB, is because in my University I went and asked from a professor to give me a Final Year Project and he asked me to do it in VB. It's been a month now that I am trying to write something decent and I hate myself everyday for turning down another project in Java because I thought it was too easy for the Final Project and I wanted something more prestige for my Diploma.
Now I am stack with this (no offense for all VB programmers) and I am struggling to finish it

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.