Solved

VB.NET Custom RoutedEvent

Posted on 2014-01-31
9
849 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do ASP.NET and MVC work together? 4 30
Problem to open text file 11 74
Syntax error 9 45
.NET tools for adding thread safety to a web app? 3 19
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

862 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

24 Experts available now in Live!

Get 1:1 Help Now