0

I have a TextBox in which I wanna place a number, the program is supposed to read it, by converting it into a decimal, however, after performing the needed maths with the number, if I delete it from the TextBox, it immediatly produces an error:

Format exception was unhandled ( input string in a incorrect format)

this happens on the line on which I try to convert the text into a decimal

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    _Wd = Convert.ToDecimal(_W);
}
Dmitry
  • 13,797
  • 6
  • 32
  • 48
César Amorim
  • 367
  • 3
  • 16

3 Answers3

2

You get

Format exception was unhandled ( input string in a incorrect format)

because string.Empty cannot be converted to a decimal.

You can use TryParse to inform you if parsing fails:

bool success = decimal.TryParse(_W, out _Wd);
if (success) {
    // Use the result
}
else {
    // Depending on your needs, do nothing or show an error
}

Note that _W being string.Empty may be a condition you want to ignore, while other parse failures might warrant an error message. If so, your else might look like

else {
    if (!string.IsNullOrEmpty(_W)) ShowAnErrorMessageSomehow();
}
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Using the result means "_Wd = Convert.ToDecimal(_W);", if it becomes a empty box, the program will do nothing, instead of providing an error, is this correct? – César Amorim Apr 18 '14 at 21:13
  • Use `decimal.TryParse` *instead of* `Convert.ToDecimal()`. `decimal.`TryParse()` returns *false* if parsing fails, whereas `Convert.ToDecimal()` throws an Exception. For ordinary program flow control (e.g. user enters no text), it is preferable to check a boolean result than to catch an exception. In the code I have shown, `Wd` will contain the parsed decimal value if `success` is *true*. – Eric J. Apr 18 '14 at 21:18
1

Sounds like you're making it so the number can't be converted into a decimal. Unsurprisingly, this causes the conversion to fail. Try using Decimal.TryParse instead:

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    Decimal.TryParse(_W, out _Wd);
}

This will prevent the exception if the conversion fails. It will also return a bool, which you can use to perform other operations conditionally only when the conversion succeeds, e.g.:

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    if(Decimal.TryParse(_W, out _Wd))
    {
        Console.WriteLine("Valid decimal entered!");
    } 
}
Ant P
  • 24,820
  • 5
  • 68
  • 105
  • But when i use " Decimal.TryParse(_W, out _Wd); " it will convert the text into decimal just as the previous code, only with the added function of TryParse, is this correct? – César Amorim Apr 18 '14 at 21:10
  • @CésarAmorim You don't need to use `Convert.ToDecimal` at all - TryParse does the same conversion as `Convert.ToDecimal`, except that it won't throw `FormatExceptions` and the return value is a `bool` telling you whether or not the conversion was successful. – Ant P Apr 18 '14 at 21:13
0

Please try this code. But make sure that user can type only numbers in the textbox. Thanks.

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    _Wd = Convert.ToDecimal("0"+_W);
}
Tharindu Welagedara
  • 2,660
  • 18
  • 27