472,353 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 3471
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
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
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...
1
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)...
11
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...
30
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...
12
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...
5
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...
5
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...
8
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.