Visual Basic单击事件一起添加分数

时间:2021-09-02 00:01:21

Not 100% sure how to do this, mainly because I am new to programming here!

不是100%肯定如何做到这一点,主要是因为我是这里的新编程!

I have created a table which is populated with random numbers from an array, when the user clicks on one of the buttons the number needs to be added onto their score. eg, when a user clicks 10 and 15, their score will be 25. Once the user has clicked on the button, the button needs to change colour To AliceBlue (just a random colour). Any advice / examples?

我创建了一个表格,其中填充了数组中的随机数字,当用户点击其中一个按钮时,需要将数字添加到他们的分数上。例如,当用户点击10和15时,他们的分数将是25.一旦用户点击了按钮,该按钮需要将颜色改为AliceBlue(只是随机颜色)。有什么建议/例子吗?

This code below makes the table which is used within the game,

下面这段代码制作了游戏中使用的表格,

Let me know what you think!

让我知道你的想法!

Dim RandomNumbers = Enumerable.Range(0, 100).ToList()
        Dim RandomNumber As New Random()
        For Me.TableColunm = 0 To 4 Step 1
            For Me.TableRow = 0 To 4 Step 1
                Dim TemporaryNumber = RandomNumbers(RandomNumber.Next(0, RandomNumbers.Count))
                RandomNumbers.Remove(CInt(TemporaryNumber))
                TableButtons = New Button()
                With TableButtons
                    .Name = "TextBox" & TableColunm.ToString & TableRow.ToString
                    .Text = TemporaryNumber.ToString
                    .Width = CInt(Me.Width / 4)
                    .Left = CInt(TableColunm * (Me.Width / 4))
                    .Top = TableRow * .Height
                    .Tag = TemporaryNumber
                    AddHandler TableButtons.Click, AddressOf TableClickEvent
                End With
                GameScreen.Controls.Add(TableButtons)
            Next TableRow
        Next TableColunm
    Catch ex As Exception
        MsgBox(ex.StackTrace & vbCrLf & "index1= " & TableColunm & vbCrLf & "index2= " & TableRow)
End Try

and

Public Sub TableClickEvent(sender As Object, e As EventArgs)
    CType(sender, Button).BackColor = Color.BlueViolet
    OverAllScoreInteger += CInt(CType(sender, Button).Tag)
End Sub

I also have to parse the score into a text box called 'UserScoreBox' the form is on 'GameScreen'

我还必须将分数解析为一个名为“UserScoreBox”的文本框,表单位于“GameScreen”上

1 个解决方案

#1


1  

You add an eventhandler to a control by using the AddHandler statement and providing the sub that handles the event.

通过使用AddHandler语句并提供处理该事件的子,将eventhandler添加到控件。

Private Sub Clickhandler(sender As Object, e As EventArgs)
  CType(sender, Button).BackColor = Color.AliceBlue
End Sub

Sender is basically the source that initiated the event. In this case the button you clicked.

发件人基本上是发起活动的来源。在这种情况下,您单击的按钮。

To make the button raise the event, add this to the code where you create the Buttons:

要使按钮引发事件,请将其添加到创建按钮的代码中:

Addhandler TableButtons.Click, AddressOf Clickhandler

To add the scores you can for example place the index of the table row the button represents to the .Tag property of the button. That way you can, in the event handler, retrieve the row of the sender and add the values.

要添加分数,您可以将按钮所代表的表行的索引放在按钮的.Tag属性中。这样,您可以在事件处理程序中检索发件人的行并添加值。

Edit: Adding the number: On button creation, save the number in the tag (You could also just use the .Text, but doesn't really matter, it's one conversion less this way):

编辑:添加数字:在按钮创建时,将数字保存在标签中(您也可以使用.Text,但实际上并不重要,这是一种转换方式):

With TableButtons
 .Tag = Temporarynumber

And in the handler unpack the object again:

然后在处理程序中再次解压缩对象:

Overallscore += CInt(CType(sender, button).Tag)

#1


1  

You add an eventhandler to a control by using the AddHandler statement and providing the sub that handles the event.

通过使用AddHandler语句并提供处理该事件的子,将eventhandler添加到控件。

Private Sub Clickhandler(sender As Object, e As EventArgs)
  CType(sender, Button).BackColor = Color.AliceBlue
End Sub

Sender is basically the source that initiated the event. In this case the button you clicked.

发件人基本上是发起活动的来源。在这种情况下,您单击的按钮。

To make the button raise the event, add this to the code where you create the Buttons:

要使按钮引发事件,请将其添加到创建按钮的代码中:

Addhandler TableButtons.Click, AddressOf Clickhandler

To add the scores you can for example place the index of the table row the button represents to the .Tag property of the button. That way you can, in the event handler, retrieve the row of the sender and add the values.

要添加分数,您可以将按钮所代表的表行的索引放在按钮的.Tag属性中。这样,您可以在事件处理程序中检索发件人的行并添加值。

Edit: Adding the number: On button creation, save the number in the tag (You could also just use the .Text, but doesn't really matter, it's one conversion less this way):

编辑:添加数字:在按钮创建时,将数字保存在标签中(您也可以使用.Text,但实际上并不重要,这是一种转换方式):

With TableButtons
 .Tag = Temporarynumber

And in the handler unpack the object again:

然后在处理程序中再次解压缩对象:

Overallscore += CInt(CType(sender, button).Tag)