Rudi
  • Home
  • About
  • Contact
  • VB.Net
    • Flippin
    • Calculator
    • Mine Scout
    • Slot Machine
    • Snake
    • Accounting using Child forms
  • Excel
    • Calendar 1
    • Calendar 2
    • Sudoku
    • Mine Scout
    • Tilexcel
    • ExceLudo
    • Alphabet Game
  • C# Child Forms
  • C# Struct Array
VB.Net Label Array

Flippin Project
VB.Net assigns an Index number to each control. Each time a control is added at the design stage, it becomes the 0 control and all other controls are incremented by 1.
In the Flippin project I want the label array to have the index numbers 1 to 36. Start with 3 controls.
Picture
Then add a label. Make its size 38 * 28, Autosize to False, Font to Arial Bold size 12pt, TextAlign to Center and BackColor to White. Copy this to get 6 labels down by 6 labels across.

Lastly add the title label. This means the labels in the array have the index numbers 1 to 36.
Picture
Picture



Select all the number labels then switch to Events by clicking the flash in the properties box. Double click the Click event to start a procedure for all the number labels.


Private Sub Label32_Click(sender As Object, e As EventArgs) Handles Label9.Click,
    Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label37.Click,
    Label36.Click, Label35.Click, Label34.Click, Label33.Click, Label32.Click,
    Label31.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, Label19.Click, Label18.Click,
    Label17.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click,
    Label12.Click, Label11.Click, Label10.Click, Label1.Click

    'The following code shows the Index numbers of the label clicked
    'Rem these lines later

    CType(sender, Label).Text() = CStr(Controls.IndexOf(CType(sender, Label)))
    CType(sender, Label).BackColor = Color.Red

  End Sub

In the picture below I have clicked every label to show its index number. The first array label I created had the index number 0 but it is now 36.

Picture
Flippin is a brain training game where the labels are flipped to find 2 matching numbers. Double click the New Game button to code randomly arranging the numbers in the Text property of each label. We'll use 2 global variables to keep track of which 2 tiles are flipped.

Dim intFirstTile As Integer
Dim intSecondTile As Integer

  Private Sub btnNewGame_Click(sender As Object, e As EventArgs) Handles btnNewGame.Click

    'Fill the array labels 1 to 18 with those numbers in the Text property
    'and also labels 19 to 36 with the numbers 1 to 18, ie 2 pairs
    'In this procedure we use the control item numbers to loop through the labels
    'It's confusing that here it's called an item number rather than an index number


    For i As Integer = 1 To 18

      Me.Controls.Item(i).Text = i
      Me.Controls.Item(i + 18).Text = i

    Next

    'Randomly swap the numbers
    Dim intTemp As Integer, intRnd As Integer
    Randomize()

    For i = 1 To 36

      intRnd = Int((36 * Rnd()) + 1)

      'Do the swapping
      intTemp = Me.Controls.Item(i).Text
      Me.Controls.Item(i).Text = Me.Controls.Item(intRnd).Text
      Me.Controls.Item(intRnd).Text = intTemp

      'Set all the colors to white to hide the numbers
      Me.Controls.Item(i).BackColor = Color.White

      'Rem this line if you want to see the numbers for now
      Me.Controls.Item(i).ForeColor = Color.White

    Next

    'Initialize pointers
    lblAttempts.Text = 0
    intFirstTile = 0
    intSecondTile = 0

  End Sub


The above procedure should be called at start-up. Double click the form.

  Private Sub frmFlippin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    btnNewGame_Click(sender, e)
  End Sub
Picture
Now we'll add code to the label array code from earlier to be able to play the game.

  Private Sub Label32_Click(sender As Object, e As EventArgs) Handles Label9.Click,
    Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label37.Click,
    Label36.Click, Label35.Click, Label34.Click, Label33.Click, Label32.Click,
    Label31.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, Label19.Click, Label18.Click,
    Label17.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click,
    Label12.Click, Label11.Click, Label10.Click, Label1.Click

    'Rem these lines
    'CType(sender, Label).Text() = CStr(Controls.IndexOf(CType(sender, Label)))
    'CType(sender, Label).BackColor = Color.Red


    'If the tile clicked is red then exit

    If CType(sender, Label).BackColor.Name = "Red" Then Exit Sub

    'If the tile clicked displays a number then exit
    If CType(sender, Label).ForeColor.Name = "Black" Then Exit Sub

    'If 2 numbers are displayed -----
    If intSecondTile > 0 Then

      If Me.Controls.Item(intFirstTile).BackColor.Name = "Red" Then
        '----- with a red background then erase the numbers on them
        Me.Controls.Item(intFirstTile).Text = ""
        Me.Controls.Item(intSecondTile).Text = ""
      Else
        '----- otherwise hide the numbers by setting the colors to white
        Me.Controls.Item(intFirstTile).ForeColor = Color.White
        Me.Controls.Item(intSecondTile).ForeColor = Color.White
      End If

      'Reset pointers
      intFirstTile = 0
      intSecondTile = 0

    End If

    'Show the number on the tile clicked
    CType(sender, Label).ForeColor = Color.Black

    'If it's the first tile then record the index number and exit
    If intFirstTile = 0 Then
      intFirstTile = Controls.IndexOf(CType(sender, Label))
      Exit Sub
    End If

    'It's the second tile, so firstly record its index number
    intSecondTile = Controls.IndexOf(CType(sender, Label))

    'If the numbers on the tiles match, then set the BackColor of both tiles to red
    If Me.Controls.Item(intFirstTile).Text =
        Me.Controls.Item(intSecondTile).Text Then

      Me.Controls.Item(intFirstTile).BackColor = Color.Red
      Me.Controls.Item(intSecondTile).BackColor = Color.Red

    End If

    'The 2 numbers will be erased if they are red/solved at the start of the next click of this sub
    'They will be hidden again if not red.

    'Keep a score

    lblAttempts.Text += 1

  End Sub
Picture



Now where did I see a flippin 10 before?
Proudly powered by Weebly