Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.NET Custom RoutedEvent

Posted on 2014-01-31
9
Medium Priority
?
940 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

971 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