Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello ALL,

While converting a c# code to vb i am facing a problem in converting a line of code.
The c# code is
private void Button_Loaded(object sender, RoutedEventArgs e)
{
    ((Button)sender).AddHandler(Button.MouseLeftButtonDownEvent,
        new MouseButtonEventHandler(Button_MouseLeftButtonDown), true);
    ((Button)sender).AddHandler(Button.MouseLeftButtonUpEvent,
        new MouseButtonEventHandler(Button_MouseLeftButtonUp), true);

    ((Button)sender).MouseMove += new MouseEventHandler(Button_MouseMove);
}


while i am able to get the Button.MouseLeftButtonDownEvent and Button.MouseLeftButtonUpEvent events i am not able to get the MouseMove event,
the converted code is something like this..

VB
Private Sub Button_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
       DirectCast(sender, Button).AddHandler(Button.MouseLeftButtonDownEvent, New MouseButtonEventHandler(AddressOf Button_MouseLeftButtonDown), True)
       DirectCast(sender, Button).AddHandler(Button.MouseLeftButtonUpEvent, New MouseButtonEventHandler(AddressOf Button_MouseLeftButtonUp), True)

       DirectCast(sender, Button).MouseMove += New MouseEventHandler(AddressOf Button_MouseMove) 
End Sub


But the DirectCast(sender,Button).MouseMove does not work it throws an error stating
MouseMove is an event and cannot be called directly use an Raiseevent to raise the event.

What is the problem here? and how can i fix this?

Looking forward to your answer...
Thanks in advance.
Posted
Updated 17-Apr-11 10:00am
v3
Comments
Sergey Alexandrovich Kryukov 17-Apr-11 16:01pm    
Rename VB to VB.NET to avoid confusion, add the tag "C#"!
Who would needs to translate it?
--SA

1 solution

I can clearly see the difference between the MouseMove line and the other two...
You don't use the += operator in VB to add an event handler.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Apr-11 16:00pm    
That is usually a key (not related to this (pointless) "translation"). My 5.
--SA
Roliking 18-Apr-11 3:45am    
well i tried to change the code below
DirectCast(sender, Button).MouseMove += New MouseEventHandler(AddressOf Button_MouseMove)
and did something like this
DirectCast(sender,Button).AddHandler(Button.MouseMove)
but i am not getting the mouse move event here at all,intellisense shows only MouseLeftButtonUp and MouseLeftButtonDown events but no MouseMove,can i directly use the MouseMove event? or what else can be done?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900