0

私の C# ウィンドウ フォームには、textBuyPrice、textSell、および textMargin という名前の 3 つのテキスト ボックスがあります。ユーザーは購入価格を入力できます。販売価格を入力すると、コードが証拠金を計算します。ユーザーが証拠金を入力すると、コードは販売価格を入力します。マージン計算を構成するこれらの 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 コードと同一であり、そのテキスト ボックスには同じ問題はありません。

どうも

編集 テキストボックスを離れようとした後、コードが2回目の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 バイト
System.Windows.Forms.dll!System.Windows.Forms.Control.NotifyEnter() + 0x1f バイト
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl. UpdateFocusedControl() + 0x195 バイト System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.AssignActiveControlInternal(System.Windows.Forms.Control 値 = {テキスト = "57.14 %"}) + 0x60 バイト
System.Windows.Forms .dll!System.Windows.Forms.ContainerControl.ActivateControlInternal(System.Windows.Forms.Control コントロール、bool originator = false) + 0x48 バイト
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.SetActiveControlInternal(System.Windows.Forms.Control 値 = {テキスト = "57.14 %"}) + 0x73 バイト
System.Windows.Forms.dll!System.Windows .Forms.ContainerControl.ValidateThroughAncestor(System.Windows.Forms.ControlncerControl, bool preventFocusChangeOnError) + 0x212 バイト
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.EnterValidation(System.Windows.Forms.Control enterControl) + 0x7c バイト
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.UpdateFocusedControl() + 0x8a バイト
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.AssignActiveControlInternal(System.Windows.Forms.Control 値= {テキスト = ""}) + 0x60 バイト
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ActivateControlInternal(System.Windows.Forms.Control コントロール、bool originator = false) + 0x48 バイト
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl .SetActiveControlInternal(System.Windows.Forms.Control 値 = {Text = ""}) + 0x73 バイト
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.SetActiveControl(System.Windows.Forms.Control ctl) + 0x33 バイト
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ActiveControl.set (System.Windows.Forms.Control 値) + 0x5 バイト
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 バイト
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.ProcessDialogKey(System.Windows.Forms.Keys keyData = LButton | Back) + 0x71 バイト

4

1 に答える 1

0

テキストボックスの Validating イベントを使用してみてください。検証をキャンセルするオプションも表示されます。

例えば

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

        }
于 2014-06-25T15:36:09.823 に答える