在我的C#窗體窗體上,我有3個文本框命名爲textBuyPrice,textSell和textMargin。用戶可以輸入買入價格,如果他們輸入賣出價格,那麼代碼將計算它們的保證金。如果用戶輸入保證金,則代碼將輸入賣出價格。請參閱組成邊距計算的這2位代碼。文本框輸入和離開事件重複
此代碼捕獲光標在文本框中的輸入並將當前值設置爲變量。
private void textMargin_Enter(object sender, EventArgs e)
{
OriginalMarginValue = this.textMargin.Text;
}
該代碼捕獲文本框中的離開事件並執行任何更改,運行一些計算並更新任何相關字段。
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;
}
}
正在發生的事情似乎是進入和離開事件之間的循環。在我嘗試退出文本框時,Leave事件觸發後。 Enter事件再次觸發,它不會讓我看到。
這是怎麼回事?我是否應該強制標籤將焦點放在另一個字段上?
上面的代碼與textSellPrice代碼相同,我沒有在該文本框中出現同樣的問題。
THX
編輯 這是因爲代碼進入試圖離開文本框後的第二次Enter事件調用堆棧:
AutoShop.exe AutoShop。 formPartDetail.textMargin_Enter(object sender = {Text =「57.14%」},System.EventArgs e = {System.EventArgs})Line 168 C# System.Windows.Forms.dll!System.Windows.Forms.Control.OnEnter(System .EventArgs e)+ 0x88字節
System.Windows.Forms.dll的!System.Windows.Forms.Control.NotifyEnter()+ 0x1F的字節
System.Windows.Forms.dll的!System.Windows.Forms.ContainerControl.UpdateFocusedControl()+ 0x195字節 系統。 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。控制值= {文本=「57.14%」})+ 0x73字節
System.Windows.Forms.dll!Syste m.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字節
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.SetActiveControlInternal(System.Windows.Forms。控制值= {Text =「」})+ 0x73字節
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導向,bool轉發)+ 0x1b字節
System.Windows.Forms.dll!System.Windows.Forms.Control.SelectNextControl(System.Windows.Forms.Control ctl,bool forward,bool tabStopOnly,bool nested,bool wrap)+ 0x7e字節
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 |返回)+ 0x71字節
對textMargin有任何驗證嗎?並且OriginalMarginValue屬性在getter或setter中是否有可能引用文本框的代碼? – ekolis
沒有驗證,並且OriginalMarginValue是一個int字段 – Ryan
您的代碼乍一看似乎是正確的。是另一個控件(即groupbox等)內的文本框?如果是這樣,是否爲該容器處理了類似的事件? (進入和離開事件被串聯在母鏈上)。 – K3N