Solved

Adding an event handler

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I need help embedding an image as HTML in my vb.net application 3 68
C# Single Form 8 42
VB.Net - TypeInitializer Error 25 27
vb.net help 4 30
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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