1

Im trying to open a new form when a label is double clicked. Im able to drag and drop the label .Im trying to open a new form on double click of label now.

  private void control_MouseDown(object sender, MouseEventArgs e)
    {
        var control = sender as Control;
        this.DoDragDrop(control.Name, DragDropEffects.Move);
    }
    private void control_DoubleClick(object sender, EventArgs e)
    {
        frm = new Frm();
        frm.ShowDialog();
        frm.Dispose();
    }

EDIT 1: I have tried both possible answers below, and they have not worked for me?

AndroidAL
  • 1,111
  • 4
  • 15
  • 35

4 Answers4

3

A more cleaner way is (note I changed Frm to Form1):

private void control_DoubleClick(object sender, EventArgs e)
{

    using (Form1 frm = new Form1())
   {
        frm.ShowDialog();
   }
}
mnieto
  • 3,744
  • 4
  • 21
  • 37
  • 4
    While what it says is right, it doesn't answer the question at all. I don't know why is this answer getting that many upvotes (I'm not downvoting it since it's not incorrect, it's just not an answer for this question) – Jcl Feb 23 '16 at 12:56
2

You can't add DragDrop on MouseDown and then DoubleClick. That won't work.

I don't think there's an easy way to get around that, but once a control is being dragged, it won't respond to double click messages.

I've made some quick tests, and there's a "hacky" way. It'll make your dragging look weird (since it'll start after some time, instead of immediately after you press the mouse button), but here it goes:

private bool _willDrag = false;

private bool control_MouseUp(object sender, MouseEventArgs e)
{
  // disable dragging if we release the mouse button
  _willDrag = false;
}

private bool control_DoubleClick(object sender, EventArgs e)
{
  // disable dragging also if we double-click
  _willDrag = false;

  // .. the rest of your doubleclick event ...
}

private void control_MouseDown(object sender, MouseEventArgs e)
{
  var control = sender as Control;
  if (control == null)
    return;

  _willDrag = true;
  var t = new System.Threading.Timer(s =>
  {
    var callingControl = s as Control;
    if (callingControl == null)
      return;

    // if we released the mouse button or double-clicked, don't drag
    if(!_willDrag)
      return;

    _willDrag = false;
    Action x = () => DoDragDrop(callingControl.Name, DragDropEffects.Move);
    if (control.InvokeRequired)
      control.Invoke(x);
    else
      x();
  }, control, SystemInformation.DoubleClickTime, Timeout.Infinite);
}
Jcl
  • 27,696
  • 5
  • 61
  • 92
1

In the form.Designer right click on your label then properties, in the properties window click in events (the thunder icon), in the double_Click event dropdown select the event handler (control_DoubleClick) this method must have two parameters an object and a eventArgs

enter image description here

1

This is tricky as the DoDragDrop will eat up any further mouse events, and MSDN posting a rather stupid example doesn't help much.

Solution: Do not start the D&D in the MouseDown if you want to still receive click or double click events but use the MouseMove instead:

Replace this

private void control_MouseDown(object sender, MouseEventArgs e)
{
    var control = sender as Control;
    this.DoDragDrop(control.Name, DragDropEffects.Move);
}

by this:

private void control_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
        DoDragDrop((sender as Control), DragDropEffects.Move); 
}

Don't forget to hook up the new event!

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
  • Thanks, i have tried ` private void control_MouseMove(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left) DoDragDrop((sender as Control), DragDropEffects.Move); else if(e.Button == System.Windows.Forms.MouseButtons.Right) { using (Form frm = new Form()) { ln.ShowDialog(); } } }` - it wont open a new form when i right click on the label – AndroidAL Feb 23 '16 at 15:57
  • Of course, for rightclicking you don't use the MouseMove but the normal MouseClick or MouseDoubleClick event depending on what you want.. RightDoubleClicking is not a normal windows action, though. It works but is not part of the windows user interface behaviour so users won't expect it and when you train them to, they will be disappointed to find that it won't work anywhere else. – TaW Feb 23 '16 at 16:02
  • 1
    This should work as well as my answer, I'd however prefer doing it on a timer, since mouse could be moving while doubleclicking. That's unless you want to control a minimum moving distance (which should be done for *correct* drag&dropping anyway). +1 anyway since, unlike all other answers, this is actually answering the question :-) – Jcl Feb 23 '16 at 16:10
  • 1
    Yes, doubleclicking can always go wrong, new windows users have always had to get used to it.. But the worst case is that nothing happens. As for the right way of doing d&d, I tend to agree but we are very much alone, everyone else does it right on the mousedown ;-) – TaW Feb 23 '16 at 16:14