Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - WPF in VB / Routed Events help needed

 
 
Old 02-05-2007   #1 (permalink)
RobinS


 
 

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
Old 02-05-2007   #2 (permalink)
RobinS


 
 

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
Old 02-06-2007   #3 (permalink)
Patrice


 
 

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
Old 02-08-2007   #4 (permalink)
RobinS


 
 

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
Old 02-09-2007   #5 (permalink)
RobinS


 
 

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
 

Thread Tools


Similar Threads
Thread Forum
Error message when I send an email thru routed email (POP) Vista mail
Port Problem with Routed Computer Vista print fax & scan
Wireless connection to a non broadcasting SSID routed Vista networking & sharing


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46