Creating events in VB.net

J

Jeffrey Spoon

Hello. I have been working on the program below. However I'm not sure
how to get my own event working properly. I created my ThresholdNormal
class and put a condition in the sub CheckThreshold. It then raises the
ThresholdOK event. My event handler Private Sub Threshold_Normal should
then run the EnterState7() sub. But the event is never raised and
judging from the fact that the label4 test I included doesn't update the
CheckThreshold() sub is never called. And I've no idea why. Any help
appreciated (sorry about the reams of code).

Code:

Form1:

Private StateVariableA As Integer 'A Main State
Private StateVariableB As Integer 'B Alarm substate
Private StateVariableBHistory As Integer 'B alarm substate
history
Protected Reading As Integer 'getReading variable
Protected LowerThreshold = 60 'thresholds
Protected UpperThreshold = 80
Protected count = -1 'debug
Private WithEvents Threshold As ThresholdNormal


Private Sub EnterState1()
'This is the Bed Empty (Default) State
'No patient in bed
StateVariableA = 1
StateAlbl.Text = StateVariableA
'
HeartRatelbl.Text = "Bed empty"
DataLogger.SetThresholds(LowerThreshold, UpperThreshold)
DataLogger.FastSpeed()
End Sub

Private Sub EnterState2()
'This is the Disable Alarm State
'Patient in bed
StateVariableB = 2
StateBlbl.Text = StateVariableB
'
DataLogger.SetDisableAlarm(True)
AlarmEnablebtn.Text = "Enable Alarm"
End Sub

Private Sub EnterState3()
'This is the Update Heart Rate State
'Responds to getReading event
StateVariableA = 3
StateAlbl.Text = StateVariableA
'
Reading = DataLogger.GetReading
' If Reading <= UpperThreshold And Reading >= LowerThreshold
Then
'Alarmlbl.Text = "Should be normal"
'End If
HeartRatelbl.Text = Reading
'
count = count + 1
countlbl.Text = count
Threshold.CheckThreshold()

End Sub

Private Sub EnterState4()
'This is the Alarm Enabled State
'Responds to Alarm Enabled event
StateVariableB = 4
StateBlbl.Text = StateVariableB
'
DataLogger.SetDisableAlarm(False)
AlarmEnablebtn.Text = "Disable Alarm"
End Sub

Private Sub EnterState5()
'This is the Alarm Substate
'This deals with State 5B
StateVariableA = 5
StateAlbl.Text = StateVariableA
EnterState2()
End Sub

Private Sub EnterState6()
'This is the Alarm activated state
'
StateVariableA = 6
StateAlbl.Text = StateVariableA
Alarmlbl.Text = "Threshold Exceeded"
End Sub

Private Sub EnterState7()
'This is the alarm reset (de-activate) state
'
StateVariableA = 7
StateAlbl.Text = StateVariableA
Alarmlbl.Text = "Threshold Normal" 'reset alarm
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Form load event
Threshold = New ThresholdNormal
EnterState1()
End Sub

Private Sub Patient_Bed_toggle(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles PatientInbtn.Click

If StateVariableA = 1 Then
EnterState5()
End If

End Sub



Private Sub Reading_Ready_get(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles DataLogger.NextReadingReady
'NextReadingReady() event handler

If StateVariableA = 5 Then
EnterState3()
ElseIf StateVariableA = 3 Then
EnterState3()
ElseIf StateVariableA = 6 Then
EnterState3()
ElseIf StateVariableA = 7 Then
EnterState3()
End If
End Sub

Private Sub Alarm_Toggle_click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles AlarmEnablebtn.Click
'Alarm Enabled/Disabled event handler

If StateVariableB = 2 Then
StateVariableBHistory = 2
StateBHlbl.Text = StateVariableBHistory
EnterState4()
ElseIf StateVariableB = 4 Then
StateVariableBHistory = 4
StateBHlbl.Text = StateVariableBHistory
EnterState2()
ElseIf StateVariableA = 6 Then
EnterState5()
ElseIf StateVariableA = 7 Then
EnterState5()
ElseIf StateVariableA = 3 Then
If StateVariableBHistory = 2 Then
EnterState2()
ElseIf StateVariableBHistory = 4 Then
EnterState4()
End If
End If
End Sub

Private Sub Upper_Threshold_exceed(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles DataLogger.UpperThresholdExceeded
'Upper threshold exceeded event handler

If StateVariableA = 7 Then
EnterState6()
ElseIf StateVariableB = 4 Then
EnterState6()
ElseIf StateVariableA = 3 Then
EnterState6()
End If

End Sub

Private Sub Lower_Threshold_exceed(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles DataLogger.LowerThresholdExceeded
'Lower threshold exceeded event handler

If StateVariableA = 7 Then
EnterState6()
ElseIf StateVariableB = 4 Then
EnterState6()
ElseIf StateVariableA = 3 Then
EnterState6()
End If

End Sub

Private Sub Threshold_Normal() Handles Threshold.ThresholdOK
If StateVariableA = 6 Then
EnterState7()
If StateVariableA = 3 Then
EnterState7()
End If
End If
End Sub

End Class

Class ThresholdNormal
Inherits Screen1

Public Event ThresholdOK()

Public Sub CheckThreshold()
If Reading <= UpperThreshold And Reading >= LowerThreshold Then
RaiseEvent ThresholdOK()

End If
Label4.Text = Reading
Label4.Text = "hello"
End Sub

End Class
 
J

JohnFol

The class that generates the events is based upon inheriting Screen1, but
it's not clear from the listing what this is, if it over rides anything etc.

Can I suggest you stick a break points in on the line Public Sub
CheckThreshold()
and add a simple try catch block. Then step through the code to see if it
gets as far as RaiseEvent ThresholdOK()
 
J

Jeffrey Spoon

JohnFol said:
The class that generates the events is based upon inheriting Screen1, but
it's not clear from the listing what this is, if it over rides anything etc.

Can I suggest you stick a break points in on the line Public Sub
CheckThreshold()
and add a simple try catch block. Then step through the code to see if it
gets as far as RaiseEvent ThresholdOK()

Sorry, I chopped off the very top of the code:

Public Class Screen1
Inherits System.Windows.Forms.Form

It's just a default form, but I have another form which is empty at the
moment, which will be used eventually. All I have done for now is made
Screen1 the start-up object in project properties.

I will try what you suggested also. Thanks for the response.
 
J

Jeffrey Spoon

JohnFol said:
The class that generates the events is based upon inheriting Screen1, but
it's not clear from the listing what this is, if it over rides anything etc.

Can I suggest you stick a break points in on the line Public Sub
CheckThreshold()
and add a simple try catch block. Then step through the code to see if it
gets as far as RaiseEvent ThresholdOK()


Ok I've done this. It is calling CheckThreshold each time but the event
is never raised because the Reading value is always 0 for some reason. I
have inherited Screen1 (where Reading resides) and it is protected so I
thought that would've worked. Also, the reading is displayed properly on
the form. Something else I don't understand is that the label4 variable
is being updated according to the debugger but it is not being updated
on the actual form.
 
J

Jeffrey Spoon

JohnFol said:
The class that generates the events is based upon inheriting Screen1, but
it's not clear from the listing what this is, if it over rides anything etc.

Can I suggest you stick a break points in on the line Public Sub
CheckThreshold()
and add a simple try catch block. Then step through the code to see if it
gets as far as RaiseEvent ThresholdOK()

Ok, it must have been since Reading was updated within a sub procedure
when the ThresholdNormal class was inheriting the attributes it was just
using the default declaration as it wouldn't see the local assignment.
So now I pass the reading as a parameter to the CheckThreshold(Reading)
sub and it worked fine.

The only thing I'm not sure about is why label4 is not updated. I assume
this is because it is not set on the form Screen1 but in a separate
class. Is some sort of access required in the properties somewhere or
should it just be done within the associated form?

Thanks John (I'll probably be back here shortly with more problems,
you'll be happy to know...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top