0

I'm trying to open a form after a label is double clicked. My Code:

else if (e.Clicks == 2)
{
    foreach (var control in myFLP.Controls)
    {
        if(control is Label)
        {
            var Id = mylabel.Name.ToString();
            int personID;

            if (!String.IsNullOrWhiteSpace(Id) && int.TryParse(Id, out personID))
            {
                Form frm = new Form(_controller, personID);
                frm.ShowDialog();
                frm.Dispose();
            }
            else 
            {
                Form2 frm2 = new Form2();
                frm2.ShowDialog();
                frm2.Dispose();
                Console.WriteLine("Hello");
            }
        }
    }
}

When i double click on the label nothing happens? So i tried calling Form frm = new Form(); without passing any parameters. The form opened after the double click, but kept opening for every label in the myFLP?

Edit 1: I have added an ELSE. I think my condition in incorrect.

Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
AndroidAL
  • 1,111
  • 4
  • 15
  • 35
  • You can use [`DoubleClick`](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.doubleclick(v=vs.110).aspx) event for `Label`. As for "nothing happens", what should happens? Did you try to set breakpoint? Is event handler called? – Sinatr Feb 26 '16 at 12:13
  • I cant use that , because im using `MouseDown`, a form should appear with data. I used a breakpoint, @ ` if (!String.IsNullOrWhiteSpace(Id) && int.TryParse(Id, out personID))` – AndroidAL Feb 26 '16 at 12:17
  • Either your double-click detection doesn't work properly or condition. Hard to tell. Check both using breakpoints. – Sinatr Feb 26 '16 at 12:20
  • i added an `else` statement under `if`. i think it could be the `condition`, because the `else` is fired. i will edit my question. – AndroidAL Feb 26 '16 at 12:22
  • First set breakpoint on `foreach`. Is it called when you double-click? Not - you have problem with double click detection. Then set breakpoint on `if(control is Label)` is it called? Not - you don't find label (see [this](http://stackoverflow.com/q/3419159/1997232)). Then set breakpoint furher, etc. until you find something what doesn't work. Tell us what. – Sinatr Feb 26 '16 at 12:28
  • Thanks for the tips, i set `breakpoints` at the points you mentioned.Program stopped at both points. It executes the `ELSE` statement? – AndroidAL Feb 26 '16 at 12:35
  • Check the value of `Id`. The name of a label is unlikely to be a valid integer. – Graham Feb 26 '16 at 12:43
  • Value of `Id` is `myLabel`, i ran `Console.WriteLine(Id)` in the `ELSE` statement. – AndroidAL Feb 26 '16 at 12:51

2 Answers2

1

You probably subscribed to event Control.Click. You should subscribe to event Control.DoubleClick.

If you are using Visual Studio designer, select the label you want to react on double-click; go to properties (-enter), Select the flash to see all events, and look for DoubleClick in the Action category.

In function InitializeComponent() (see the constructor of your form) You'll see something similar to:

this.label1.DoubleClick += new System.EventHandler(this.label1_DoubleClick);

the event handling function:

private void label1_DoubleClick(object sender, EventArgs e)
{
    // sender is the label that received the double click:
    Debug.Assert(Object.ReferenceEquals(sender, this.label1));

    Label doubleClickedLabel = (Label)Sender;
    var Id = doubleClickedLabel.Text;
    int personID;
    if (!String.IsNullOrWhiteSpace(Id) && int.TryParse(Id, out personID))
    {   // open form. Note the use of the using statement
        using (Form frm = new Form(_controller, personID)
        {
            frm.ShowDialog();
        }
    }
    else 
    {
        using (Form2 frm2 = new Form2())
        {
            frm2.ShowDialog();
        }
    }
}
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
0

I think you are checking the wrong label. And the following line

var Id = mylabel.Name.ToString();

should be changed to

var Id = control.Name.ToString();
Marc
  • 3,905
  • 4
  • 21
  • 37