Solved

Adding an event handler

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

13 Experts available now in Live!

Get 1:1 Help Now