For example, if on form load Button1 was created, how would I make this button function?
-
When you double click it in the gui you create an event handler. There you can write the code to do what you needOskar Kjellin– Oskar Kjellin2012-02-29 15:46:04 +00:00Commented Feb 29, 2012 at 15:46
-
winforms? webforms? wpf? be specific.Joel Coehoorn– Joel Coehoorn2012-02-29 20:24:39 +00:00Commented Feb 29, 2012 at 20:24
Add a comment
|
2 Answers
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
Comments
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
Mitchell
You cannot click the button in the designer. It is created on form load.