简体   繁体   English

双击不适用于标签

[英]Double Click doesnt work on Label

I'm trying to open a form after a label is double clicked . double clicked label后,我试图打开一个form 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? 当我double click label什么也没发生? So i tried calling Form frm = new Form(); 所以我尝试调用Form frm = new Form(); without passing any parameters. 不传递任何参数。 The form opened after the double click , but kept opening for every label in the myFLP ? double click后会打开表格,但myFLP每个标签都会一直打开?

Edit 1: I have added an ELSE . 编辑1:我添加了ELSE I think my condition in incorrect. 我认为我的状况不正确。

You probably subscribed to event Control.Click. 您可能已订阅事件Control.Click。 You should subscribe to event Control.DoubleClick. 您应该订阅事件Control.DoubleClick。

If you are using Visual Studio designer, select the label you want to react on double-click; 如果您使用的是Visual Studio设计器,请双击选择要反应的标签。 go to properties (-enter), Select the flash to see all events, and look for DoubleClick in the Action category. 转到属性(输入),选择闪光灯以查看所有事件,然后在“操作”类别中查找DoubleClick。

In function InitializeComponent() (see the constructor of your form) You'll see something similar to: 在函数InitializeComponent()中(请参见表单的构造函数),您将看到类似以下内容:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM