Click here to Skip to main content
13,353,462 members (62,483 online)
Click here to Skip to main content
Add your own
alternative version

Stats

22.6K views
436 downloads
26 bookmarked
Posted 22 Aug 2008

Global events for your application

Rate this:
Please Sign up or sign in to vote.
How to use events through your application for multiple receivers.

Introduction

Sometimes it is useful that any form in an application can receive an event of another task so that it can handle some specific tasks. For example, you have a database that contains your lookup values that are used by your combo boxes. Now, if you update this lookups, it would be nice that all open forms would update their combo boxes, too. But how do we handle this?

Background

As I searched for a solution for this problem, a Xing buddy named Sascha Kiefer gave me the hint of using some delegates (using C#...). But using delegates in VB.NETis not necessary, and after some more research, I had the solution with just a few lines of code.

Using the code

First of all, you need a class that contains your event and a procedure to raise the event.

Public Class GlobalEventing
    Public Shared Event SpecialEventRaised(ByVal sender As Object, _
           ByVal type As String, ByVal msg As String)

    Public Shared Sub SpecialEvent(ByVal sender As Object, _
           ByVal type As String, ByVal msg As String)
        RaiseEvent SpecialEventRaised(sender, type, msg)
    End Sub
End Class

Now, the easiest part is to raise the event ... just call the proc.

GlobalEventing.SpecialEvent(me, "my typ", "my message")

In your forms, you just add an object reference, an addhandler statement, and a addressof procedure to handle the event.

Public Class ChildForm
    Inherits System.Windows.Forms.Form
  Dim gEvent As GlobalEventing
  (...)

  Private Sub ChildForm_Load(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load
      AddHandler gEvent.SpecialEventRaised, AddressOf SpecialEventFired
  End Sub

  Private Sub SpecialEventFired(ByVal sender As Object, _
              ByVal typ As String, ByVal msg As String)
    txtOutput.AppendText(Now().ToLocalTime.ToString & " received " & _
                         typ & " / " & msg & vbCrLf)
  End Sub
End Class

Example

In the example, you will find a small solution (in VS2003) with an MDI form. Each of the child forms can receive the event - just take a look.

Personal

Once more, I saw that often, complex questions can result in very easy answers - but you have to find them. Hope you can make use of this little bit of code... enjoy! Any remarks are welcome!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Daniel Ch. Bloch (MCSD, MCAD, MCTS)
Software Developer (Senior) http://www.btv-data.ch
Switzerland Switzerland
On software development since 1992, going through the different steps from mainframe computing with VMS/Vax over VB4-6, Access and SQL server, Cold Fusions and ASP programming to my currently status as Chief Solution Developer for Smartclient and Client/Server development with vb.net and SQL Server. MCSD and some other stuff.


You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionMy vote of 5 Pin
Member 1098919421-Sep-14 11:21
memberMember 1098919421-Sep-14 11:21 
GeneralMy vote of 5 Pin
SD SteveG22-Jan-12 6:43
memberSD SteveG22-Jan-12 6:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 22 Aug 2008
Article Copyright 2008 by Daniel Ch. Bloch (MCSD, MCAD, MCTS)
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid