Solved

VB.NET Custom RoutedEvent

Posted on 2014-01-31
9
869 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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