AndroidAL
AndroidAL

Reputation: 1119

Double Click on label open Form C#

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?

Upvotes: 1

Views: 2217

Answers (4)

Jcl
Jcl

Reputation: 28272

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);
}

Upvotes: 2

TaW
TaW

Reputation: 54433

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!

Upvotes: 1

Felipe Sierra
Felipe Sierra

Reputation: 181

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

Upvotes: 1

mnieto
mnieto

Reputation: 3874

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();
   }
}

Upvotes: 3

Related Questions