0
Public Class Form1
    Dim i As Integer = 0
    Dim txt As New TextBox()
    Dim btn As New Button()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btn.Name = "btnMove"
        btn.Size = New Size(60, 20)
        btn.Location = New Point(220, 20)
        btn.Text = "move"
        btn.TextAlign = ContentAlignment.MiddleCenter
        Me.Controls.Add(btn)
        Me.BringToFront()
    End Sub

    Private Sub btnMove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click

    End Sub
End Class

this is my code and i want to add an event to btn Button i hope that i make my problem clear and sorry for my bad English

6
  • 2
    Well you've already added the btnMove_Click handler to btn using Handles btn.Click - in what way does it not work? Commented Sep 1, 2013 at 16:26
  • @JonSkeet: His code has deeper problems; he's trying to misuse an array. Commented Sep 1, 2013 at 16:28
  • @JonSkeet Handles clause requires a With Events variable defined in the containing type or one of its base types Commented Sep 1, 2013 at 16:29
  • Maybe code wouldn't compile since btn whould be declared with the WithEvents statement Dim WithEvents btn As New Button() to be able to use the Handles btn.Click Commented Sep 1, 2013 at 16:29
  • You are welcome :). I will see it there is no evident duplicate of this issue. If not I could remove my comment with typos and write it as an answer. Commented Sep 1, 2013 at 16:39

2 Answers 2

1
   Private Sub btnMove_Click(...) Handles btn.Click

The Handles keyword requires you to declare the control that generates the event with the WithEvents keyword. Fix:

   Dim WithEvents btn As New Button()

The alternative is to subscribe the event explicitly with the AddHandler keyword. In which case you omit the Handles keyword and write it like this instead:

Private Sub Form1_Load(...) Handles MyBase.Load
   '' etc..
   AddHandler btn.Click, AddressOf btnMove_click
End Sub

Using the designer to add these controls is certainly the best way, it avoids little mistakes like this.

Sign up to request clarification or add additional context in comments.

Comments

1
Public Class Form2
    Dim btn As New Button
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btn.Name = "btnMove"
        btn.Size = New Size(60, 20)
        btn.Location = New Point(220, 30)
        btn.Text = "Move"
        btn.TextAlign = ContentAlignment.MiddleCenter
        Me.Controls.Add(btn)
        Me.BringToFront()
        AddHandler btn.Click, AddressOf btnMove_click
    End Sub
    Private Sub btnMove_click(ByVal sender As Object, ByVal e As System.EventArgs)
        MsgBox("welcome to salfkjsadlkf")
    End Sub
End Class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.