Not 100% sure how to do this, mainly because I am new to programming here!
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?
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'
You add an eventhandler to a control by using the AddHandler
statement and providing the sub that handles the event.
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.
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):
With TableButtons
.Tag = Temporarynumber
And in the handler unpack the object again:
Overallscore += CInt(CType(sender, button).Tag)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.