0

I've created a button, but can't get it to run the msgbox("click") when I click.. What am I doing wrong? Thanks.

Private Sub main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With mybut
mybut.AutoSize = True
mybut.Name = "delete-btn" & btn_number
mybut.Location = New System.Drawing.Point(500, 20)
mybut.Text = "Delete"
End With
End Sub

Private Sub mybut_Click(sender As Object, e As EventArgs)
MsgBox("Click")
End Sub
1
  • You don't need mybut in the With. You can just type .PropName (e.g., .AutoSize = True) Commented Nov 18, 2013 at 11:54

1 Answer 1

1

You need a Handles for your button click

Private Sub mybut_Click(sender As Object, e As EventArgs) Handles mybut.Click
MsgBox("Click")
End Sub

If it is a dynamic button you need to add an event handler

AddHandler mybut.Click, AddressOf Me.mybut_Click
Sign up to request clarification or add additional context in comments.

5 Comments

Tried that but then I get an error on my handles saying that "Handles Clause requires a WithEvents variable defined in the containing type or one of its base types".. Don't even know what that means, I'm very new at this.
@user2989415 do you create the button dynamically or using the designer?
In that case you need to add event handlers for the button when you create it. I will update my answer to help
Ok, now I've tried to put the addhandler at different places in the script, but doesn't really seem to make a difference?
Add it just after you create the button, as it's dynamic you won't need the Handles mybut.Click

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.