简体   繁体   中英

C# - Copy input from one textbox to another and vise versa

this is my first post and I'm fairly new to C# I'm trying to create a Celsius to Fahrenheit converter and vise versa.

I have two textboxes, the user can either input a number into the Celsius textbox and the Fahrenheit will automatically be calculated and displayed into the Fahrenheit textbox or the user can input into the Fahrenheit and the Celsius value will be calculated and output.

Here is the code I have so far, this is a WFA.

private void txtCelsius_TextChanged(object sender, EventArgs e)
    {
        ///*
        if(string.IsNullOrEmpty(txtCelsius.Text))
        {
            txtFahrenheit.Clear();
            return;

        }
        txtFahrenheit.Text = ((double.Parse(txtCelsius.Text)) * 1.8 + 32).ToString();
        //*/
    }

    private void txtFahrenheit_TextChanged_1(object sender, EventArgs e)
    {
        ///*
        if (string.IsNullOrEmpty(txtFahrenheit.Text))
        {
            txtCelsius.Clear();
            return;
        }
        txtCelsius.Text = ((double.Parse(txtFahrenheit.Text)) / 1.8 - 32).ToString();
        //*/
    }

Clearly, when I run this and input a value into either one of the textbox it will create an infinite loop. If I comment one or the other out it works for the other one.

Can someone help? Is there a way I can do something like the following pseudocode.

if textbox1 is getting input
    textbox2.input = disabled
    do calculations and display
if textbox2 is getting input
    textbox1.input = disabled
    do calculations and display

If tried searching for a solution but finding it hard to word what I'm looking for and coming up with no solutions.

PS. I keep seeing this in other posts, this is not homework, I'm just trying to come up with small programs to learn.

You are getting into an infinite loop because each time you update the other text boxes value it's firing the TextChanged event. To handle this add a variable to control when to update use TextChanged code.

bool _updating = false;
private void txtCelsius_TextChanged(object sender, EventArgs e)
{
   if (!_updating)
   {
     try
     {
       _updating = true;
       ///*
       if(string.IsNullOrEmpty(txtCelsius.Text))
       {
         txtFahrenheit.Clear();
         return;
       }
       txtFahrenheit.Text = ((double.Parse(txtCelsius.Text)) * 1.8 + 32).ToString();
       //*/
     }
     finally
     {
       _updating = false;
     }
   }
}

private void txtFahrenheit_TextChanged_1(object sender, EventArgs e)
{
   if (!_updating)
   {
     try
     {
       _updating = true;
       ///*
       if (string.IsNullOrEmpty(txtFahrenheit.Text))
       {
         txtCelsius.Clear();
         return;
       }
       txtCelsius.Text = ((double.Parse(txtFahrenheit.Text)) / 1.8 - 32).ToString();
       //*/
     }
     finally
     {
       _updating = false;
     }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM