peterbest69
peterbest69

Reputation: 49

Computer Click Event Visual Basic

How do I create a random click event driven by the computer? Where the user doesn't get to choose where it clicks? I have created a table that is 5 buttons wide and 5 buttons tall, and created a click event where the user clicks on a button and the random number value is added onto their score.

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 UserTableButtonClickEvent
        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


Private Sub UserTableButtonClickEvent(sender As Object, e As EventArgs)
    CType(sender, Button).BackColor = Color.LightSteelBlue
    OverAllScoreInteger += CInt(CType(sender, Button).Tag)
    GameScreen.UserScoreBox.Text = OverAllScoreInteger.ToString
    Dim TableButton As Button = CType(sender, Button)
    TableButton.Text = "#"
    TableButton.Enabled = False
End Sub

How do I make another event like this but at random?

Upvotes: 0

Views: 280

Answers (1)

Jens
Jens

Reputation: 6375

Enumerate all controls in the GameScreen.Controls collection and add them to a temporary list if they are a button. Then generate a random number in the range of the length of the list and click the button with this index:

Dim TempButtonList As New List(Of Button)
For Each c as Control in GameScreen.Controls
  If TypeOf(c) Is Button Then TempButtonList.Add(CType(c, Button))
Next
Dim Rnd as New Random
TempButtonList(rnd.Next(0, TempButtonList.Count-1)).PerformClick()

The PerformClick() method does exactly the same as if you would click that button with your mouse. The If in the loop makes sure you only add buttons in the controls collection, because there may of course be other controls inside.

Complete example

It's probably easier that way (Add a FlowLayoutPanel with Size=275; 275 and two Labels to a Form):

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim Rnd As New Random
    For i = 1 To 25
        Dim b As New Button With {.Text = Rnd.Next(0, 100)}
        b.Width = 40
        b.Height = 40
        FlowLayoutPanel1.Controls.Add(b)
        AddHandler b.Click, AddressOf b_hndl
    Next
End Sub
Private MyScore As Integer = 0
Private HisScore As Integer = 0
Dim Zug As Boolean = True
Private Sub b_hndl(sender As Object, e As EventArgs)
    Dim ThisB As Button = CType(sender, Button)
    Dim CurrScore As Integer = CInt(ThisB.Text)
    Dim CurrZug As Boolean = Zug
    Zug = Not Zug
    ThisB.Enabled = False
    ThisB.Text = "#"
    If CurrZug Then
        MyScore += CurrScore
        Label1.Text = MyScore.ToString
        ComputerMove()
    Else
        HisScore += CurrScore
        Label2.Text = HisScore.ToString
    End If
End Sub
Private Sub ComputerMove()
    Dim TempButtonList As New List(Of Button)
    For Each c As Control In FlowLayoutPanel1.Controls
        If TypeOf (c) Is Button Then
            Dim thisb As Button = CType(c, Button)
            If thisb.Enabled Then TempButtonList.Add(thisb)
        End If
    Next
    If TempButtonList.Count = 0 Then Exit Sub
    Dim Rnd As New Random
    TempButtonList(Rnd.Next(0, TempButtonList.Count - 1)).PerformClick()
End Sub
End Class

The Dim Zug As Boolean = True determines if the player or the computer is currently in queue to select a button. It is switched on every button press, so the players take turns. The ComputerMove function is executed after the player clicked a button.

Upvotes: 1

Related Questions