簡體   English   中英

嘗試從另一個文本框更新一個文本框,反之亦然C#Winforms

[英]Trying to update one textbox from another and vice-versa C# Winforms

我在Winform上有兩個文本框。 我們的想法是在一個盒子中輸入華氏溫度,然后在用戶輸入時將Celcius放在另一個盒子中,反之亦然。

我使用文本框的“離開”方法可以正常工作,但我試圖使用TextChanged方法來避免單擊或按下結果按鈕。

我現在遇到的問題是,當處理第一個字符時,焦點移動到第二個框,它會觸發TextChanged方法。

我嘗試使用發件人啟用if()但發件人現在是目標框。

請問有什么想法嗎?

       private void TB_Fahrenheit_TextChanged(object sender, EventArgs e)
    {

        float C, F;

        if (TB_Fahrenheit.Text != "")
        {
            if (float.Parse(TB_Fahrenheit.Text) < 1.0F)
            {
                TB_Fahrenheit.Text = "0" + TB_Fahrenheit.Text;
            }

            F = float.Parse(TB_Fahrenheit.Text);
            C = ((F - 32) * 5) / 9;
            TB_Celcius.Text = C.ToString();
        }

    }

    private void TB_Celcius_TextChanged(object sender, EventArgs e)
    {
        float C, F;
        string SentBy = ((TextBox)sender).Name;

        MessageBox.Show(SentBy);
        return;

        if(SentBy != TB_Fahrenheit.Text)
        {

        if (TB_Celcius.Text != "")
        {
            if (float.Parse(TB_Celcius.Text) < 1.0F)
            {
                TB_Celcius.Text = "0" + TB_Celcius.Text;
            }

            C = float.Parse(TB_Celcius.Text);
            F = ((C * 9) / 5) + 32;
            TB_Fahrenheit.Text = F.ToString();
        }
        }

    }

    private void TB_Celcius_Enter(object sender, EventArgs e)
    {
        TB_Celcius.Clear();
        TB_Fahrenheit.Clear();
    }

    private void TB_Fahrenheit_Enter(object sender, EventArgs e)
    {
        TB_Celcius.Clear();
        TB_Fahrenheit.Clear();
    }

我會使用布爾檢查器。您正在更改具有文本更改事件的文本框的文本。 如果當前正在編輯其他框,則需要確保它不執行任何操作。 我的例子

private void setSliderValue(int currentAudioSecond)
{
    valueFromTimer = true;
    //Do Stuff
    valueFromTimer = false;
}

然后當我的滑塊控件上的值更改時:

private void sliderAudio_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    if (!valueFromTimer)
    {
        //Do stuff  
    }
}

我在加載窗口或表單時經常這樣做,我填寫一個組合框。 為了避免將選定的值/索引更改為觸發,我必須實現這樣的標志。

我只需在Control.Enter EventControl.Leave事件上訂閱和取消訂閱該事件

當您Enter fahrenheitTextBox時,它將停用celsiusTextBox的TextChanged事件。 輸入celsiusTextBox時會發生相同的情況,您的fahrenheitTextBox.TextChanged事件將被取消訂閱,允許您利用正確的事件TextChanged事件來實現您的目標。

除此之外,我不鼓勵在你的GUI中使用數學公式,因為你可能有錯,而且很難測試。 最好有一個靜態Convert類,其中包含適當的方法來進行所需的轉換。

public static class Convert {
    public float ToFahrenheit(float celsius) {
        float f = celsius * 9 / 5 + 32;
        return f;
    }

    public float ToCelsius(float fahrenheit) {
        float c = (fahrenheit - 32) * 5 / 9;
        return c;
    }
}

然后你可以編寫你的單元測試。

[TestMethod]
public void ZeroCelsiusShouldConvertTo32Fahrenheit() {
    // Given
    float expected 32.0;
    float celsius = 0.0;        


    // When
    var actual = Convert.ToFahrenheit(celsius);

    // Then
    Assert.AreEqual(typeof(float), actual);
    Assert.AreEqual(expected, actual);
}

我認為你必須使用Leave事件而不是TextChanged 。因為它是在什么時候引發的

  1. 您可以通過使用鍵盤(TAB,SHIFT + TAB等),通過調用Select或SelectNextControl方法或通過將ContainerControl.ActiveControl屬性設置為當前窗體來更改焦點。

  2. 使用鼠標或通過調用Focus方法更改焦點時。

我建議使用KeyPress事件來計算其他TextBox中的值,類似這樣,盡管你必須對float.Parse進行一些格式檢查以確保你有一個數值。

        private void TB_Fahrenheit_KeyPress(object sender, KeyPressEventArgs e)
    {
        float C, F;

        if (TB_Fahrenheit.Text != "")
        {
            if (float.Parse(TB_Fahrenheit.Text) < 1.0F)
            {
                TB_Fahrenheit.Text = "0" + TB_Fahrenheit.Text;
            }

            F = float.Parse(TB_Fahrenheit.Text);
            C = ((F - 32) * 5) / 9;
            TB_Celcius.Text = C.ToString();
        }
    }

    private void TB_Celcius_KeyPress(object sender, KeyPressEventArgs e)
    {
        float C, F;
        string SentBy = ((TextBox)sender).Name;


        if (TB_Celcius.Text != "")
        {
            if (float.Parse(TB_Celcius.Text) < 1.0F)
            {
                TB_Celcius.Text = "0" + TB_Celcius.Text;
            }

            C = float.Parse(TB_Celcius.Text);
            F = ((C * 9) / 5) + 32;
            TB_Fahrenheit.Text = F.ToString();
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM