简体   繁体   English

C#温度转换器

[英]C# Temperature Converter

I am trying to create a temperature converter and everything is working great. 我正在尝试创建一个温度转换器,并且一切正常。

    private void hsbTemperature_Scroll(object sender, ScrollEventArgs e)
    {
        TempFah = hsbTemperature.Value;
        txtCelsius.Text = Convert.ToString(TempFah);
        TempCel = Convert.ToInt32((TempFah - 32) * 5 / 9);
        txtFahrenheit.Text = TempCel.ToString();
    }

    private void hsbTemperature_ValueChanged(object sender, EventArgs e)
    {
        TempFah = hsbTemperature.Value;
        txtCelsius.Text = Convert.ToString(TempFah);
        TempCel = Convert.ToInt32((TempFah - 32) * 5 / 9);
        txtFahrenheit.Text = TempCel.ToString();
    }
}

However, when i try to change the value for the Fahrenheit textbox,the value of Celsius's textbox does not change and vise-versa. 但是,当我尝试更改华氏文本框的值时,摄氏文本框的值不会更改,反之亦然。 The values for both of the textboxes only change when i move the scrollbar. 仅当我移动滚动条时,两个文本框的值才会更改。

So what i require is that when i change the value for my Fahrenheit i want the value for Celsius to also change and vise-versa. 所以我需要的是,当我更改华氏温度的值时,我希望摄氏温度的值也要发生变化,反之亦然。

Please help, Thank You. 请帮忙,谢谢。

Here's what I would do: 这就是我要做的:

It looks like you already have these class level properties defined: 看来您已经定义了以下类级别的属性:

private int TempFah;
private int TempCel;

Also, you don't need to put any code in the Scroll event. 另外,您无需在Scroll事件中放入任何代码。 ValueChanged is really what we care about, and it will get called right after Scroll anyway. ValueChanged确实是我们关心的问题,无论如何在Scroll之后都将调用它。

private void hsbTemperature_Scroll(object sender, ScrollEventArgs e)
{
    // Don't use this method when you really care about the value
}

So that we don't end up in any infinite loops of property change events triggering each other, I created a private bool variable to keep track of whether we are updating the text fields programmatically or not (as opposed to the user doing it). 为了避免出现属性更改事件彼此触发的无限循环,我创建了一个私有的bool变量来跟踪我们是否正在以编程方式更新文本字段(与用户执行此操作相反)。 If this is set to true, then we don't need to process any code in the change events because the UpdateTempFields method (coming up) is already doing it. 如果将其设置为true,那么我们不需要在change事件中处理任何代码,因为UpdateTempFields方法(即将出现)已经在执行此操作。

private bool programIsUpdatingProperties = false;

Just to help separate out the code into re-usable parts, we can create a separate function to get the Celsius value from a Fahrenheit value. 只是为了帮助将代码分成可重复使用的部分,我们可以创建一个单独的函数来从华氏值中获取摄氏度值。 And while we're at it, make one that does the opposite: 而当我们在做它时,做一个相反的事情:

private int GetCelciusValue(int fahrenheitValue)
{
    return (fahrenheitValue - 32) * 5 / 9;
}

private int GetFahrenheitValue(int celsiusValue)
{
    return celsiusValue * 9 / 5 + 32;
}

Now it's time to create the UpdateTempFields method which, given a Fahrenheit temperature, knows how to update all the control values on the form. 现在是时候创建UpdateTempFields方法了,在给定华氏温度的情况下,该方法知道如何更新表单上的所有控件值。

One trick to ensure that we don't over-set the hsbTemperature.Value (the user could enter one that's too big or small) is to use the smallest of either the hsbTemperature.Maximum or the number, and to ensure we don't under-set it, we get the maximum value of either the hsbTemperature.Minimum or the number. 确保我们不要过度设置hsbTemperature.Value (用户可以输入太大或太小的值)的一种hsbTemperature.Maximum是使用hsbTemperature.Maximum或数字中的hsbTemperature.Maximum ,并确保不将其设置为最小值,我们将获得hsbTemperature.Minimum或最大值的最大值。 This is done below using Math.Max and Math.Min below: 这是使用下面的Math.MaxMath.Min完成的:

private void UpdateTempFields(int newFahrenheitTemp)
{
    // Let the rest of the code know this is us, not the user
    programIsUpdatingProperties = true;

    TempFah = newFahrenheitTemp;
    TempCel = GetCelciusValue(TempFah);

    txtCelsius.Text = TempCel.ToString();
    txtFahrenheit.Text = TempFah.ToString();

    // Ensure we don't try to set the scroll bar to a value it can't handle
    hsbTemperature.Value =
        Math.Max(hsbTemperature.Minimum,
            Math.Min(hsbTemperature.Maximum, TempFah));

    // Carry on as normal now...
    programIsUpdatingProperties = false;
}

Now, in the change events for our controls, all we have to do is call the UpdateTempFields() method with a Fahrenheit value, and it will take care of the rest. 现在,在控件的更改事件中,我们所要做的就是调用带有Fahrenheit值的UpdateTempFields()方法,其余的将由它来处理。 But we only want to do this if the user is the one changing the value, so check our flag first: 但是,我们只想在用户更改值的情况下执行此操作,因此请首先检查我们的标志:

private void txtFahrenheit_TextChanged(object sender, EventArgs e)
{
    if (!programIsUpdatingProperties)
    {
        int fahrValue;
        // Use int.TryParse to ensure it's a valid integer (if it's not, do nothing)
        if (int.TryParse(txtFahrenheit.Text, out fahrValue))
        {
            UpdateTempFields(fahrValue);
        }
    }
}

private void txtCelsius_TextChanged(object sender, EventArgs e)
{
    if (!programIsUpdatingProperties)
    {
        int celsiusValue;
        // Use int.TryParse to ensure it's a valid integer (if it's not, do nothing)
        if (int.TryParse(txtCelsius.Text, out celsiusValue))
        {
            // Convert celcius to fahrenheit first
            var fahrValue = GetFahrenheitValue(celsiusValue);
            UpdateTempFields(fahrValue);
        }
    }
}

private void hsbTemperature_ValueChanged(object sender, EventArgs e)
{
    if (!programIsUpdatingProperties)
    {
        UpdateTempFields(hsbTemperature.Value);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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