After studying this project, I found I did not understand exactly what "TryCast(sender, Label) means or does.
I then wanted to re-start the program with another set of random characters but I can't do it any other way than as in Button1_Click1. Here I use the Shell command to run the .exe file in the project Bin/Debug folder. I know this is a useless way to do this but I cannot find another way to re-start the program with all new positions for icons.
Any help would be truly appreciated.
Please check out the code below.
Public Class Form1
Private firstClicked As Label = Nothing
Private secondClicked As Label = Nothing
Private random As New Random
Private icons = New List(Of String) From {"!", "!", "b", "b", "$", "$", "%", "%", "~", "~", "j", "j", "J", "J", "Z", "Z",
"f", "f", "h", "h", "m", "m", "o", "o", "p", "p", "u", "u", "s", "s", "-", "-", "U", "U", "O", "O"}
Public Sub New()
' This call is required by Windows Form Designer but I do not know why?????
InitializeComponent()
' Add any initialization after the InitializeComponent() call
AssignIconsToSquares()
End Sub
Private Sub AssignIconsToSquares()
For Each control In TableLayoutPanel1.Controls
Dim iconLabel = TryCast(control, Label) 'What does this mean and do?
If iconLabel IsNot Nothing Then
Dim randomNumber = random.Next(icons.Count)
iconLabel.Text = icons(randomNumber)
iconLabel.ForeColor = iconLabel.BackColor
icons.RemoveAt(randomNumber)
End If
Next
End Sub
Dim Trys As Integer
Private Sub Label_click(sender As System.Object, e As System.EventArgs) Handles Label9.Click, Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label30.Click, Label3.Click, Label29.Click, Label28.Click, Label27.Click, Label26.Click, Label25.Click, Label24.Click, Label23.Click, Label22.Click, Label21.Click, Label20.Click, Label2.Click, Label19.Click, Label18.Click, Label17.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click, Label11.Click, Label10.Click, Label1.Click, Label31.Click, Label32.Click, Label33.Click, Label34.Click, Label35.Click, Label36.Click
Trys = Trys + 1
Me.Text = "You have used " & Trys & " Trys"
Dim clickedLabel = TryCast(sender, Label) 'I do not understand this??
If clickedLabel IsNot Nothing Then
If clickedLabel.ForeColor = Color.Black Then Exit Sub
clickedLabel.ForeColor = Color.Black
If firstClicked Is Nothing Then
firstClicked = clickedLabel
firstClicked.ForeColor = Color.Black
Exit Sub
End If
secondClicked = clickedLabel
secondClicked.ForeColor = Color.Black
CheckForWinner()
If firstClicked.Text = secondClicked.Text Then
firstClicked = Nothing
secondClicked = Nothing
Exit Sub
End If
Timer1.Start()
End If
End Sub
Private Sub CheckForWinner()
For Each control In TableLayoutPanel1.Controls
Dim iconLabel = TryCast(control, Label) 'mmmmmmmm
If iconLabel IsNot Nothing AndAlso
iconLabel.ForeColor = iconLabel.BackColor Then Exit Sub
Next
' If the loop didn't return, it didn't find any unmatched icons, the user won. Show a message and close the form
Me.Text = "Congratulations You have found all Pairs. You have used " & Trys & " Trys"
Button1.Visible = True
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
firstClicked.ForeColor = firstClicked.BackColor
secondClicked.ForeColor = secondClicked.BackColor
firstClicked = Nothing
secondClicked = Nothing
End Sub
Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.Close()
Button1.Visible = False
Trys = 0
Me.Text = "You have used " & Trys & " Trys"
'Here, I need to re-start the game with a fresh list of Random symbols but I have no idea how.
'So I used the following code to re-run the project
Shell("C:\Users\Rapid\Documents\Visual Studio 2010\Projects\QuickMatch\QuickMatch\bin\Debug\QuickMatch.exe")
'The outcome is spot on! The program randomly places icons on every label contained within the TableLayoutPanel
End Sub
Public Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
WindowState = vbNormal
End Sub
End Class