I made this class to help those out that want a spinner that looks like a slot machine reel.
Code:
Option Strict On
Option Explicit On
Public Class Spinner
Inherits Windows.Forms.Control
Private tmr As New Timer
Public Sub New()
tmr.Interval = _speed
AddHandler tmr.Tick, AddressOf Timer_Tick
Call SetPB()
End Sub
Private Sub Timer_Tick(sender As Object, e As EventArgs)
For i As Integer = 0 To pb_list.Count - 1
Dim pb As PictureBox = pb_list.Item(i)
pb.Top -= Jump
If pb.Bottom <= 0 AndAlso i <> 0 Then
pb.Top = pb_list.Item(i - 1).Bottom
ElseIf pb.Bottom <= 0 AndAlso i = 0 Then
pb.Top = pb_list.Item(pb_list.Count - 1).Bottom
End If
Next
End Sub
Private _speed As Integer = 50
Public Property Speed() As Integer
Get
Return _speed
End Get
Set(ByVal value As Integer)
_speed = value
tmr.Interval = _speed
End Set
End Property
Private _jump As Integer = 1
Public Property Jump() As Integer
Get
Return _jump
End Get
Set(ByVal value As Integer)
_jump = value
End Set
End Property
Private imgs As Bitmap() = {New Bitmap(1, 1)}
Public Property Images() As Bitmap()
Get
Return imgs
End Get
Set(ByVal value As Bitmap())
imgs = value
Call SetPB()
End Set
End Property
Private no_slots As Integer = 3
Public Property Number_of_Slots() As Integer
Get
Return no_slots
End Get
Set(ByVal value As Integer)
no_slots = value
Call SetPB()
End Set
End Property
Private spinning As Boolean
Public ReadOnly Property IsSpinning() As Boolean
Get
Return spinning
End Get
End Property
Private stop_even As Boolean = True
Public Property Stop_In_Slot() As Boolean
Get
Return stop_even
End Get
Set(ByVal value As Boolean)
stop_even = value
End Set
End Property
Private pb_list As New List(Of PictureBox)
Private Sub SetPB()
pb_list.Clear()
For i As Integer = 0 To imgs.Length - 1
Dim pb As New PictureBox
With pb
.Size = New Size(Me.Width, CInt(Me.Height / no_slots))
.Left = 0
.Top = .Size.Height * i
.Image = imgs(i)
.SizeMode = PictureBoxSizeMode.Zoom
End With
pb_list.Add(pb)
Next
Me.Controls.AddRange(pb_list.ToArray)
End Sub
Friend Sub StartSpinning()
spinning = True
tmr.Start()
End Sub
Friend Sub StopSpinning()
spinning = False
If stop_even Then
Dim at_slot As Boolean = False
Do Until at_slot = True
For i As Integer = 0 To pb_list.Count - 1
Dim pb As PictureBox = pb_list.Item(i)
pb.Top -= Jump
If pb.Bottom <= 0 AndAlso i <> 0 Then
pb.Top = pb_list.Item(i - 1).Bottom + 1
at_slot = True
ElseIf pb.Bottom <= 0 AndAlso i = 0 Then
pb.Top = pb_list.Item(pb_list.Count - 1).Bottom + 1
at_slot = True
End If
Next
Loop
End If
tmr.Stop()
End Sub
End Class
Some notes on it:
- Set the number of slots atleast 1 less than the total number of images
- The lower the speed, the higher it goes(based on timer interval)
To set the image list, it would look like this:
Code:
Dim arr() As Bitmap = {My.Resources.bell, My.Resources.Cherry, My.Resources.gold}
Spinner1.Images = arr
To start spinning the reels you call Spinner.StartSpinning and to stop the reels you call Spinner.StopSpinning.
Eventually I would like to replace the do loop if the stop_even variable is true with something else. Also, I'd like to add 'slot' spots so that when the image falls on that spot you can get it's value.