MalvEarp
MalvEarp

Reputation: 525

C# Program Crashes When No Number In TextBox

I have a WPF Application in C# and for one of my textboxes, the input is taken and then automatically converted(Celsius to Fahrenheit). When you input a number, it works fine, but then once all the digits of the inputted number are removed, the program crashes. I guess this is because the input format is 'invalid' because it's just trying to convert nothing? I'm stumped on how to work around this, any help would be appreciated, thanks!

This is my code within the application:

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

and this is the code from the API I have created:

public decimal ConvertToFarenheit(decimal celcius)
{
    temperatureValueInFahrenheit = (celcius * 9 / 5 + 32);

    return temperatureValueInFahrenheit;
}

Upvotes: 1

Views: 584

Answers (4)

Robert Harvey
Robert Harvey

Reputation: 180858

private void tempC_TextChanged(object sender, TextChangedEventArgs e)
{
    Decimal temp;
    if (!Decimal.TryParse(out temp, tempC.Text))
       return;
    ...

Upvotes: 2

saeed
saeed

Reputation: 2497

Try Decimal.TryParse Here is some examples

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      

Upvotes: 0

Waqar
Waqar

Reputation: 9366

Try this :

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

Upvotes: 0

Steve
Steve

Reputation: 216313

You should call the method Decimal.TryParse that tries to convert the value and signal if the conversion is not possible.

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");

Upvotes: 5

Related Questions