qakmak
qakmak

Reputation: 1377

Winform MouseDoubleClick event not firing after add MouseDown event

I'm creating a User Control. It can be drag, also can be select. and need can be get single click, and double click.

At first I add the Selected property in the Mouse Click event. But It only get firing after Mouse up. Then I change My solution , I add it in Mouse Down event:

private void MyControl_MouseDown(object sender, MouseEventArgs e)
{             
      if (!Selected)
      {
            Selected = true;

        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        }
        else if(e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            if (null != MouseRightClick)
                MouseRightClick(model, this);
        }
      }                                 
}

But after that, I can't get Mouse Double Click event anymore:

private void MyControl_MouseDoubleClick(object sender, MouseEventArgs e)
{
   // do something
}

Update I saw the link you guys gave me, and I know the reason was:

that calling DoDragDrop() will cancel mouse capture

But I still want to know, Is there any way to keep the Drag with Double Click events?

Upvotes: 2

Views: 2856

Answers (1)

qakmak
qakmak

Reputation: 1377

Actually after you know the reason as I said in my question update, It's very easy to fixed. We should declare a variable for recognize draw:

private bool IsCanDraw = false;

Then you should add it in Mouse Down

private void MyControl_MouseDown(object sender, MouseEventArgs e)
{
      if (e.Clicks != 1)
      {
          IsCanDraw = false;
      }
      else
     {
         IsCanDraw = true;
     }

     // your other code

}

Adn you need to change Mouse Move to :

private void MyControl_MouseMove(object sender, MouseEventArgs e)
{
   if (e.Button == MouseButtons.Left & IsCanDraw)
   {
        this.DoDragDrop(this, DragDropEffects.Move);
        // Disables IsCanDraw method until next drag operation
        IsCanDraw = false;
    }
}

As you see you can only do DoDragDrop() whene IsCanDraw == true Then you Mouse Double Click event will fine.


The original ideas from(VB.NET) :

http://vbcity.com/forums/t/100458.aspx

http://sagistech.blogspot.com/2010/03/dodragdrop-prevent-doubleclick-event.html

Upvotes: 3

Related Questions