?
Solved

Adding an event handler

Posted on 2006-06-12
5
Medium Priority
?
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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 86

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

743 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