Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 23-Apr-13 11:28am
v2

There is no such stupid think as TryCast in .NET, this is a pure VB.NET thingy, pretty much equivalent to the as operator:
http://msdn.microsoft.com/en-us/library/zyy863x8%28v=vs.110%29.aspx[^].

In your case, the use of it tries to cast Control to Label. As all labels are controls but not all controls are labels, this is down-casting, which can be successful or not. The unsuccessful cast (when an instance of Control is not a Label is indicated by Null (VB.NET Nothing). Pretty simple, isn't it?

And this is totally unrelated to the problem of "original state" of the form. There is no such facility in .NET, and, if you think about it, it should not even exist. Only the author of a form code knows what's supposed to be considered as the "original" state of a form or anything at all. When you merely add some controls to the form, you create some state machine. It might be not just controls, but also any other instance members you may need to add. You never indicate the phase when the state is to be claimed as "initial"; the code add controls one by one and modifies some of its properties.

In particular, I can suggest that you develop a method which turns all the form state in some "initial" state. For consistency, you can call this method once at some moment of time the form is first shown on screen, and later call the same method when it is needed.

—SA
 
Share this answer
 
Hello Sergey,
Thank you for explaining TryCast, I think I have understood how it works. What you are saying is that TryCast is trying to cast or place (in this case a symbol) to the label and it will continue to do so until all Labels contain a symbol. If it should try to do the same to a different control, Nothing is returned and the TryCast process ends.

What I meant when I say its original state, is the way the form loads on running the program. I understand that the process randomizes the text in each of the labels and as such will be different every time. This is the state I would like to be able to return the program to when button1 is clicked.
Having used the Shell command to run the program executable, I realised the .exe file would not be in the same location on a users machine and would evidently stop the program. I have continued to work on this particular problem and came up with this;

Shell(Path.Combine(Application.StartupPath, "QuickMatch.exe"))

I am hoping that by using the StartupPath the .exe will be in the same location on all machines, this would ensure no errors.



If you think I am making a mistake by re-setting the form (to its original state) in this way, please let me know.

You have given me plenty to think about and I am truly grateful.

Sergey,
Thank you.

Busted Bumper
 
Share this answer
 
Comments
Joezer BH 9-Feb-14 5:00am    
You can comment below the answer
(you posted this as an additional answer!)

Cheers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900