2014-12-05 74 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; 
    } 
    ... 
} 

这工作,但我并不能保证它会继续总是工作。你可能需要解决一些角落案例或我错过的明显的事情。这是一个非常黑客的代码,你最好重新设计逻辑。