如何将数组用于多个按钮

时间:2016-09-16 21:55:06

标签: vb.net

我有25个按钮,当我点击按钮时,它会自动禁用并给你点数。但是,我不确定如何使用数组来防止为所有25个按钮重复相同的代码块。我尝试了这个,但输出中出现错误。

第4行:预期表达 第6行:预期结束语 第9行:表达式不会产生值。

Public Class SBCL

Dim number = New Integer() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}
Dim score = As Integer

Private Sub Button(number)_Click(sender As Object, e As EventArgs) Handles Button14.Click
score = score + 1
    MsgBox(score & " " & "points out of 25")
    Button(number).Enabled = False
    ProgressBar1.Value = ProgressBar1.Value + 4
End Sub

Private Sub AboutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem.Click
    MsgBox("Created by Nathen35 of Moonboy Development.")
End Sub

Private Sub ChangeLogToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ChangeLogToolStripMenuItem.Click
    MsgBox("Version 0.4a: Exit button added. Progress bar added.")
End Sub

Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click
    Close()
End Sub

End Class

2 个答案:

答案 0 :(得分:0)

您可以连接按钮以调用相同的功能。然后在该功能中,您可以检查单击了哪个按钮并执行您想要执行的操作。

 Private Sub OnButtonClick(sender As Object, e As EventArgs) Handles btn3.Click, btn2.Click, btn1.Click
    Dim ClickButton As Button = CType(sender, Button)
    Select Case ClickButton.Name
        Case "btn1"
            'Your Code For btn1 (For your case amount of points to give)
        Case "btn2"
              'Your Code For btn2 (For your case amount of points to give)
        Case "btn3"
            'Your Code For btn3 (For your case amount of points to give)
    End Select
    ClickButton.Enabled = False 'Disable the button after clicking it
End Sub

如果要在代码中生成按钮(而不是在设计器中),则可以使用以下代码:

 Private Sub GenerateButtons()
    Dim btn1 As New Button() 'Generates a button
    btn1.Name = "btn1" 'Name of the button
    btn1.Text = "Click me" 'Text on the button
    btn1.Location = New Point(0, 0) 'location of the button
    btn1.Size = New Size(100, 100) 'Size of the button
    Me.Controls.Add(btn1) 'Add the button to the current form
    AddHandler btn1.Click, AddressOf OnButtonClick 'Bing a click event

    Dim btn2 As New Button()
    btn2.Name = "btn1"
    btn2.Text = "Click me"
    btn2.Location = New Point(200, 200)
    btn2.Size = New Size(100, 100)
    Me.Controls.Add(btn2)
    AddHandler btn2.Click, AddressOf OnButtonClick
End Sub
Private Sub OnButtonClick(sender As Object, e As EventArgs)
    Dim ClickButton As Button = CType(sender, Button)
    Select Case ClickButton.Name
        Case "btn1"
            'Your Code For btn1 (For your case amount of points to give)
        Case "btn2"
            'Your Code For btn2 (For your case amount of points to give)
    End Select
    ClickButton.Enabled = False 'Disable the button after clicking it
End Sub

答案 1 :(得分:0)

更改此部分:

Public Class SBCL

    Dim score As Integer

    Private Sub Button_Click(sender As Object, e As EventArgs)
        Dim button = TryCast(sender, Button)
        If button Is Nothing Then Exit Sub

        score = score + 1
        MsgBox(score & " points out of 25")
        button.Enabled = False
        ProgressBar1.Value = ProgressBar1.Value + 4
    End Sub

然后在表单设计器中选择所有25个按钮>右键单击它们> Properties> Events>并在Click事件下拉列表中选择Button_Click

enter image description here