0

For example, if on form load Button1 was created, how would I make this button function?

2
  • When you double click it in the gui you create an event handler. There you can write the code to do what you need Commented Feb 29, 2012 at 15:46
  • winforms? webforms? wpf? be specific. Commented Feb 29, 2012 at 20:24

2 Answers 2

2
Public WithEvents newButton As Windows.Forms.Button

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim i As Integer

    For i = 1 To 5
        newButton = New Windows.Forms.Button
        newButton.Name = "btnButton" & i
        newButton.Text = "Button " & i
        newButton.Top = 20 + i * 30
        newButton.Left = 40

        AddHandler newButton.Click, AddressOf ButtonClicked
        Me.Controls.Add(newButton)
    Next
End Sub

Private Sub ButtonClicked(ByVal sender As Object, ByVal e As EventArgs)
    MsgBox("You clicked: " & sender.name & vbCrLf & "Button name: " & sender.Text)
End Sub

Reference

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

Comments

0

If you double click the button in the designer view you will be taken to the code-behind for the Button_Click event. There you can add any functionality you want to have happen when the button is clicked by the user.

1 Comment

You cannot click the button in the designer. It is created on form load.

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.