Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...