Visual Basic 2010 Graphical Dice

Dice is an important tool used in many board games, virtual or physical. We have shown you how to create graphical dice using Visual Basic 6, you can check out the link below to view the code in VB6.

http://www.vbtutor.net/VB_Sample/dice3.htm

Writing the code to create a graphical dice in Visual Basic 2010 is slightly different from the code in VB6. In VB6, we used the shape control to draw the dots. However, in Visual Basic 2010, there is no built-in shape tool so we use images instead. Therefore, the first step in creating a graphical dice in Visual Basic 2010 is to create six images, each denote one of the faces of the dice. Next, you write a code that load the images randomly, it is that simple.

The Interface

Visual Basic 2010 dice


The Code

Public Class Form1
Dim m, n As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
m = m + 10
If m < 1000 Then

n = Int(1 + Rnd() * 6)
Select Case n
Case 1
PictureBox1.Image = Image.FromFile(“C:\Documents and Settings\Voon Kiong Liew\My Documents\Liew Folder\VB_Sample\Images\dice1.jpg”)
Case 2
PictureBox1.Image = Image.FromFile(“C:\Documents and Settings\Voon Kiong Liew\My Documents\Liew Folder\VB_Sample\Images\dice2.jpg”)
Case 3
PictureBox1.Image = Image.FromFile(“C:\Documents and Settings\Voon Kiong Liew\My Documents\Liew Folder\VB_Sample\Images\dice3.jpg”)
Case 4
PictureBox1.Image = Image.FromFile(“C:\Documents and Settings\Voon Kiong Liew\My Documents\Liew Folder\VB_Sample\Images\dice4.jpg”)
Case 5
PictureBox1.Image = Image.FromFile(“C:\Documents and Settings\Voon Kiong Liew\My Documents\Liew Folder\VB_Sample\Images\dice5.jpg”)
Case 6
PictureBox1.Image = Image.FromFile(“C:\Documents and Settings\Voon Kiong Liew\My Documents\Liew Folder\VB_Sample\Images\dice6.jpg”)
End Select
Else
Timer1.Enabled = False
m = 0

End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = True

End Sub
End Class