[英]C# Temperature Converter
我正在嘗試創建一個溫度轉換器,並且一切正常。
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();
}
}
但是,當我嘗試更改華氏文本框的值時,攝氏文本框的值不會更改,反之亦然。 僅當我移動滾動條時,兩個文本框的值才會更改。
所以我需要的是,當我更改華氏溫度的值時,我希望攝氏溫度的值也要發生變化,反之亦然。
請幫忙,謝謝。
這就是我要做的:
看來您已經定義了以下類級別的屬性:
private int TempFah;
private int TempCel;
另外,您無需在Scroll
事件中放入任何代碼。 ValueChanged
確實是我們關心的問題,無論如何在Scroll
之后都將調用它。
private void hsbTemperature_Scroll(object sender, ScrollEventArgs e)
{
// Don't use this method when you really care about the value
}
為了避免出現屬性更改事件彼此觸發的無限循環,我創建了一個私有的bool
變量來跟蹤我們是否正在以編程方式更新文本字段(與用戶執行此操作相反)。 如果將其設置為true,那么我們不需要在change事件中處理任何代碼,因為UpdateTempFields
方法(即將出現)已經在執行此操作。
private bool programIsUpdatingProperties = false;
只是為了幫助將代碼分成可重復使用的部分,我們可以創建一個單獨的函數來從華氏值中獲取攝氏度值。 而當我們在做它時,做一個相反的事情:
private int GetCelciusValue(int fahrenheitValue)
{
return (fahrenheitValue - 32) * 5 / 9;
}
private int GetFahrenheitValue(int celsiusValue)
{
return celsiusValue * 9 / 5 + 32;
}
現在是時候創建UpdateTempFields
方法了,在給定華氏溫度的情況下,該方法知道如何更新表單上的所有控件值。
確保我們不要過度設置hsbTemperature.Value
(用戶可以輸入太大或太小的值)的一種hsbTemperature.Maximum
是使用hsbTemperature.Maximum
或數字中的hsbTemperature.Maximum
,並確保不將其設置為最小值,我們將獲得hsbTemperature.Minimum
或最大值的最大值。 這是使用下面的Math.Max
和Math.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;
}
現在,在控件的更改事件中,我們所要做的就是調用帶有Fahrenheit值的UpdateTempFields()
方法,其余的將由它來處理。 但是,我們只想在用戶更改值的情況下執行此操作,因此請首先檢查我們的標志:
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.