Solved

VB.NET Custom RoutedEvent

Posted on 2014-01-31
9
841 Views
Last Modified: 2014-02-24
I have a hierarchical structure of UserControls that I need to have a custom RoutedEvent to work with. What I need is to be able to have a parameter bubble through the hierarchy of controls with certain controls modifying the parameter so that when the event bubbles up to the base of the hierarchy I have a URI-like parameter that describes the path it took.

So far I have created a class derived from RoutedEventArgs that adds a String parameter to the event argument.

Public Class ShowAsEventArgs
        Inherits RoutedEventArgs
        '
        Public Sub New(ByVal evt As RoutedEvent, ByVal myName As String)
            MyBase.New(evt)
            mFieldPath = myName
        End Sub
        '
        Private mFieldPath As String
        '
        Public Property FieldPath As String
            Get
                Return mFieldPath
            End Get
            Set(value As String)
                mFieldPath = value
            End Set
        End Property
    End Class

Open in new window


I have also defined the routed event:

    Public Shared ReadOnly ShowAsFieldEvent As RoutedEvent = EventManager.RegisterRoutedEvent("ShowAsField", RoutingStrategy.Bubble, GetType(RoutedEventHandler), GetType(BitField))
    '
    Public Custom Event ShowAsField As RoutedEventHandler
        AddHandler(value As RoutedEventHandler)
            Me.AddHandler(ShowAsFieldEvent, value)
        End AddHandler

        RemoveHandler(value As RoutedEventHandler)
            Me.RemoveHandler(ShowAsFieldEvent, value)
        End RemoveHandler

        RaiseEvent(sender As Object, e As BitField.ShowAsEventArgs)
            Me.RaiseEvent(e)
        End RaiseEvent
    End Event

Open in new window


The problem I am having is adding a handler for this event, I tried this:

 AddHandler BitField.ShowAsFieldEvent, AddressOf HandleShowAsFieldEvent

Open in new window


However, I get an error that says that "ShowAsFieldEvent" is not an event of "BitField".

I spite of lots of searches I am unable to find an example of how to do this. Any input would be much appreciated.
Sid.
0
Comment
Question by:Sid Price
  • 5
  • 3
9 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39833013
Is this a WPF application?
0
 

Author Comment

by:Sid Price
ID: 39833105
Yes it is WPF question,
Sid.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39835711
You don't seem to register this event with EventManager anywhere as shown in example on following page

http://wpftutorial.net/RoutedEvents.html
0
 

Author Comment

by:Sid Price
ID: 39835948
Yes I do, it is this line in the code I posed:

 Public Shared ReadOnly ShowAsFieldEvent As RoutedEvent = EventManager.RegisterRoutedEvent("ShowAsField", RoutingStrategy.Bubble, GetType(RoutedEventHandler), GetType(BitField))

Open in new window


Sid
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39836285
Ah yes you have. The only other difference I see is that you have put RaiseEvent line within the event declaration.
0
 

Author Comment

by:Sid Price
ID: 39844549
Once again this does not relate to the question I asked and the details I gave. Once more:

If I add a handler for the an instance of my UserControl the event is raised and my handler runs. The issue is that I need to be able to add a handler when I do not have an instance of the UserControl, it was my understanding that because the definition of the event is shared I can do this. As I commented I get an error saying it is not a member of the class.
Sid.
0
 

Accepted Solution

by:
Sid Price earned 0 total points
ID: 39871350
I have found the solution, VB has TWO AddHandler methods and the one used in my code was NOT for RoutedEvents. To select the correct "AddHandler" method it must be prefixed like this:

Me.AddHandler BitField.ShowAsFieldEvent, AddressOf HandleShowAsFieldEvent

Open in new window


Sid.
0
 

Author Closing Comment

by:Sid Price
ID: 39882037
I resolved this issue by further research without help from any of the answers given.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now