Events from MDIchildren from Parent

Naamah

Member
Joined
Mar 1, 2008
Messages
7
Location
Italy
Programming Experience
1-3
Hi guys,
in my application i wish to send an event from a child form to parent form and handling event from this parent.
How can i made this?
I can able to raise an event inside the same form, but i can't do this across multiple form.
Please, anyone can post a sample code of child and parent.
Thank you!
 
To handle an event of a class you must either declare a variable WithEvents or use the AddHandler statement.
 
Ok... but you can post an example code? :D
... sorry, i'm a newbie for the VB.NET, i have just programmed only in LabVIEW e ASP.NET. :eek:
 
I have found this code below...

VB.NET:
Sub TestEvents()
   Dim Obj As New Class1()
   ' Associate an event handler with an event.
   AddHandler Obj.Ev_Event, AddressOf EventHandler
   Obj.CauseSomeEvent()   ' Ask the object to raise an event.
End Sub

Sub EventHandler()
   ' This procedure handles events raised by the object Obj.
   MsgBox("EventHandler caught event.")   ' Handle the event.
End Sub

Public Class Class1
   Public Event Ev_Event()   ' Declare an event.
   Sub CauseSomeEvent()
      RaiseEvent Ev_Event()   ' Raise an event.
   End Sub
End Class

In my application the "Sub TestEvents()" must be are in children form and the "Sub EventHandler" must be are in the parent form... the declaration of Class... uhm...
If I place this piece of code in a single form it's all ok, but in my application the event is generated in children form e handled in parent form.
How i can made this?
 
A form is a class, simple as that. If a form or an object within reach have an event you can subscribe to it.
VB.NET:
dim f as new yourformclass
addhandler f.theevent, addressof yourhandlermethod
 
OK... for a test, in a module i have placed this code...

VB.NET:
    Sub TestEvents()
        Dim Obj As New Ricerca()
        ' Associate an event handler with an event.
        AddHandler Obj.Ev_Event, AddressOf EventHandler
        Obj.CauseSomeEvent()   ' Ask the object to raise an event.
    End Sub

    Sub EventHandler()
        ' This procedure handles events raised by the object Obj.
        MsgBox("EventHandler caught event.")   ' Handle the event.
    End Sub

...Ricerca() is the form that raise the event and i have placed this code...

VB.NET:
    Public Event Ev_Event()   ' Declare an event.
    Sub CauseSomeEvent()
        RaiseEvent Ev_Event()   ' Raise an event.
    End Sub

... i have tryed to insert your suggestion in the form that handle the event but i can't place you code, because "AddHandler" is non recognized by VS2008 (Syntax Error). WHY??? :confused:

VB.NET:
    Dim f As New Ricerca
    AddHandler f.Ev_Event, AddressOf yourhandlermethod
 
Ok... i have write the code below, and no error are generated, but... no event are raised! why?

This is the "Parent Form" that handle the event.
VB.NET:
Imports System.Windows.Forms

Public Class MDIParent1

  
    Private Sub MDIParent1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (System.IO.File.Exists(db_folder & "\Magazzino.mdb")) Then
            MessageBox.Show("Nella cartella predefinita (" & db_folder & ") non è stato drovato l'archivio.", "Errore!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        Dim f As New Ricerca()
        AddHandler f.Ev_Event, AddressOf StatusBarWrite
        UltraStatusBar1.Panels.Item(0).Text = "Sistema pronto."
    End Sub

    Private Sub StatusBarWrite()
        UltraStatusBar1.Panels.Item(0).Text = LastAction 'LastAction is a Public variable declared in a module
    End Sub

    Private Sub UltraListBar1_ItemSelected(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinListBar.ItemEventArgs) Handles UltraListBar1.ItemSelected
        Dim MDIchildren
        Select Case UltraListBar1.SelectedGroup.Index
            Case 0
                Select Case e.Item.Index
                    Case 0
                        MDIchildren = New Situazione()
                    Case 1
                        MDIchildren = New Ricerca()
                End Select
        End Select
        Try
            MDIchildren.MdiParent = Me
            MDIchildren.Show()
        Catch e1 As Exception
            MessageBox.Show(e1.ToString, "Errore!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

End Class

...and this is the children form that raise the event...

VB.NET:
Public Class Ricerca

    Public Event Ev_Event()
    Dim ArticoloTrovato As New db_record 'Public class declared in a module
    Dim FirstCycle As Boolean = True

    Private Sub UltraCombo1_Textchanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles UltraCombo1.TextChanged
        If Not (FirstCycle) Then
            Dim StringOutput As String
            Select Case UltraOptionSet1.CheckedIndex.ToString
                Case 0
                    RaiseEvent Ev_Event() 'Send the event
                    ArticoloTrovato = SearchCode(UltraCombo1.Text)
                Case 1
                    LastAction = "Scaricato " & ArticoloTrovato.Descrizione & " dal magazzino."
                Case 2
                    LastAction = "Caricato " & ArticoloTrovato.Descrizione & " nel magazzino."
            End Select
            If ArticoloTrovato.Codice = 0 Then
                StringOutput = "Articolo non presente in magazzino!"
                UltraButton1.Enabled = False
                UltraButton2.Enabled = False
            Else
                If ArticoloTrovato.Quantità = 0 Then
                    StringOutput = "Articolo terminato!"
                    If UltraOptionSet1.CheckedIndex = 1 Then UltraOptionSet1.CheckedIndex = 0
                    UltraButton1.Enabled = True
                    UltraButton2.Enabled = False
                Else
                    LastRecord = ArticoloTrovato
                    UltraButton1.Enabled = True
                    UltraButton2.Enabled = True
                    UltraTextEditor1.Text = UltraCombo1.Text
                    StringOutput = ArticoloTrovato.Descrizione & "." & vbCrLf
                    Select Case ArticoloTrovato.Quantità
                        Case 0
                            StringOutput = StringOutput & "Nessun pezzo presente in magazzino."
                        Case 1
                            StringOutput = StringOutput & "1 solo pezzo presente in magazzino."
                        Case Else
                            StringOutput = StringOutput & ArticoloTrovato.Quantità & " pezzi presenti in magazzino."
                    End Select
                    If Not (ArticoloTrovato.Rivendita = 0) Then StringOutput = StringOutput & vbCrLf & "Prezzo di rivendita €" & ArticoloTrovato.Rivendita & "."
                End If
            End If
            UltraLabel1.Text = StringOutput
        Else
            FirstCycle = False
        End If
        UltraTextEditor1.Focus()
    End Sub
...
...
...
End Class

Where i wrong?
Thanks for any suggestion!
 
If Ev_Event event is raised from the f instance you should catch it by that.
 
Your problem lies elsewhere because you always get events that you subscribe to when they are raised, that is how the event mechanism work, there is no magic involved whatsoever. Examine this code example and see if there is any detail about it you don't understand:
VB.NET:
Sub test()
    Dim x As New y
    AddHandler x.evt, AddressOf evthandler
    x.raise()
End Sub

Sub evthandler(ByVal i As Integer)
    MsgBox("hello event")
End Sub

Class y
    Public Event evt(ByVal i As Integer)
    Public Sub raise()
        RaiseEvent evt(0)
    End Sub
End Class
You may also want to run the code and see that the AddHandler statement in fact works as it's supposed to.

When confident about how events work you can start debugging your own scenarios. Is the event raised from that class when you think it should? Use a breakpoint to confirm this. Are you confused about multiple instances of a class? For example I see you create a new instance of the Ricerca class which is a form, but never called Show, you do however set the Text property of something, is this what causes the event to be raised? In the event handler you set this same Text property of something, is that really smart?
 
... sorry for this incombence JohnH, but the problem remain, in fact Event handling are difficult to me.

Premit that all the event marked with the breakpoints that i have tryed are reached without a problem.

I have tryed with your suggestions, and the code in the main form are...
VB.NET:
Public Class MDIParent1

    Sub evthandler(ByVal i As Integer)
        MsgBox("hello event")
    End Sub

    Private Sub MDIParent1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim x As New Ricerca
        AddHandler x.evt, AddressOf evthandler
        'x.raise()
    End Sub

...
...
...

... then in the children form that raise the event the code are...

VB.NET:
Public Class Ricerca

    Public Event evt(ByVal i As Integer)
    Public Sub raise()
        RaiseEvent evt(0)
    End Sub
    Public Event EventSB(ByVal S As String)

    Private Sub Ricerca_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        raise()
    End Sub
...
...
...

The problem is...
If I uncomment the "x.raise()" call in the main form the event is generated and handled correctly. But if I raise the event by call the same sub (i mean "raise()") from the children from the event is generated but not handled by the main form. Why???
I suppose that if call "x.raise()" from the main form the eventi is generated too from the children form. It's ok? In fact the sub "raise()" is a part of class of the children form.
So, why this does'nt work?

This drive me crazy!
:confused::mad::eek::cool:
 
What I see here is that you create a new instance of the Ricerca form class. In the Load event of this class you call RaiseEvent, but you never do anything that cause the Load event of the form to happen. You have also failed to follow the advice to use a breakpoint on this code line, because that would have told you quite clearly that the Load event never happened and consequently that your RaiseEvent call also was never called. If you are uncertain what causes the Load event of the form to happen you open help index and type "load event" and select the "form.load event" topic, here is the general description:
Occurs before a form is displayed for the first time.
You might argue that you are sitting there waiting "before a form is displayed" right now, but the emphasis is here on the action "displayed", so unless you call Show/ShowDialog consider the form NOT to be about to be displayed in this regard.
 
Back
Top