Rolling Numbers Game in Visual Basic 2008

This time I will teach you how to create Rolling Numbers Game in Visual Basic 2008. In these features the numbers are rolled randomly and it has a Progress Bar that serves as a timer. Let's begin: Open the Visual Basic 2008, create a Windows Application and do the Form just like this. first form Double click the Timer and do the following code. The event is, everytime the timer ticks, the numbers in the Textbox will randomly roll. And if the progress bar reached its maximum value, the timer and the number in the Textbox will stop.
  1.    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  2.  
  3.         'DECLARING A RANDOM VARIABLE TO STORE THE NUMBERS ON IT.
  4.         'IT REPRESENTS A PSUEDO RANDOM GENERATOR.
  5.         Dim num As Random = New Random()
  6.         'DECLARING AN INTEGER VARIABLE TO STORE THE NONNEGATIVE RANDOM NUMBERS.
  7.         'STARTS WITH THE MINIMUM VALUE TO THE MAXIMUM VALUE
  8.         'NEXT REPRESENTS TO RETURN A RANDOM NUMBER WITHIN A SPECIFIED RANGE.
  9.         Dim result As Integer = num.Next(1, 7)
  10.         'PUT THE RESULT IN THE TEXTBOX.
  11.         TextBox1.Text = result.ToString
  12.         'PROGRESSBAR INCREASED BY 1.
  13.         ProgressBar1.Value += 1
  14.         'THE CONDITION IS, WHEN THE PROGRESSBAR VALUE REACHED TO 100
  15.         'THEN THE TIMER WILL STOP AND THE NUMBER IN THE TEXTBOX WILL STOP ROLLING TOO.
  16.         If ProgressBar1.Value = 100 Then
  17.             'FILTERING THE CHECKBOX.
  18.             'IF THE CHECKBOX1 IS CHECKED THE MESSAGE WILL APPEAR IN THE LISTBOX.
  19.             If CheckBox1.CheckState = CheckState.Checked Then
  20.                 If TextBox1.Text = CheckBox1.Text Then
  21.                     'ADD THE STRING MESSAGE IN THE LISTBOX.
  22.                     ListBox1.Items.Add("The number " & TextBox1.Text & " won!")
  23.                 End If
  24.             End If
  25.  
  26.             If CheckBox2.CheckState = CheckState.Checked Then
  27.                 If TextBox1.Text = CheckBox2.Text Then
  28.                     ListBox1.Items.Add("The number " & TextBox1.Text & " won!")
  29.                 End If
  30.             End If
  31.             If CheckBox3.CheckState = CheckState.Checked Then
  32.                 If TextBox1.Text = CheckBox3.Text Then
  33.                     ListBox1.Items.Add("The number " & TextBox1.Text & " won!")
  34.                 End If
  35.             End If
  36.  
  37.             If CheckBox4.CheckState = CheckState.Checked Then
  38.                 If TextBox1.Text = CheckBox4.Text Then
  39.                     ListBox1.Items.Add("The number " & TextBox1.Text & " won!")
  40.                 End If
  41.             End If
  42.  
  43.             If CheckBox5.CheckState = CheckState.Checked Then
  44.                 If TextBox1.Text = CheckBox5.Text Then
  45.                     ListBox1.Items.Add("The number " & TextBox1.Text & " won!")
  46.                 End If
  47.             End If
  48.  
  49.             If CheckBox6.CheckState = CheckState.Checked Then
  50.                 If TextBox1.Text = CheckBox6.Text Then
  51.                     ListBox1.Items.Add("The number " & TextBox1.Text & " won!")
  52.                 End If
  53.             End If
  54.  
  55.             If CheckBox7.CheckState = CheckState.Checked Then
  56.                 If TextBox1.Text = CheckBox7.Text Then
  57.                     ListBox1.Items.Add("The number " & TextBox1.Text & " won!")
  58.                 End If
  59.             End If
  60.             Timer1.Stop()
  61.         End If
  62.  
  63.     End Sub
Go back to the Design Views, double click the Button and do the following code for starting the timer and assigning the Progress Bar of a minimum value.
  1. Private Sub btnRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRoll.Click
  2.         'CLEARING THE LISTBOX
  3.         ListBox1.Items.Clear()
  4.         'timer starts
  5.         Timer1.Start()
  6.         'PROGRESSBAR VALUE RETURNS TO 0 EVERYTIME YOU CLICK THE BUTTON
  7.         ProgressBar1.Value = 0
  8.     End Sub
The complete source code is included and you can dowload it.

Add new comment