VB Tutor VB.NET 2022 Tutorial VB2019 Tutorial VB6 Tutorial VB Sample Code About Us
Visual Basic Sample Code

Animated Dice

Visual Basic implementation with animation effects


This animated dice program demonstrates how to create engaging visual effects using Visual Basic. The program simulates dice rolling with a realistic animation sequence before landing on a random number between 1 and 6.

The animation is achieved using a Timer control that rapidly changes the dice faces to create the rolling effect. This example shows both VB6 and VB.NET implementations, highlighting the differences between these two versions of Visual Basic.

Interactive Dice Demo

Try the interactive dice simulation below. Click the "Roll Dice" button to see the animation in action!

Technology Used

Windows Forms

GUI framework for creating desktop applications

Timer Control

Creates the animation effect by triggering events at intervals

Random Numbers

Generates unpredictable dice rolls

Shape Controls

Visual elements that form the dice faces

VB6 Implementation

The original VB6 implementation uses Shape controls to represent the dots on the dice. The animation is controlled by a Timer that rapidly changes which shapes are visible.

Design Interface

VB6 Design Interface

Runtime Demo

VB6 Code

Dim x As Integer

Sub roll()
    x = x + 10
    Randomize Timer
    n = Int(1 + Rnd * 6)
    
    ' Hide all shapes initially
    For i = 0 To 6
        Shape1(i).Visible = False
    Next i
    
    ' Show the appropriate shapes for each dice face
    If n = 1 Then
        Shape1(3).Visible = True
        Shape2.FillColor = &HC0C0C0
    ElseIf n = 2 Then
        Shape1(2).Visible = True
        Shape1(4).Visible = True
        Shape2.FillColor = &H8080FF
    ElseIf n = 3 Then
        Shape1(2).Visible = True
        Shape1(3).Visible = True
        Shape1(4).Visible = True
        Shape2.FillColor = &H80FF&
    ElseIf n = 4 Then
        Shape1(0).Visible = True
        Shape1(2).Visible = True
        Shape1(4).Visible = True
        Shape1(6).Visible = True
        Shape2.FillColor = &HFFFF00
    ElseIf n = 5 Then
        Shape1(0).Visible = True
        Shape1(2).Visible = True
        Shape1(3).Visible = True
        Shape1(4).Visible = True
        Shape1(6).Visible = True
        Shape2.FillColor = &HFFFF&
    ElseIf n = 6 Then
        Shape1(0).Visible = True
        Shape1(1).Visible = True
        Shape1(2).Visible = True
        Shape1(4).Visible = True
        Shape1(5).Visible = True
        Shape1(6).Visible = True
        Shape2.FillColor = &HFF00FF
    End If
End Sub

Private Sub Roll_Dice_Click()
    Timer1.Enabled = True
    x = 0
End Sub

Private Sub Timer1_Timer()
    If x < 1000 Then
        roll
    Else
        Timer1.Enabled = False
    End If
End Sub

VB.NET Implementation

The VB.NET version uses a similar approach but with modern .NET features. The main differences include:

VB.NET Code

Public Class DiceForm
    Private x As Integer = 0
    Private rand As New Random()
    Private diceImages As New List(Of Image)()
    
    Private Sub DiceForm_Load(sender As Object, e As EventArgs) _
        Handles MyBase.Load
        
        ' Load dice images (1.png to 6.png)
        For i = 1 To 6
            diceImages.Add(Image.FromFile($"dice_{i}.png"))
        Next
    End Sub
    
    Private Sub RollDice()
        x += 10
        Dim n As Integer = rand.Next(1, 7)
        DicePictureBox.Image = diceImages(n - 1)
    End Sub
    
    Private Sub RollButton_Click(sender As Object, e As EventArgs) _
        Handles RollButton.Click
        
        AnimationTimer.Enabled = True
        x = 0
    End Sub
    
    Private Sub AnimationTimer_Tick(sender As Object, e As EventArgs) _
        Handles AnimationTimer.Tick
        
        If x < 1000 Then
            RollDice()
        Else
            AnimationTimer.Enabled = False
            ' Show final result message
            MessageBox.Show($"Dice shows: {GetDiceValue()}")
        End If
    End Sub
    
    Private Function GetDiceValue() As Integer
        ' Determine current dice value based on image
        Return diceImages.IndexOf(DicePictureBox.Image) + 1
    End Function
End Class

Key Differences Between VB6 and VB.NET

  • Object-Oriented: VB.NET is fully object-oriented while VB6 has limited OOP support
  • Resource Management: VB.NET uses garbage collection and the IDisposable pattern
  • Event Handling: VB.NET uses the Handles clause instead of procedure naming conventions
  • Graphics: VB.NET has superior graphics capabilities with GDI+
  • Error Handling: VB.NET uses Try/Catch instead of On Error
Visual Basic Sample Code Book

Visual Basic Programming with Code Examples

Learn both VB6 and VB.NET with practical examples like this animated dice program. This comprehensive guide covers:

  • Core concepts of Visual Basic programming
  • Side-by-side VB6 and VB.NET code comparisons
  • GUI development with Windows Forms
  • Database programming with ADO.NET
  • Game development techniques
  • Real-world application examples
Buy Now on Amazon Kindle Edition