gilgamesh Posted July 3, 2007 Posted July 3, 2007 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 Quote
MrPaul Posted July 3, 2007 Posted July 3, 2007 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: 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: Quote Never trouble another for what you can do for yourself.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.