473,498 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3540
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2234
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
2
1959
by: dave | last post by:
Hi, I have a COM control / server I'm trying to get events from in my c# applications. 1) I'm trying to get events in both c# console apps and in c# winform apps. 2) I can call methods on the...
1
7536
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
11
1837
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
30
3606
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
12
2183
by: cj | last post by:
I would like to have menu items a main menu bar that represent the days of the week. When you click on them they alternate from checked to unchecked. Right now I have 7 subs that look like this...
5
2740
by: Daniel | last post by:
Hey guys When you hook an event (c# 2.0 syntax): myEvent += MyMethodToFire; You need to also unsubscribe it to avoid a resource leak so that the object it is in gets garbage collected like so...
5
1883
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
8
1139
by: Rex the Strange | last post by:
Hello All, I don't know if this is possible, but I'll give it a shot, here. I'm sick of writing the following line of code in control event handlers: if not visible then exit sub Is it...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.