Windows Vista Forums

WPF in VB / Routed Events help needed

  1. #1


    RobinS Guest

    WPF in VB / Routed Events help needed


    I'm trying to learn WPF and do it in VB instead of C#. (God forbid I should
    do *anything* the easy way. ;-)

    Here's something weird. On p162-3 of this book by Petzold (in C# of course)
    in an example about using Routed Events, he has a loop that assigns an
    eventhandler to the PreviewKeyUp, etc., events, like this:

    //these are elements defined in code above this point
    UIElement[] els = { win, grid, btn, text };
    foreach (UIElement el in els)
    {
    el.PreviewKeyDown += AllPurposeEventHandler;
    el.PreviewKeyUp += AllPurposeEventHandler;
    el.KeyDown += AllPurposeEventHandler;
    el.KeyUp += AllPurposeEventHandler;
    }

    When I hover over el.PreviewKeyUp in the C# program, it says
    "KeyEventHandler UIElement.PreviewKeyUp", which is right.

    When I hover over el, I get "(local variable) UIElement el".



    These are just event handlers, right?

    Here's the AllPurposeEventHandler signature:

    void AllPurposeEventHandler(object sender, RoutedEventArgs args)
    {
    // do some stuff
    }

    This works fine. So here's my corresponding VB code:

    'I used m_grid instead of grid, because VB is not case-sensitive,
    ' and grid is a control in WPF.
    Dim els() as UIElement = {win, m_grid, btn, text}
    For Each el As UIElement In els
    AddHandler el.PreviewKeyDown, AddressOf AllPurposeEventHandler
    AddHandler el.PreviewKeyUp, AddressOf AllPurposeEventHandler
    AddHandler el.KeyDown, AddressOf AllPurposeEventHandler
    AddHandler el.KeyUp, AddressOf AllPurposeEventHandler
    Next el

    Here's this routine...

    Private Sub AllPurposeEventHandler(ByVal sender As Object,
    ByVal e As RoutedEventArgs)
    'do some stuff (snipped for length
    End Sub

    When I hover over el.PreviewKeyUp, it says

    Public Event PreviewKeyDown(sender as Object,
    e as System.Windows.Input.KeyEventArgs)

    When I hover over el, I get "Dim el as System.Windows.UIElement".

    And it won't take AllPurposeEventHandler as a delegate because the args are
    the wrong type, not RoutedEventArgs.

    Why the heck would it do that? Do you see any obvious typos? Is it
    precompiling differently than C#? Am I misunderstanding the C# code?

    I tried casting el to a UIElement (although it already is one) and then
    checking the PreviewKeyDown, and it does the same thing. I also tried
    taking it out of the loop and using the actual controls (win, m_grid, btn,
    text), but I get the same result.

    I have the same namespaces imported in VB as I do in C#.

    Any ideas?

    Thanks,
    Robin S.
    Ts'i mahnu uterna ot twan ot geifur hingts uto.



      My System SpecsSystem Spec

  2. #2


    RobinS Guest

    Re: WPF in VB / Routed Events help needed

    I tried these variations on a theme:

    AddHandler CType(el.PreviewKeyDown, RoutedEvent), ...
    AddHandler CType(el.PreviewKeyDown, RoutedEventHandler), ...
    AddHandler CType(el.PreviewKeyDown, UIElement.PreviewKeyDown), ...

    Intellisense error on all is: 'AddHandler' or 'RemoveHandler' statement
    event operand must be a dot-qualified expression or a simple name.

    But you got me thinking, so I tried this:

    AddHandler DirectCast(el, UIELement).PreviewKeyDown, ...

    Darn it; I got the same error as before -- Method ... does not have the
    same signature as delegate. It just won't recognize the PreviewKeyDown as a
    RoutedEvent on the UIElement type. Grrrrrrr.

    Robin S.
    --------------------------------------
    "ClayB" <clayb@syncfusion.com> wrote in message
    news:1170678537.557395.70120@k78g2000cwa.googlegroups.com...
    > Just something to try, but I am not sure if VB will take the syntax.
    > Try casting the event handler in the AddHandler statement. Maybe
    > something like:
    >
    > AddHandler CType(el.PreviewKeyDown, RoutedEventHandler),
    > AddressOf AllPurposeEventHandler
    >
    > ==============
    > Clay Burch
    > Syncfusion, Inc.
    >




      My System SpecsSystem Spec

  3. #3


    Patrice Guest

    Re: WPF in VB / Routed Events help needed

    I'm surprising that this C# code may work cause your
    'AllPurposeEventHandler' sub is definitely not of the excpected type.
    By setting type KeyEventArgs instead of RoutedEventArgs for e param
    you can compile according without any problem to the error message.

    But maybe I didn't understand what you want...


      My System SpecsSystem Spec

  4. #4


    RobinS Guest

    Re: WPF in VB / Routed Events help needed


    Okay, here's the deal. I contacted the WPF group, and they are going to
    escalate this because it seems like it's not working right.

    In the meantime, with the help of a friend, I have a solution.

    The problem is that VB won't recognize the keyboard and mouse events of the
    UIElement as Routed Events like it should.

    The way to get around this problem is to use a relaxed delegate instead of
    specifically binding the element to the handler.

    Relaxed delegates allow you to create an event handler with all parameter
    types as object and still be able to bind them to an event. Unfortunately,
    they are only available in C#.

    But you make a workaround. So instead of this, which doesn't work because
    VB won't recognize PreviewKeyDown as a RoutedEvent of UIElement el

    AddHandler el.PreviewKeyDown, AddressOf AllPurposeEventHandler
    AddHandler el.KeyDown, AddressOf AllPurposeEventHandler
    (etc.)

    You can use this, which is more verbose, but does work:

    Dim eventInfo As EventInfo
    Dim methodInfo as MethodInfo

    'I only need to do this once, because they are all going to call
    ' the same event.
    methodInfo = Me.GetType().GetMethod("AllPurposeEventHandler")

    eventInfo = el.GetType().GetEvent("PreviewMouseDown")
    Dim dlg as [Delegate] = [Delegate].CreateDelegate( _
    eventInfo.EventHandlerType, Me, methodInfo)
    eventInfo.AddEventHandler(el, dlg)

    Dim dlg2 As [Delegate] = [Delegate].CreateDelegate( _
    eventInfo.EventHandlerType, Me, methodInfo)
    eventInfo = el.GetType().GetEvent("MouseDown")
    eventInfo.AddEventHandler(el, dlg2)

    (and so on, for each event)

    You have to import System.Reflection at the top of your module.

    Hope this helps someone else with their WPF Routed Events in VB.

    Robin S.
    Ts'i mahnu uterna ot twan ot geifur hingts uto.
    -----------------------------------------------
    "RobinS" <RobinS@NoSpam.yah.none> wrote in message
    news:8dOdndWnUqHqSlvYnZ2dnUVZ_silnZ2d@comcast.com...
    >
    > I'm trying to learn WPF and do it in VB instead of C#. (God forbid I
    > should do *anything* the easy way. ;-)
    >
    > Here's something weird. On p162-3 of this book by Petzold (in C# of
    > course) in an example about using Routed Events, he has a loop that
    > assigns an eventhandler to the PreviewKeyUp, etc., events, like this:
    >
    > //these are elements defined in code above this point
    > UIElement[] els = { win, grid, btn, text };
    > foreach (UIElement el in els)
    > {
    > el.PreviewKeyDown += AllPurposeEventHandler;
    > el.PreviewKeyUp += AllPurposeEventHandler;
    > el.KeyDown += AllPurposeEventHandler;
    > el.KeyUp += AllPurposeEventHandler;
    > }
    >
    > When I hover over el.PreviewKeyUp in the C# program, it says
    > "KeyEventHandler UIElement.PreviewKeyUp", which is right.
    >
    > When I hover over el, I get "(local variable) UIElement el".
    >
    > These are just event handlers, right?
    >
    > Here's the AllPurposeEventHandler signature:
    >
    > void AllPurposeEventHandler(object sender, RoutedEventArgs args)
    > {
    > // do some stuff
    > }
    >
    > This works fine. So here's my corresponding VB code:
    >
    > 'I used m_grid instead of grid, because VB is not case-sensitive,
    > ' and grid is a control in WPF.
    > Dim els() as UIElement = {win, m_grid, btn, text}
    > For Each el As UIElement In els
    > AddHandler el.PreviewKeyDown, AddressOf AllPurposeEventHandler
    > AddHandler el.PreviewKeyUp, AddressOf AllPurposeEventHandler
    > AddHandler el.KeyDown, AddressOf AllPurposeEventHandler
    > AddHandler el.KeyUp, AddressOf AllPurposeEventHandler
    > Next el
    >
    > Here's this routine...
    >
    > Private Sub AllPurposeEventHandler(ByVal sender As Object,
    > ByVal e As RoutedEventArgs)
    > 'do some stuff (snipped for length
    > End Sub
    >
    > When I hover over el.PreviewKeyUp, it says
    >
    > Public Event PreviewKeyDown(sender as Object,
    > e as System.Windows.Input.KeyEventArgs)
    >
    > When I hover over el, I get "Dim el as System.Windows.UIElement".
    >
    > And it won't take AllPurposeEventHandler as a delegate because the args
    > are the wrong type, not RoutedEventArgs.
    >
    > Why the heck would it do that? Do you see any obvious typos? Is it
    > precompiling differently than C#? Am I misunderstanding the C# code?
    >
    > I tried casting el to a UIElement (although it already is one) and then
    > checking the PreviewKeyDown, and it does the same thing. I also tried
    > taking it out of the loop and using the actual controls (win, m_grid,
    > btn, text), but I get the same result.
    >
    > I have the same namespaces imported in VB as I do in C#.
    >
    > Any ideas?
    >
    > Thanks,
    > Robin S.
    > Ts'i mahnu uterna ot twan ot geifur hingts uto.
    >
    >




      My System SpecsSystem Spec

  5. #5


    RobinS Guest

    Re: WPF in VB / Routed Events help needed

    "Patrice" <contact-ongla@leg-si.com> wrote in message
    news:1170755170.138458.106110@v33g2000cwv.googlegroups.com...
    > I'm surprising that this C# code may work cause your
    > 'AllPurposeEventHandler' sub is definitely not of the excpected type.
    > By setting type KeyEventArgs instead of RoutedEventArgs for e param
    > you can compile according without any problem to the error message.
    >
    > But maybe I didn't understand what you want...
    >



    It *is* a RoutedEvent in C#, so it *does* match the arguments of the
    AllPurposeEventHandler.

    When you click on a button, it routes the click through all of the content
    on the button. In C#, this is reflected when you have a UIElement such as a
    button, and you look at the intellisense available for the MouseDown event.
    It shows up as a RoutedEvent, not as a mouse event.

    But in VB, it shows up as a Mouse Event. It won't capture it as a routed
    event.

    I posted a workaround that lets you sort of do relaxed delegates, but it
    would be really nice if the same thing worked the same way in both
    languages.

    Robin S.
    Ts'i mahnu uterna ot twan ot geifur hingts uto.



      My System SpecsSystem Spec

WPF in VB / Routed Events help needed

Similar Threads
Thread Thread Starter Forum Replies Last Post
vpn bridged to routed migration Jerzy Kaczorek SBS Server 4 14 Feb 2010
Online calendar events showing up as Private events? Scott Live Mail 0 13 Jan 2010
How To Clear Administrative Events Log - Events Viewer ColTom2 Vista General 1 20 Nov 2009
Port Problem with Routed Computer Ty McConnell Vista print fax & scan 0 05 Sep 2008
Fire events from Xaml files.. events in Pre compiled Dll's SureshGubba Avalon 1 15 Sep 2006