Mousedown event

gilgamesh

Newcomer
Joined
Apr 28, 2004
Messages
3
Hi,
First off thanks for your time.
I have a class that is derived from a control. I am trying to handle mouse events. I am able to handle most mouse events just fine except for MouseDown.

My code is fairly simple:
//constructor for the class
public MyControlClass()
{
this.MouseDown+=new MouseButtonEventHandler(MyControlClass_MouseDown);

}

public void MyControlClass_MouseDown(object sender, MouseEventArgs e)
{
MouseDownPt = e.GetPosition(this);
MessageBox.Show(MouseDownPt.X + " " + MouseDownPt.Y);
}

I put in breakpoints inside mousedown...never being hit.
Any suggestions?
Thanks again,
G
 
Override protected methods

I don't have any insight into the problem, but personally I find it neater to override the protected On[Action] methods when deriving from controls:

C#:
protected override void OnMouseDown(MouseEventArgs e)
{
    MouseDownPt = e.GetPosition(this);
    MessageBox.Show(MouseDownPt.X + " " + MouseDownPt.Y);

    //Call method in base class
    base.OnMouseDown(e);
}

Note that this is the preferred method of handling events in derived classes, as specified in MSDN:

The OnMouseDown method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Good luck :cool:
 
Back
Top