Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating custom events with custom EventArgs in vb.net?

Posted on 2009-05-05
4
Medium Priority
?
1,470 Views
Last Modified: 2012-05-06
I have an application where I would like to create an event system that will pass a custom event argument to a series of form controls.

Essentially, this is meant to be a rudimentary access control mechanism. The custom argument will contain an object that will indicate a requested access level and each control will then take actions based on the values in the passed event argument.

I was looking into defining a custom event (see attached snippet), but I am not quite sure how to wire all this up.

A couple of questions:

1) Is this do-able? Can an entire class be passed as an event argument?
2) Will a disabled control still be able to receive a custom event message?
3) Can all my controls respond to the same custom event at the same time?
4)  How do I wire all this up?

I was thinking about deriving control classes from each of the UI control types I will be using so that I can define the custom event handler for each UI control from within the derived control class. I could add the handler when I create an instance of the control and remove it on disposal. Does that make sense?

Public Enum AccessLevels As Integer
    Guest    ' Read Only access (and non-SSN reports)
    Staff  ' Team Managers & Staff
    Physician    ' Area Managers
    Administrator         ' Administrator
End Enum
 
Public Class AccessToken
    Private _AccessLevel As AccessLevels
    Public Property AccessLevel() As AccessLevels
        Get
            Return _AccessLevel
        End Get
        Set(ByVal value As AccessLevels)
            _AccessLevel = value
        End Set
    End Property
 
    Public Sub New(ByVal Level As AccessLevels)
        AccessLevel = Level
    End Sub
End Class
 
Public Class MyEventArg
        Inherits EventArgs
        Private _Token As AccessToken
        Public Property Token() As AccessToken
            Get
                Return _Token
            End Get
            Set(ByVal value As AccessToken)
                _Token = value
            End Set
        End Property
 
    End Class
 
Public Custom Event AccessLevelEvent As EventHandler
        AddHandler(ByVal d As EventHandler)
        End AddHandler
        RemoveHandler(ByVal d As EventHandler)
        End RemoveHandler
        RaiseEvent(ByVal sender As Object, ByVal e As MyEventArg)
        End RaiseEvent
    End Event

Open in new window

0
Comment
Question by:kkamm
[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
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1500 total points
ID: 24305837
Let's answer those questions...

#1
It makes perfect sense and is completely "do-able".  =)
You can pass ANYTHING in a custom event...and pass more than one parameter if desired (it is, after all, a "custom" event!).

#2
A disabled control will still receive custom messages...it just typically won't receive USER input from the mouse/keyboard.

#3
Yes..ALL subscribers to an event will be notified when it is fired...you just can't predict the ORDER in which they will be notified.

#4
I think you already nailed it...use AddHandler() when you create the controls.

A simple example of a custom event passing non-standard parameters:
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sc As New SomeClass
        AddHandler sc.SomeEvent, AddressOf sc_SomeEvent
        sc.SomeSub()
    End Sub
 
    Private Sub sc_SomeEvent(ByVal Priority As Integer, ByVal data As SomeData)
        Dim msg As String = String.Join(" ", New String() {data.a, data.b, data.c})
        MessageBox.Show(msg, Priority.ToString)
    End Sub
 
End Class
 
Public Class SomeClass
 
    Public Event SomeEvent(ByVal Priority As Integer, ByVal data As SomeData)
 
    Public Sub SomeSub()
        RaiseEvent SomeEvent(411, New SomeData("Idle_Mind", "was", "here"))
    End Sub
 
End Class
 
Public Class SomeData
 
    Public a, b, c As String
 
    Public Sub New(ByVal a As String, ByVal b As String, ByVal c As String)
        Me.a = a
        Me.b = b
        Me.c = c
    End Sub
 
End Class

Open in new window

0
 
LVL 1

Author Comment

by:kkamm
ID: 24309057
Thanks for the input, Idle Mind.

Have you dealt with this in a WPF context by any chance?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24309161
I have not played with WPF at all unfortunately...   =\
0
 
LVL 1

Author Comment

by:kkamm
ID: 24317947
This will ultimately be used in a WPF context, so I have to do this via routed events, but I don't think the basic functionality should be much different.

At any rate, the question was essentially answered for me. Thanks for the assist.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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