简体   繁体   English

如何引用多个按钮? (VB.NET)

[英]How to refer to multiple buttons? (VB.NET)

So, im building this project this version of Tic Tac Toe where each of the spaces of the main game have another Tic Tac Toe game inside.所以,我正在构建这个版本的井字游戏,主游戏的每个空间里面都有另一个井字游戏。 Search for "Ultimate Tic Tac Toe" if you want to know more.如果您想了解更多信息,请搜索“终极井字游戏”。 Within each space of the Grid, I have one button.在网格的每个空间内,我都有一个按钮。 9 per game, per 9 games equals 81 buttons.每局 9 个,每 9 个游戏等于 81 个按钮。

My question is: is there a way in VB.NET where I can link multiple buttons together?我的问题是:在 VB.NET 中有没有一种方法可以将多个按钮链接在一起? I want to create a variable that increases by one each time any of these buttons is pressed.我想创建一个变量,每次按下这些按钮时都会增加一个。 Do I have to do this in each individual sub, or is there a way to do this to multiple buttons?我是否必须在每个单独的子中执行此操作,还是有办法对多个按钮执行此操作? (I want to have other buttons: one to restart, one to exit, but these should not be included in this sum, only the 81 buttons I mentioned.) (我想要其他按钮:一个重启,一个退出,但这些不应该包括在这个总数中,只有我提到的81个按钮。)

There is more than one way to accomplish this.实现这一目标的方法不止一种。 Both the methods I will show you involve writing a custom event handler.我将向您展示的两种方法都涉及编写自定义事件处理程序。 The method must match the signature of the normal handler.该方法必须与普通处理程序的签名相匹配。 The example here is for a Button.Click event.此处的示例是针对 Button.Click 事件的。

Private Sub MyButtonHandler(sender As Object, e As EventArgs)
    Dim b = DirectCast(sender, Button)
    Dim t = b.Tag.ToString
    If t = "SomeValue" Then
        'Do something
    End If
End Sub

In the Form Designer select the button you need and open the Properties window.在表单设计器 select 中,您需要的按钮并打开属性 window。 在此处输入图像描述

Select the lighting bolt (left of the wrench). Select 照明螺栓(扳手左侧)。

Look for the Click event.查找 Click 事件。 Click the drop down arrow.单击下拉箭头。 在此处输入图像描述

Low and behold, there is you method listed as a choice to handle this event.低头,你有一个方法被列为处理此事件的选择。 This is because the method has the appropriate parameters, namely sender As Object and e As EventArgs.这是因为该方法具有相应的参数,即 sender As Object 和 e As EventArgs。 Just select your method for each button you want to use that method.只需 select 您对要使用该方法的每个按钮的方法即可。

When you check back to the code, you will see that a Handles clause has been automatically added to your method.当您查看代码时,您会看到 Handles 子句已自动添加到您的方法中。 You can have multiple Handles clauses.您可以有多个 Handles 子句。

Private Sub MyButtonHandler(sender As Object, e As EventArgs) Handles Button1.Click

Another way to do this is with AddHandler.另一种方法是使用 AddHandler。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    AddHandler Button1.Click, AddressOf MyButtonHandler
End Sub

If you already added all the Buttons to your Interface, AND you used a consistent NAMING CONVENTION, then you can use a couple of loops to find each button and wire them up using AddHandler as Mary has already suggested.如果您已经将所有按钮添加到您的界面,并且您使用了一致的命名约定,那么您可以使用几个循环来查找每个按钮并使用AddHandler将它们连接起来,正如 Mary 已经建议的那样。

In this example, the buttons are name "btnC0R0" through "btnC2R2", where the number after the "C" represents the column and the number after the "R" represents the row.在此示例中,按钮名称为“btnC0R0”到“btnC2R2”,其中“C”后面的数字代表列,“R”后面的数字代表行。

We can search for these buttons in the Load() event of the Form.我们可以在 Form 的Load()事件中搜索这些按钮。 I've also placed a Point structure in the .Tag property of each button so you can use that information when the button is clicked:我还在每个按钮的.Tag属性中放置了一个Point结构,因此您可以在单击按钮时使用该信息:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim btn As Button
        Dim ctlName As String
        For r As Integer = 0 To 2
            For c As Integer = 0 To 2
                ctlName = "btnC" & c & "R" & r
                btn = Me.Controls.Find(ctlName, True).FirstOrDefault
                If Not IsNothing(btn) Then
                    btn.Tag = New Point(c, r)
                    AddHandler btn.Click, AddressOf btn_Click
                End If
            Next
        Next
    End Sub

    Private Sub btn_Click(sender As Object, e As EventArgs)
        Dim btn As Button = DirectCast(sender, Button)
        Dim pt As Point = DirectCast(btn.Tag, Point)
        Me.Text = pt.X & ", " & pt.Y
    End Sub

End Class

Output: Output:

在此处输入图像描述

  Private Sub btnInp1_Click(sender As Object, e As EventArgs) Handles btnInp1.Click, btnInp2.Click, btnInp3.Click, btnInp4.Click, btnInp5.Click, btnInp6.Click, btnInp7.Click, btnInp8.Click, btnInp9.Click


    If player = 1 Then
        player = 2
    Else
        player = 1
    End If

    Me.Text = "Player" & player

    If player = 1 Then
        sender.Text = "X"
    Else
        sender.Text = "O"
    End If
    sender.Enabled = False
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM