Solved

Adding an event handler

Posted on 2006-06-12
5
281 Views
Last Modified: 2010-04-23
Hi

I have a form and a class.

The class has a event that i want to monitor.

The form displays information based on events from this class.

Hence i want the form to loaded before the class is so I can monitor all the events and dont miss any on startup.

My question is can I add an event handler before the class is instantiated.

Such as

Private WithEvents myClass as SomeClass

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      AddHandler myClass.SomeEvent, addressOf SomeFunction

      myClass = new SomeClass

End Sub

Are there any associated problems that might occur with this?
0
Comment
Question by:adam_pedley
  • 3
  • 2
5 Comments
 
LVL 4

Author Comment

by:adam_pedley
ID: 16891341
haha

well now that im a bit less lazy and tried it myself you need to instantiate it first.

So now, whats the proper way to solve this issue.

A redesign?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 16891488
Don't use AddHandler() and WithEvents together...

When using WithEvents you flag a sub to trap an Event using the Handles keyword:
(this can be done using the dropdown menus across the top of the IDE)

Public Class SomeClass
    Public Event SomeEvent()
    Public Event AnotherEvent(ByVal param1 As Integer)
End Class

Public Class Form1

    Private WithEvents sc As SomeClass

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        sc = New SomeClass
    End Sub

    Private Sub sc_SomeEvent() Handles sc.SomeEvent

    End Sub

    Private Sub sc_AnotherEvent(ByVal param1 As Integer) Handles sc.AnotherEvent

    End Sub

End Class
0
 
LVL 4

Author Comment

by:adam_pedley
ID: 16891496
so with this approach I wont miss any events fired by the class?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16891505
Nope...it is already "wired" up via the "Handles" clauses at the end of the respective subs.

This is the same way that your normal GUI components are wired up for your form via the IDE form designer.
0
 
LVL 4

Author Comment

by:adam_pedley
ID: 16891510
thanks again :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question