VB .NET – How to use WithEvents Example
As far as I am interesting in probabilities and similar theories in general, today I have decided to illustrate the WithEvents option in Visual Basic with the possibility to bet whether a rolled dice would be big (4,5,6) or small (1,2,3).
So, with the assistance of Visual Studio I have created the following application:

The idea is to guess whether an imaginary dice is small (1,2,3) or big (4,5,6). In case that you guess correctly, your bet is doubled and a small percentage is taken for the bank. Fair. You have two fields where you may give data – the top one where you give your bet money and the bottom one, where are your current money. In the picture above, the current money is 0 and the bet money is 1000. Which would result in a caught error, if you run.
Pretty much that is all about the simple interface. Let’s go back to the WithEvents part. When we press the “Run the bet”, we roll the imaginary dice and we call a sub called “CheckTheMoney”. This sub looks like this currently:
Public Sub CheckTheMoney()
If Me.BetMoney = Me.TotalMoney Then
RaiseEvent CheckMyMoney(Me, "All in! Was it worthy?")
ElseIf 3 * Me.BetMoney > Me.TotalMoney Then
RaiseEvent CheckMyMoney(Me, "You are playing with a lot of money!")
ElseIf 10 * Me.BetMoney < Me.TotalMoney Then
RaiseEvent CheckMyMoney(Me, "I know you can bet more...")
End If
End Sub
The idea is that it raises an event, if something of the abovementioned criteria is mentioned. It passes to the event the sender and a string. So, this is what happens later:
Public Class Form1
Private WithEvents myDice As New Dice()
Private Sub myDice_CheckTheMoney(ByVal sender As System.Object,
ByVal e As String) Handles myDice.CheckMyMoney
lblIssue.Text = e
End Sub
'more code here
end class
And at the end, we have our label named “lblIssue” updated with the string, which we have passed to the event. You may see the string in the forms here:

So pretty much that is all. Just before going to the code, here is a small summary of the names of the objects in the Visual Studio, in case that you would like to build something similar:
- rbSmaller
- rbBigger
- tbInitital
- tbBet
- Button1
- Button2
- lblStartingMoney
- lblResult
- lblCurrentMoney
- lblIssue
The elements starting with Rb are radiobuttons, tb – textbox, lbl – labels. Button1 and Button2 are buttons.
Pretty much that is all.
Here comes the code, available in GitHub as well:
Public Class Form1
Private bFirstGame As Boolean
Private iStartingMoney As Integer
Private WithEvents myDice As New Dice()
Private Sub myDice_CheckTheMoney(ByVal sender As System.Object,
ByVal e As String) Handles myDice.CheckMyMoney
lblIssue.Text = e
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
bFirstGame = True
lblIssue.Text = ""
tbInitial.Text = "1000"
tbInitial.ReadOnly = False
tbBet.Text = "200"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim iBet As Integer
Dim bSmaller As Boolean
lblIssue.Text = ""
Try
If bFirstGame Then
iStartingMoney = Convert.ToDouble(tbInitial.Text)
lblStartingMoney.Text = iStartingMoney.ToString()
myDice.TotalMoney = iStartingMoney
End If
tbInitial.ReadOnly = True
bFirstGame = False
iBet = Convert.ToDouble(tbBet.Text)
myDice.BetMoney = Convert.ToDouble(tbBet.Text)
bSmaller = rbSmaller.Checked
myDice = New Dice(myDice.TotalMoney, myDice.BetMoney, bSmaller)
Call myDice.RollTheDice()
tbInitial.Text = myDice.TotalMoney.ToString
lblResult.Text = myDice.Result.ToString
lblStartingMoney.Text = iStartingMoney.ToString
lblCurrentMoney.Text = myDice.TotalMoney.ToString
Catch ex As Exception
MessageBox.Show("Error" & vbCrLf & " Probably you have not given any input" & vbCrLf & "Or you are out of money...")
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
bFirstGame = True
tbInitial.Text = "1000"
tbInitial.ReadOnly = False
tbBet.Text = "200"
lblIssue.Text = ""
lblStartingMoney.Text = "Thank you"
lblResult.Text = "for"
lblCurrentMoney.Text = "playing!"
End Sub
End Class
Public Class Dice
Private p_iTotalMoney As Integer
Private p_iBetMoney As Integer
Private p_bHighResult As Boolean
Private p_sResult As String
Public Event CheckMyMoney(ByVal sender As Object, ByVal e As String)
Public Sub New()
p_iTotalMoney = 0
p_iBetMoney = 0
p_bHighResult = False
End Sub
Public Sub New(ByVal i_total_money As Integer, _
ByVal i_bet_money As Integer, _
ByVal bHighResult As Boolean)
TotalMoney = i_total_money
BetMoney = i_bet_money
p_bHighResult = bHighResult
End Sub
Public ReadOnly Property Result As String
Get
Result = p_sResult
End Get
End Property
Public Property BetMoney() As Integer
Get
Return Me.p_iBetMoney
End Get
Set(value As Integer)
p_iBetMoney = value
End Set
End Property
Public Property TotalMoney() As Integer
Get
Return p_iTotalMoney
End Get
Set(value As Integer)
p_iTotalMoney = value
End Set
End Property
Public Property HighResult() As Boolean
Get
Return p_bHighResult
End Get
Set(value As Boolean)
p_bHighResult = value
End Set
End Property
Private Function GetResult() As Integer
Dim rnd As New Random()
Return rnd.Next(1, 6)
End Function
Public Sub CheckTheMoney()
If Me.BetMoney = Me.TotalMoney Then
RaiseEvent CheckMyMoney(Me, "All in! Was it worthy?")
ElseIf 3 * Me.BetMoney > Me.TotalMoney Then
RaiseEvent CheckMyMoney(Me, "You are playing with a lot of money!")
ElseIf 10 * Me.BetMoney < Me.TotalMoney Then
RaiseEvent CheckMyMoney(Me, "I know you can bet more...")
End If
End Sub
Public Sub RollTheDice()
Dim iResult As Integer
iResult = Me.GetResult()
CheckTheMoney()
If (iResult > 3 And p_bHighResult) Or (iResult < 4 And Not p_bHighResult) Then
Me.TotalMoney += Me.BetMoney * 0.95
p_sResult = "You win and you pay a winning fee of 5 %"
Else
'lose
Me.TotalMoney -= Me.BetMoney
p_sResult = "You lose"
End If
End Sub
End Class
Enjoy it!