469,923 Members | 1,586 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

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.
Feb 5 '07 #1
3 3377
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.

Feb 5 '07 #2
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" <cl***@syncfusion.comwrote in message
news:11*********************@k78g2000cwa.googlegro ups.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.

Feb 5 '07 #3

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" <Ro****@NoSpam.yah.nonewrote in message
news:8d******************************@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.


Feb 9 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Edward Diener | last post: by
30 posts views Thread by Burkhard | last post: by
5 posts views Thread by Daniel | last post: by
8 posts views Thread by Rex the Strange | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.