Vb.net 用vb编程一个模块来处理多个按钮点击

Vb.net 用vb编程一个模块来处理多个按钮点击,vb.net,Vb.net,我正在用Windows窗体编写一个井字游戏。我有一个用于运动场的“桌子”和用于图标的按钮。我使用了一个模块,我想处理每个按钮的点击,而不是为每个按钮编写代码 代码: 按钮单击条件之一的示例 基本上,正如您所看到的,这目前只会更改btn_框1,但我希望它在单击每个单独的按钮btn_框2时更改它们,而不仅仅是更改btn_框1。有没有一个简单的方法可以做到这一点?这里有一个非常简单的方法来做一个tic-tac-toe游戏: 检查谁是当前玩家:X或O。 当当前玩家单击他想要的按钮时,首先更改该按钮的文本

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

代码:

按钮单击条件之一的示例


基本上,正如您所看到的,这目前只会更改btn_框1,但我希望它在单击每个单独的按钮btn_框2时更改它们,而不仅仅是更改btn_框1。有没有一个简单的方法可以做到这一点?

这里有一个非常简单的方法来做一个tic-tac-toe游戏:

检查谁是当前玩家:X或O。 当当前玩家单击他想要的按钮时,首先更改该按钮的文本,检查该按钮是否尚未单击。 更新当前播放器等。。 您可以这样做:

' 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
我会让你做一个寻找赢家的功能:


希望有帮助。

尝试让ChangeText将按钮作为其参数,而不是没有参数,在该方法中,您可以检查需要更新的按钮。每个事件处理程序中的发送者对象都将是该按钮,因此您可以将其传递到ChangeText中。甚至更干净的方法是只有一个事件处理程序来处理所有的按钮点击事件,它调用ChangeText.handles Button1.click,Button2.click等。
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
' 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