Solved

VB.NET Custom RoutedEvent

Posted on 2014-01-31
9
879 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
[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
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

759 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