如何编写代码中的按钮

时间:2012-02-29 15:45:15

标签: vb.net

例如,如果在表单上加载了Button1,我将如何使该按钮起作用?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

如果双击设计器视图中的按钮,您将进入Button_Click事件的代码隐藏。在那里,您可以添加您希望在用户点击按钮时发生的任何功能。