【问题标题】:Programming a module to handle multiple button clicks vb对模块进行编程以处理多个按钮单击 vb
【发布时间】:2015-03-30 17:00:28
【问题描述】:

我正在使用 Windows 窗体编写井字游戏。我有一个用于比赛场地的“桌子”和用于图标的按钮。我使用了一个模块,我想处理每个按钮的点击,而不是为每个按钮编写代码。

代码:

Private Sub Btn_Box6_Click(sender As Object, e As EventArgs) Handles Btn_Box6.Click
        ChangeTxt()
    End Sub

按钮点击条件之一的示例

Sub ChangeTxt()
        If Form1.Active_PlayerTxt.Text = "X" Then
            PlayerOne = True
        ElseIf Form1.Active_PlayerTxt.Text = "O" Then
            PlayerTwo = True
        Else
            MsgBox("Please enter X or O to decide the player!")
        End If
        If PlayerOne = True Then
            Form1.Btn_Box1.Text = "X"
        ElseIf PlayerTwo = True Then
            Form1.Btn_Box1.Text = "O"
        Else
            MsgBox("Please enter X or O to decide the player!")
        End If
        Form1.Active_PlayerTxt.Text = ""
        PlayerOne = False
        PlayerTwo = False
    End Sub

所以基本上如您所见,这目前仅更改 btn_box1,但我希望它在单击每个单独的按钮 (btn_box2) 时更改它们,而不是仅更改 btn_box1。有没有简单的方法可以做到这一点?

【问题讨论】:

  • 尝试让 ChangeText 将 Button 作为其参数,而不是没有参数,并且在该方法中,您可以检查需要更新的 Button。每个事件处理程序的 Sender 对象将是那个 Button,因此您可以将它传递给 ChangeText。更简洁的是只有一个事件处理程序来处理所有按钮单击事件,它调用 ChangeText。

标签: vb.net


【解决方案1】:

这是玩井字游戏的一种非常简单的方法:

  1. 检查谁是当前玩家:X 或 O。
  2. 当当前玩家点击他想要点击的按钮时,更改该按钮的文本(首先检查它是否未被点击)。
  3. 更新当前播放器等。

你可以这样做:

' Create an enum to represent the current player.
Public Enum CurrentPlayer
    X
    O
End Enum

' Create an "instance" of the enum.
Public cPlayer As CurrentPlayer

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ' Lets say the first player is X.
    cPlayer = CurrentPlayer.X

    ' Reset the text of the buttons.
    ' You can do this using the properties of the buttons in the designer.
    For Each btn As Button In Me.Controls
        btn.Text = ""
    Next

End Sub

' Occures when the first button is clicked.
Private Sub ButtonClicked(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
    ' Calls the funciton and passing the current button (byref).
    CheckText(sender)
End Sub

' Passing an argument byref does not create a copy
' of the argument, but sends the "real" one.
Public Sub CheckText(ByRef btn As Button)

    ' First check if the current button is "free".
    If btn.Text = "" Then

        ' Change the text of the button according to the current player.
        Select Case cPlayer
            Case CurrentPlayer.X
                btn.Text = "x"
                cPlayer = CurrentPlayer.O
            Case CurrentPlayer.O
                btn.Text = "O"

                ' Changes the current player.
                cPlayer = CurrentPlayer.X
        End Select
    Else
        MsgBox("This button is already used!")
    End If
End Sub

我会让你做寻找赢家的功能:)

希望对您有所帮助。

【讨论】:

  • Handles Button1.Click, Button2.Click,等等等等
猜你喜欢
  • 2012-12-10
  • 2011-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
  • 1970-01-01
相关资源
最近更新 更多