2016-02-26 83 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(); 
相關問題