Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RaiseEvent skipped 1

Status
Not open for further replies.

Reinout

IS-IT--Management
Feb 22, 2004
48
BE
I did a search in the forum but nothing seemed to work for me.

Where I want to call the event :

Public Class Test
Inherits System.Windows.Forms.UserControl
Public Event TestEvent()

Private sub initPage()
RaiseEvent TestEvent()
End sub
End Class



Where I tell the event what to do :

Public Class frmFood
Inherits frmBase (custom usercontrol)
Private WithEvents ucTest As Test
Within a sub :
ucTest = new Test(some parameters)

Private Sub ucTest_ChangeNode() Handles Test.ChangeNode
MsgBox("Change node")
End Sub
End Class
 
Not sure exactly what your asking but your not linking the event to anything.

If frmFood should acknowledge the event then you need an event handler. You've got an event handler for

Test.ChangNode

but nothing for

Test.TestEvent
 
Sorry, since all the names I use are dutch, I translated them and didn't check carefully. Should be :


Public Class Test
Inherits System.Windows.Forms.UserControl

Public Event ChangeNode()

Private sub initPage()
RaiseEvent ChangeNode()
End sub

End Class



Public Class frmFood
Inherits frmBase (custom usercontrol)

Private WithEvents ucTest As Test
Within a sub :
ucTest = new Test(some parameters)

Private Sub ucTest_ChangeNode() Handles Test.ChangeNode
MsgBox("Change node")
End Sub

End Class


I enter the line "RaiseEvent ChangeNode()" but when I'm in debug mode it just goes to the next line in the code, without executing the event.
I have some experience using them, since it works in other forms and usercontrols I use.
 
Private Sub ucTest_ChangeNode() Handles ucTest.ChangeNode
MsgBox("Change node")
End Sub

Should be this way.
I guess I could use an Edit-button on fridays...
 
Anyone any idea what could be the mistake? To make it all clear I'll repaste the code :

Public Class Test
Inherits System.Windows.Forms.UserControl

Public Event ChangeNode()

Private sub initPage()
RaiseEvent ChangeNode()
End sub

End Class



Public Class frmFood
Inherits frmBase (custom usercontrol)

Private WithEvents ucTest As Test
Within a sub :
ucTest = new Test(some parameters)

Private Sub ucTest_ChangeNode() Handles ucTest.ChangeNode
MsgBox("Change node")
End Sub


In debug mode I enter the line RaiseEvent ChangeNode() but the code is not executed (I don't get the msgbox). I have similar raiseevents working correctly, so I'm wondering what I'm missing in this case.
 
1) where you say 'within a sub', does this sub execute on load? If the Test class has not been instantiated then you've set the handle but it's not pointing to an instantiated class (ie it's pointing to nothing). The safest way is to declare modular...

private withevents ucTest as NEW Test (instead of private withevents ucTest as Test)

or

=New Test in form load (or in a sub being called by form load)

2) is the initPage sub in the Test class being called? ... in the code you've provided it isn't.
 
It works kinda different.

ucTest = new Test(some parameters)
This is called when someone's clicking on a treeview (and changes the selected node).
When I click a node, another user control is showing on another part of this base-form.

When a certain node is clicked, the selected node has to return to node 1 in some conditions. That condition is evaluated in the called user control.

The initpage is called yes, otherwise I wouldn't enter it while in debug mode.

Maybe I have to add some "real" code :



Public Class frmMaaltijd
Inherits MedLucasNet.frmBase

Private WithEvents ucAnamnese As mlaMaaltijd.Anamnese

Private Sub frmMaaltijd_uTreeMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs, ByVal uNode As UltraTreeNode) Handles MyBase.uTreeMouseUp
Select Case uNode.Tag
Case "Registratie"
ucAnamnese = New mlaMaaltijd.Anamnese(logGebruiker, sCurrentPatient, "R")
End Select
End Sub

Private Sub ucAnamnese_VeranderNode() Handles ucAnamnese.VeranderNode
MsgBox("Verander node")
End Sub

End Class




Public Class Anamnese
Inherits System.Windows.Forms.UserControl

Public Event VeranderNode()

Public Sub New(ByVal _Gebruiker As udtLogGebruiker, ByVal sOpnNr As String, ByVal Type As String)
MyBase.New()
...
InitializeComponent()
...
initAnamneseRegistratie()
End Sub

Private Sub initAnamneseRegistratie()
If dsAnReg.Tables(0).Rows.Count = 0 Then
RaiseEvent VeranderNode()
End if
End Sub

End Class


I didn't change the names now since I want to be sure I don't make any stupid mistakes anymore.
 
Stupid question!!

what's in dsAnReg.Tables(0).Rows.Count at that the time you try to call it.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
A '0' in this case. The actual if-statement is a bit more complicated and before the "if" I call for a sub filling the dataset, but I left that out to make it a little easier to see the problem.
 
Try using Addhandler instead of linking 'handles' to a sub

Code:
ucAnamnese = New mlaMaaltijd.Anamnese(logGebruiker, sCurrentPatient, "R")
addhandler ucAnamnese.VeranderNode, addressof frmMaaltijd_uTreeMouseUp

This will also give an indication of issues with the number of parameters being used. If you're trying to pass parameters then I think you'll need to use delegates.
I always use delegates with or without parameters so I'm not sure exactly how events and parameters function without them.


 
ucAnamnese = New mlaMaaltijd.Anamnese(logGebruiker, sCurrentPatient, "R")
AddHandler ucAnamnese.VeranderNode, AddressOf ucAnamnese_VeranderNode


Private Sub ucAnamnese_VeranderNode()
MsgBox("Verander node")
End Sub



I guess you mean this? I still don't enter the sub "ucAnamnese_VeranderNode()", don't get the msgbox either. I tried it with delegates as well, with the same result.
 
yeah you're right, I just copy-and-pasted the first handles statement I saw.

A couple more things...after that I'm running out of possibilities

1) if you're using addhandler (which I recommend as you're instantiating the class 'dynamically') then remove the 'withevents' when it's initially declared

2)Along with Chrissie1's point, are you absolutely sure the if statement is working properly?
 
The If-statement is working correctly, it's the most important if-statement on this page and deciding which of 2 data-subjects is loaded ( and that part is working correct ;-) ). I can try to add a printscreen if I find how to add an image.

... Can't find it.

With or without addhandler, the code is not executing. If I change the names of the events or insert a parameter, I get errors so I guess my program knows where the link points to.
 
I'm running on fumes now...

is the case statement firing properly?

Code:
Case "Registratie"
ucAnamnese = New mlaMaaltijd.Anamnese(logGebruiker, sCurrentPatient, "R")
AddHandler ucAnamnese.VeranderNode, AddressOf ucAnamnese_VeranderNode

if the class isn't being instantiated here then that would produce the same problems

barring that...with everything you've said, it should be working.
Can you post all the code or does it depend on too many external elements to make it worthwhile?
 
Code:
Public Class frmMaaltijd
    Inherits MedLucasNet.frmBase

Private WithEvents ucHistoriek As mlaMaaltijd.Dieethistoriek
Private WithEvents ucAnamnese As mlaMaaltijd.Anamnese
Private WithEvents ucBevraging As mlaMaaltijd.Bevraging
Private logGebruiker As udtLogGebruiker
Private sCurrentPatient As String

Public Sub New(ByVal _Gebruiker As udtLogGebruiker, ByVal sOpnNr() As String)
MyBase.New(_Gebruiker, sOpnNr, eGetPatients.ByOpnameNummer)

'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call

logGebruiker = _Gebruiker
sCurrentPatient = sOpnNr(0)

beginLaden()
End Sub

Private Sub beginLaden()
Dim bControlCreated As Boolean = False

ucAnamnese = New mlaMaaltijd.Anamnese(logGebruiker, sCurrentPatient, "A")
ucAnamnese.VeranderNode, AddressOf ucAnamnese_VeranderNode

bControlCreated = True

If bControlCreated Then
pnlControl.Controls.Add(ucAnamnese)
ucAnamnese.Location = New Point(0, 0)
 ucAnamnese.Dock = DockStyle.Fill
Try
With uTree.Nodes(0).Nodes(0).Override.NodeAppearance
.ForeColor = Color.Navy
.FontData.Bold = Infragistics.Win.DefaultableBoolean.True
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub


Private Sub frmMaaltijd_uTreeMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs, ByVal uNode As UltraTreeNode) Handles MyBase.uTreeMouseUp
Dim bControlCreated As Boolean = False
Select Case e.Button
Case MouseButtons.Left
Cursor.Current = Cursors.WaitCursor
Try
Try

For Each knop As UltraTreeNode In uTree.Nodes
knop.Selected = False
With knop.Override.NodeAppearance
.FontData.Bold = Infragistics.Win.DefaultableBoolean.False
.ForeColor = Color.Black
End With
For Each knop2 As UltraTreeNode In knop.Nodes
knop2.Selected = False
With knop2.Override.NodeAppearance
.FontData.Bold = Infragistics.Win.DefaultableBoolean.False
.ForeColor = Color.Black
End With
Next
Next

With uNode.Override.NodeAppearance
.FontData.Bold = Infragistics.Win.DefaultableBoolean.True
.ForeColor = Color.Navy
End With

Select Case uNode.Tag
Case "Anamnese"
ucAnamnese = New mlaMaaltijd.Anamnese(logGebruiker, sCurrentPatient, "A") 
pnlControl.Controls.Clear()
pnlControl.Controls.Add(ucAnamnese)
ucAnamnese.Location = New Point(0, 0)
ucAnamnese.Dock = DockStyle.Fill
Case "Registratie"
AddHandler ucAnamnese.VeranderNode, AddressOf ucAnamnese_VeranderNode
ucAnamnese = New mlaMaaltijd.Anamnese(logGebruiker, sCurrentPatient, "R")
pnlControl.Controls.Clear()
pnlControl.Controls.Add(ucAnamnese)
ucAnamnese.Location = New Point(0, 0)
ucAnamnese.Dock = DockStyle.Fill
End Select
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
Cursor.Current = Cursors.Default
End Try

End Select
uTree.ActiveNode = Nothing
End Sub

Private Sub ucAnamnese_VeranderNode() 
MsgBox("Verander node")
End Sub

End class
 
Code:
Public Class Anamnese
    Inherits System.Windows.Forms.UserControl

    Public Event VolgendePatient(ByVal OpnNr As String)
    Public Event VorigePatient(ByVal OpnNr As String)

    Private WithEvents ucDieetopvolging_A As mlaMaaltijd.Dieetopvolging_A
    Private WithEvents ucDieetopvolging As mlaMaaltijd.Dieetopvolging

    Public Event VeranderNode()




    Public Sub New(ByVal _Gebruiker As udtLogGebruiker, ByVal sOpnNr As String, ByVal Type As String)
        MyBase.New()
        Me.Cursor = Cursors.WaitCursor

        loggebruiker = _Gebruiker 'New udtLogGebruiker
        'loggebruiker.Computer = "Informatica104"
        'loggebruiker.GebruikersNaam = "ReinoutW"
        'loggebruiker.Toegang = New DataSet

        pPatient = New Patient(loggebruiker, sOpnNr, eGetPatients.ByOpnameNummer)

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        oDieet = New mlcDieet.Dieet
        'Add any initialization after the InitializeComponent() call
        Me.Cursor = Cursors.Default

        AnReg = Type

        initAnamneseRegistratie()

    End Sub


    Private Sub initAnamneseRegistratie()
        opvullenDSAnReg()

        If dsAnReg.Tables(0).Rows.Count = 0 Then
            aanmakenAnamneseRegistratie()
            AnReg = "A"
            MsgBox("Eerste registratie / anamnese.")
            RaiseEvent VeranderNode()
            'initAnamneseRegistratie()
            opvullenDSAnReg()
            initGegevensPatient()
            EersteRegistratie = True
        ElseIf dtvAnamnese(0).Item("Status") = 1 And AnReg <> "A" Then
            AnReg = "A"
            MsgBox("Eerste registratie / anamnese.")
            RaiseEvent VeranderNode()
            initGegevensPatient()
            EersteRegistratie = True
        ElseIf dtvAnamnese(0).Item("Status") = 0 Then
            AnReg = "A"
            MsgBox("Eerste registratie / anamnese.")
            RaiseEvent VeranderNode()
            initGegevensPatient()
            EersteRegistratie = True
        Else
            initGegevensPatient()
            EersteRegistratie = False
        End If
    End Sub


End class

This class has over 5000 lines, I think it wouldn't be too good an idea to paste them...
 
two things...
1)
Code:
Case "Registratie"
AddHandler ucAnamnese.VeranderNode, AddressOf ucAnamnese_VeranderNode
ucAnamnese = New mlaMaaltijd.Anamnese(logGebruiker, sCurrentPatient, "R")

the addhandler must go after the instantiation

2)
Code:
ucAnamnese = New mlaMaaltijd.Anamnese(logGebruiker, sCurrentPatient, "A")
ucAnamnese.VeranderNode, AddressOf ucAnamnese_VeranderNode
there is no 'AddHandler'
 
Sorry, something must have gone wrong while deleting the comment-lines :) I put the AddHandler before the declaration for testing, as the raiseEvent should have been executed when the line "ucAnamnese = New mlaMaaltijd.Anamnese(logGebruiker, sCurrentPatient, "R")" was executed, when I put the addhandler afterwards, I don't have the event declared, right?
 
the problem is that you're setting a handler to nothing (or to the old instance of the class) when you set it before the next instantiation.

I've never come up against a situation where you need to handle an event that is kicked of by the constructor...someone else will have to jump in.

Personally I would look at kicking off the initialization after the handlers have been set...

dim newclass as new class
set handlers for newclass
newclass.myinitmethod

...but with your code in mind there's probably a better way
 
That's it jubble !

It works like you say it, you have to create a new instance of the class, afterwards declare addhandlers and then initialize the class.

So :
Private WithEvents ucAnamnese As mlaMaaltijd.Anamnese

Private Sub frmMaaltijd_uTreeMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs, ByVal uNode As UltraTreeNode) Handles MyBase.uTreeMouseUp
Select Case uNode.Tag
Case "Registratie"
ucAnamnese = New mlaMaaltijd.Anamnese(logGebruiker, sCurrentPatient, "R")
AddHandler ucAnamnese.VeranderNode, AddressOf ucAnamnese_VeranderNode
ucAnamnese.initAnamneseRegistratie()
End Select
End Sub

Private Sub ucAnamnese_VeranderNode()
msgbox("now")
End Sub


Public Class Anamnese
Inherits System.Windows.Forms.UserControl
Public Sub New(ByVal _Gebruiker As udtLogGebruiker, ByVal sOpnNr As String, ByVal Type As String)
MyBase.New()
loggebruiker = _Gebruiker
pPatient = New Patient(loggebruiker, sOpnNr, eGetPatients.ByOpnameNummer)
InitializeComponent()
End Sub

Public Sub initAnamneseRegistratie()
opvullenDSAnReg()
If ...
RaiseEvent VeranderNode()
initGegevensPatient()
End if
End Sub
End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top