C# 当文本框中没有数字时,C程序崩溃
我有一个C语言的WPF应用程序,对于我的一个文本框,输入被获取,然后自动转换成摄氏温度到华氏温度。当您输入一个数字时,它工作正常,但一旦输入的数字的所有数字被删除,程序就会崩溃。我猜这是因为输入格式是“无效的”,因为它只是试图转换什么? 我很困惑如何解决这个问题,任何帮助都将不胜感激,谢谢 这是我在应用程序中的代码:C# 当文本框中没有数字时,C程序崩溃,c#,validation,C#,Validation,我有一个C语言的WPF应用程序,对于我的一个文本框,输入被获取,然后自动转换成摄氏温度到华氏温度。当您输入一个数字时,它工作正常,但一旦输入的数字的所有数字被删除,程序就会崩溃。我猜这是因为输入格式是“无效的”,因为它只是试图转换什么? 我很困惑如何解决这个问题,任何帮助都将不胜感激,谢谢 这是我在应用程序中的代码: private void tempC_TextChanged(object sender, TextChangedEventArgs e) { tempC.MaxLengt
private void tempC_TextChanged(object sender, TextChangedEventArgs e)
{
tempC.MaxLength = 3;
Temperature T = new Temperature(celsius);
T.temperatureValueInCelcius = Convert.ToDecimal(tempC.Text);
celsius = Convert.ToDecimal(tempC.Text);
T.ConvertToFarenheit(celsius);
tempF.Text = Convert.ToString(T.temperatureValueInFahrenheit);
}
这是我创建的API中的代码:
public decimal ConvertToFarenheit(decimal celcius)
{
temperatureValueInFahrenheit = (celcius * 9 / 5 + 32);
return temperatureValueInFahrenheit;
}
如果无法进行转换,则应调用尝试转换值和信号的方法
if(Decimal.TryParse(tempC.Text, out celsius))
{
// Value converted correctly
// Now you can use the variable celsius
}
else
MessageBox.Show("The textbox cannot be converted to a decimal");
如果无法进行转换,则应调用尝试转换值和信号的方法
if(Decimal.TryParse(tempC.Text, out celsius))
{
// Value converted correctly
// Now you can use the variable celsius
}
else
MessageBox.Show("The textbox cannot be converted to a decimal");
试试这个:
private void tempC_TextChanged(object sender, TextChangedEventArgs e)
{
if(tempC.Text = "")
return;
tempC.MaxLength = 3;
Temperature T = new Temperature(celsius);
T.temperatureValueInCelcius = Convert.ToDecimal(tempC.Text);
celsius = Convert.ToDecimal(tempC.Text);
T.ConvertToFarenheit(celsius);
tempF.Text = Convert.ToString(T.temperatureValueInFahrenheit);
}
试试这个:
private void tempC_TextChanged(object sender, TextChangedEventArgs e)
{
if(tempC.Text = "")
return;
tempC.MaxLength = 3;
Temperature T = new Temperature(celsius);
T.temperatureValueInCelcius = Convert.ToDecimal(tempC.Text);
celsius = Convert.ToDecimal(tempC.Text);
T.ConvertToFarenheit(celsius);
tempF.Text = Convert.ToString(T.temperatureValueInFahrenheit);
}
试试Decimal.TryParse
这里有一些例子
string value;
decimal number;
// Parse a floating-point value with a thousands separator.
value = "1,643.57";
if (Decimal.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// Parse a floating-point value with a currency symbol and a
// thousands separator.
value = "$1,643.57";
if (Decimal.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// Parse value in exponential notation.
value = "-1.643e6";
if (Decimal.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// Parse a negative integer value.
value = "-1689346178821";
if (Decimal.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// The example displays the following output to the console:
// 1643.57
// Unable to parse '$1,643.57'.
// Unable to parse '-1.643e6'.
// -1689346178821
试试Decimal.TryParse
这里有一些例子
string value;
decimal number;
// Parse a floating-point value with a thousands separator.
value = "1,643.57";
if (Decimal.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// Parse a floating-point value with a currency symbol and a
// thousands separator.
value = "$1,643.57";
if (Decimal.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// Parse value in exponential notation.
value = "-1.643e6";
if (Decimal.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// Parse a negative integer value.
value = "-1689346178821";
if (Decimal.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
// The example displays the following output to the console:
// 1643.57
// Unable to parse '$1,643.57'.
// Unable to parse '-1.643e6'.
// -1689346178821
尝试使用Decimal.TryParse。如果转换只是为了在UI中显示而没有业务需要,请研究使用ValueConverter。尝试使用Decimal.TryParse。如果转换只是为了在UI中显示而没有业务需要,请研究使用ValueConverter。您是否缺少一个out参数?确认。对TryParse真是一个讨厌的东西。@RobertHarvey你真的不喜欢它:-@Steve:回想起来,所有的try方法可能都应该返回一个元组,但元组当时并不存在。你是不是遗漏了一个out参数?确认。对TryParse真是个讨厌的东西。@RobertHarvey你真的不喜欢它:-@Steve:回想起来,所有的try方法都应该返回一个元组,但元组在当时并不存在。