文本框进入和离开事件重复

时间:2012-10-29 00:52:21

标签: c# .net winforms

在我的C#窗体上,我有3个文本框,分别命名为textBuyPrice,textSell和textMargin。用户可以输入买入价,如果他们输入卖出价,那么代码将为他们计算保证金。如果用户输入保证金,则代码将输入卖出价。请参阅构成保证金计算的这2位代码。

此代码捕获文本框中光标的输入,并将当前值设置为变量。

private void textMargin_Enter(object sender, EventArgs e)
        {
        OriginalMarginValue = this.textMargin.Text;
        }

此代码捕获文本框中的leave事件并进行任何更改,运行一些计算并更新任何相关字段。

private void textMargin_Leave(object sender, EventArgs e)
        {
        if (this.textMargin.Text != OriginalMarginValue)
            {
            Parts part = new Parts();
            decimal dcmBuy = part.RemoveDollarSign(this.textBuyPrice.Text);
            decimal dcmMargin = part.RemovePercentSign(this.textMargin.Text);
            decimal dcmSell = part.GetSellPrice(dcmBuy, dcmMargin / 100);
            string strSell = dcmSell.ToString("C");
            this.textSellPrice.Text = strSell; 
            }

        }

正在发生的事情似乎是Enter和Leave事件之间的循环。当我尝试并跳出文本框时,Leave事件触发后。 Enter事件再次触发,它不会让我标签。

发生了什么事?我应该强制标签将焦点设置在另一个字段上吗?

上面的代码与textSellPrice代码相同,我在该文本框中没有相同的问题。

THX

修改 的 这是调用堆栈,因为代码在尝试离开文本框后第二次进入Enter事件:

  

AutoShop.exe!AutoShop.formPartDetail.textMargin_Enter(object sender = {Text =“57.14%”},System.EventArgs e = {System.EventArgs})第168行C#       System.Windows.Forms.dll!System.Windows.Forms.Control.OnEnter(System.EventArgs e)+ 0x88 bytes
      System.Windows.Forms.dll!System.Windows.Forms.Control.NotifyEnter()+ 0x1f bytes
      System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.UpdateFocusedControl()+ 0x195 bytes       System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.AssignActiveControlInternal(System.Windows.Forms.Control value = {Text =“57.14%”})+ 0x60 bytes
      System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ActivateControlInternal(System.Windows.Forms.Control control,bool originator = false)+ 0x48 bytes
      System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.SetActiveControlInternal(System.Windows.Forms.Control value = {Text =“57.14%”})+ 0x73 bytes
      System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ValidateThroughAncestor(System.Windows.Forms.Control ancestorControl,bool preventFocusChangeOnError)+ 0x212 bytes
      System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.EnterValidation(System.Windows.Forms.Control enterControl)+ 0x7c bytes
      System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.UpdateFocusedControl()+ 0x8a bytes
      System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.AssignActiveControlInternal(System.Windows.Forms.Control value = {Text =“”})+ 0x60 bytes
      System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ActivateControlInternal(System.Windows.Forms.Control control,bool originator = false)+ 0x48 bytes
      System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.SetActiveControlInternal(System.Windows.Forms.Control value = {Text =“”})+ 0x73 bytes
      System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.SetActiveControl(System.Windows.Forms.Control ctl)+ 0x33 bytes
      System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ActiveControl.set(System.Windows.Forms.Control value)+ 0x5 bytes
      System.Windows.Forms.dll!System.Windows.Forms.Control.Select(bool directed,bool forward)+ 0x1b bytes
      System.Windows.Forms.dll!System.Windows.Forms.Control.SelectNextControl(System.Windows.Forms.Control ctl,bool forward,bool tabStopOnly,bool nested,bool wrap)+ 0x7e bytes
      System.Windows.Forms.dll!System.Windows.Forms.Form.ProcessTabKey(bool forward = true)+ 0x24 bytes
      System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ProcessDialogKey(System.Windows.Forms.Keys keyData = LButton | Back)+ 0x71 bytes

1 个答案:

答案 0 :(得分:0)

尝试对文本框使用验证事件。您也可以选择取消验证。

e.g。

   private void textBox2_Validating(object sender, CancelEventArgs e)
        {
            if (DoSomething(textBox2))
            {
                textBox3.Text = textBox2.Text;
            }
            else
            {
                e.Cancel = true; //cancel the validation.
            }

        }