Trying to update one text field from another and vice versa C # Winforms

I have two text fields in Winform. The idea is to enter Fahrenheit in one box and present it to Celcius in another as a user, and vice versa.

I work fine using the Leave method in the text box, but I tried to use the TextChanged method to not press or not click the button for the result.

Now the problem is that when processing the first character, the focus moves to the second block, which calls the TextChanged method.

I tried using the sender to include if (), but the sender is now the destination field.

Any thoughts please?

       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();
    }
+4
source share
4

. . , , .

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

, :

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

, . / , , .

+3

Control.Enter Control.Leave.

Enter fahrenheitTextBox, TextChanged celsiusTextBox. , celsiusTextBox , fahrenheitTextBox.TextChanged , EventChanged Event .

, , , . 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);
}
+2

, Leave TextChanged. ,

  • you change focus using the keyboard (TAB, SHIFT + TAB, etc.) by calling the Select or SelectNextControl methods or setting the ContainerControl.ActiveControl property to the current form.

  • When you change focus with the mouse or call the Focus method.

+1
source

I would suggest using the KeyPress event to calculate the value in another TextBox, something like this, although you will need to do some format checking on float.Parse to make sure you have a numeric value.

        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();
        }

    }
0
source

All Articles