2014-12-05 70 views
0

我有一個文本框,其中其Leave事件是這樣的:時間我需要這個事件如何控制異步事件文本

private async void TxtLotTextLeave(object sender, EventArgs e) 
{ 
    if (!isChecked) 
    { 
     isChecked = true; 
     var mylength = BarcodeUtil.LotStripZeroes(txtLot.Text.Trim()).Length; 
     var strippedLot = BarcodeUtil.LotStripZeroes(txtLot.Text.Trim()); 
     if (mylength > 0) 
     { 
      if (mylength.Between(16, 18) && 
       (strippedLot.StartsWith(AppState.LotOldStandardDigits) || 
       strippedLot.StartsWith(AppState.LotStandardDigits))) 
      { 
       await GetLotData(); 
      } 
      else 
      { 
       ShowAppMessage(AppMessages["WrongLot"], 0, Color.Black, Color.BlanchedAlmond); 
       txtLot.Text = ""; 
       LotFocus(true); 
      } 
     } 
    } 
} 

99%喜歡這個工作。

但我只需要當一個特定的按鈕點擊不發射它。

按鈕點擊:

private void BtnClearClick(object sender, EventArgs e) 
{ 
    ClearForm(); 
    LotFocus(true); 
} 

我嘗試了明顯的使用全局布爾變量,並將其設置爲false click事件,並檢查它在產假,但它不具有做work..I嫌疑人與異步?

附加信息:

了我所是創建一個布爾變量needTxtValidation並嘗試將其設置爲在不同的地方,如按一下按鈕,文本框,按鍵,鼠標按下按鈕假的,但它沒有工作。

+1

發佈你的嘗試與布爾標誌,應該工作。而且AFAIK沒有乾淨的方式去做你想做的事情。另外,哪個UI框架? Winforms或..? – 2014-12-05 06:57:47

+0

我也可以使用一個骯髒的... – e4rthdog 2014-12-05 07:00:24

回答

1

好吧,這是我設法找到的骯髒的方式。您需要繼承Button,覆蓋WndProc並公開一個表示當前是否正在處理MouseDown的布爾值。

class ButtonEx : Button 
{ 
    public bool IsInMouseDown { get; set; } 
    protected override void WndProc(ref Message m) 
    { 
     const int WM_LBUTTONDOWN = 0x0201; 
     try 
     { 
      if (m.Msg == WM_LBUTTONDOWN) 
       IsInMouseDown = true; 
      base.WndProc(ref m); 
     } 
     finally //Make sure we set the flag to false whatever happens. 
     { 
      if (m.Msg == WM_LBUTTONDOWN)//Required to fight with reentracy 
       IsInMouseDown = false; 
     } 
    } 
} 

然後在您的休假方法

private async void TxtLotTextLeave(object sender, EventArgs e) 
{ 
    if (yourButton.IsInMouseDown) 
    { 
     Console.WriteLine("Ignoring Leave"); 
     return; 
    } 
    ... 
} 

這工作,但我並不能保證它會繼續總是工作。你可能需要解決一些角落案例或我錯過的明顯的事情。這是一個非常黑客的代碼,你最好重新設計邏輯。