Invoke event of class from another class

false74

Well-known member
Joined
Aug 4, 2010
Messages
76
Programming Experience
Beginner
I am new to making events and I have read up on them to further my understanding of them, and I have come across a situation that I am stuck at. Currently I have a handful of classes (lets call them a childform) that are all inherited from a class that's a form (lets call it a parentform) with an event called UpdateData(). When these forms are created they are added into a hashtable of ParentForms then added into another form. I call on these forms through a method that returns the form from the hastable.
VB.NET:
Public Class ParentForm
    Inherits Form
    Public Event UpdateData()
    Public Sub UpdateLoad() Handles Me.Load 'update on load automatically
        RaiseEvent UpdateData()
    End Sub
VB.NET:
Public Class ChildFormA
    Inherits ParentForm
    'add textboxes, buttons etc here
    Private Sub UpdateFormComponents() Handles Me.UpdateData
        'get data from elsewhere and update components
    End Sub

Now my issue is, I have a separate (lets say its a settings dialog box) form. I want to be able to raise the UpdateData event of the a ChildForm. I want to be able to do something like:
ModulehashTable(1).UpdateData
Where modulehastable is where all the forms are stored at and will return the form with the key of 1, allowing me to call the updatedata event.
I cannot just call UpdateFormComponents() method because not all the forms have the same method names that handles that, or could have multiple methods that handle the updatedata event.

Hope this makes sense :eek:
 
The most appropriate way to define and raise and event is like so:
Public Class Form1

    Public Event SomeEvent As EventHandler

    Protected Overridable Sub OnSomeEvent(e As EventArgs)
        RaiseEvent SomeEvent(Me, e)
    End Sub

    Private Sub DoSomething()
        'Do something here.

        'Raise the event.
        OnSomeEvent(New EventArgs)
    End Sub

End Class
The event should always be declared as type EventArgs or EventArgs(Of T). For more information on this pattern, follow the Blog link in my signature and check out my post on Custom Events.

The event is only ever raise from that one method dedicated to the purpose. Whenever you want to raise the event, you call that method. If you want to be able to raise the event from outside the object, you add a public method that will then call that method. An example of this is the Button.PerformClick method. It is public and you can call from anywhere that you can access the Button. Internally, it calls the Button's OnClick method, whose sole purpose is to raise the Click event. That would look something like this:
Public Class Form1

    Public Event SomeEvent As EventHandler

    Protected Overridable Sub OnSomeEvent(e As EventArgs)
        RaiseEvent SomeEvent(Me, e)
    End Sub

    Private Sub DoSomething()
        'Do something here.

        'Raise the event.
        OnSomeEvent(New EventArgs)
    End Sub

    Public Sub RaiseSomeEvent()
        OnSomeEvent(New EventArgs)
    End Sub

End Class
The code you posted looks odd though. Why exactly is the child form handling its own event that is inherited from the parent form?
 
I posted a a very simplified example of my code just to demo what my issue was. To summarize the way my application works is:
at run time the user selects from a list of choices. each choice requires different forms to run, but some of these forms are used on multiple choices.
Each choice is it's own class which is all inherited from a base class that has variables that all the choices have in common.
In the choice's class it creates the forms and adds them into a hashtable/dictionary with a set enumerator as their key. with the enumeration being public and shared, each form can be called from anywhere.
once all the forms are added into the hashtable another form is opened and each form in the hashtable is added into that mainform as a mdi form.
the forms from added in the choice class are linked back to the choice class, so it can call upon the methods and properties from it. since one form can change a variable in the choice class, another form would need to be updated to show the change, if needed be.
 
Back
Top