Solved

Adding an event handler

Posted on 2006-06-12
5
275 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now