2016-02-26 86 views
0

我试图在labeldouble clicked后打开form。 我的代码:Double Click无法在标签上工作

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

当我double clicklabel没有任何反应?所以我尝试呼叫Form frm = new Form();而不传递任何参数。表格在double click之后打开,但在myFLP中的每个标签都保持打开状态?编号1: 我已添加ELSE。我认为我的病情不正确。

+0

您可以使用['DoubleClick' ](标签)的https://msdn.microsoft.com/en-us/library/system.windows.forms.control.doubleclick(v = vs.110).aspx)事件。至于“什么都没有发生”,会发生什么?你尝试设置断点吗?是否调用事件处理程序 – Sinatr

+0

我不能使用它,因为即时通讯使用'MouseDown',表单应该与数据一起出现。我使用了一个断点,@'if(!String.IsNullOrWhiteSpace(Id)&& int.TryParse(Id,out personID))' – AndroidAL

+0

您的双击检测无法正常工作或无法正常工作。很难说。使用断点检查两者。 – Sinatr

回答

1

你可能订阅事件Control.Click。您应该订阅事件Control.DoubleClick。

如果您使用的是Visual Studio设计器,请选择您想要双击的标签;转到属性(-enter),选择Flash查看所有事件,然后在“操作”类别中查找DoubleClick。

在函数的InitializeComponent()(请参阅您的窗体的构造函数),你会看到类似的东西:

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

事件处理函数:

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

我认为你正在检查错误的标签。 及以下线路

var Id = mylabel.Name.ToString(); 

应改为

var Id = control.Name.ToString(); 
相关问题