BenevolentDeity
BenevolentDeity

Reputation: 645

MouseEventArgs NotHandled equivalent?

I apologize for not knowing enough about what I want to do to use the correct terminology, but here's my situation. I have a Forms application and have implemented a MouseDown handler as represented by the pseudo-code below. In this code, if a certain condition is true I want do some custom thing, but if it is not true I want the default MouseDown behavior to occur as if there were no custom handler at all. By default I mean the ability to drag the focused form around the desktop if the left mouse button is pressed and held when the mouse pointer is in the form's title bar, or whatever other things one could expect to happen if there were no custom handler. Maybe I'm off base by assuming that some default MouseDown event is even involved when dragging the entire form around but I do know I am currently not able to do it if I implement the MouseDown event myself.

  private void treeView1_MouseDown(object sender, MouseEventArgs e)
  {
     if (...some testable condition is true...)
     {
        ...do something...
     }
     else
     {
        ...forward the MouseDown event to the default MouseDown handler...
     }
  }

Upvotes: 0

Views: 345

Answers (1)

ElliotSchmelliot
ElliotSchmelliot

Reputation: 8392

Not sure what your testable condition is, but you might be looking for a custom control in which you override specific event behaviors. One example of this here. Another example below:

public class CustomControl : SomeBaseControl
{
    protected override void OnMouseDown(MouseEventArgs mevent)
    {
        if (your_condition_passes)
        {
            // Do your logic
            return; // Prevent the base control event from firing
        }
        base.OnMouseDown(mevent); // Allow the base control event to fire
    }

}

You might choose to replace the your_condition_passes placeholder above with a boolean property that could be set either directly from the designer or through code.

Upvotes: 2

Related Questions