Click here to Skip to main content
15,886,639 members
Articles / Programming Languages / Visual Basic

Global events for your application

Rate me:
Please Sign up or sign in to vote.
2.73/5 (9 votes)
22 Aug 2008CPOL1 min read 34.3K   519   26   2
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.

VB
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.

VB
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.

VB
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.

Image 1

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)


Written By
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.


Comments and Discussions

 
QuestionMy vote of 5 Pin
Member 1098919421-Sep-14 10:21
Member 1098919421-Sep-14 10:21 
GeneralMy vote of 5 Pin
SD SteveG22-Jan-12 5:43
SD SteveG22-Jan-12 5: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.