Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27203

null reference expection was unhandled? help?

$
0
0
im trying to make a matching game and iv only just started the code and im expecting 2 icons to stay briefly when I click them and then disappear but now when I click 1 I cant click the other and get the message null reference expection was unhandled the code is
Code:

Public Class Form1


    'first clicked points to the first label control
    'that the player clicks, but it will be nothing
    'if the player hasnt clicked a label yet
    Private firstclicked As Label = Nothing

    'second clicked points to the second label control
    'that the player clicks
    Private secondclicked As Label = Nothing


    ' Use this Random object to choose random icons for the squares
    Private random As New Random

    ' Each of these letters is an interesting icon
    ' in the Webdings font,
    ' and each icon appears twice in this list
    Private icons =
      New List(Of String) From {"!", "!", "N", "N", ",", ",", "k", "k",
                                "b", "b", "v", "v", "w", "w", "z", "z"}


    ''' <summary>
    ''' Assign each icon from the list of icons to a random square
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AssignIconsToSquares()

        ' The TableLayoutPanel has 16 labels,
        ' and the icon list has 16 icons,
        ' so an icon is pulled at random from the list
        ' and added to each label
        For Each control In TableLayoutPanel1.Controls
            Dim iconLabel = TryCast(control, Label)
            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

    Public Sub New()
        ' This call is required by Windows Form Designer
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call
        AssignIconsToSquares()
    End Sub

    Private Sub label_click(ByVal sender As System.Object,
                            ByVal e As System.EventArgs) Handles Label9.Click, Label8.Click,
        Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label3.Click, Label2.Click,
        Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click, Label11.Click,
        Label10.Click, Label1.Click

        If Timer1.Enabled Then Exit Sub

        Dim clickedlabel = TryCast(sender, Label)

        If clickedlabel IsNot Nothing Then

            'if the clicked label is black, the player clicked
            'an icon thats already been revealed
            'ignore the click
            If clickedlabel.ForeColor = Color.Black Then Exit Sub

            'if first clicked is nothing,this is the first icon
            'so in the pair that the player clicked,
            'so set the first clicked to the label that the player
            'clicked, change its colour to black, and return
            If firstclicked Is Nothing Then
                firstclicked = clickedlabel
                firstclicked.ForeColor = Color.Black

                Timer1.Start()
                Exit Sub
            End If


        End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        'stop the timer
        Timer1.Stop()

        'hide both icons
        firstclicked.ForeColor = firstclicked.BackColor
        secondclicked.ForeColor = secondclicked.BackColor

        'reset firstclicked and second clicked
        'so the next time a label is
        'clicked, the program knows its the first clicked
        firstclicked = Nothing
        secondclicked = Nothing
    End Sub
End Class


Viewing all articles
Browse latest Browse all 27203

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>