R
RobinS
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.
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.