Deleting number from text box produces error

César Amorim

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);
}
Eric J.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Deleting a number from an array

From Dev

Deleting text from text file

From Dev

How to edit data from rich text box without deleting the current data in c#

From Dev

Java Script Get Number of index for array from text box

From Dev

Deleting a number from an array in PHP

From Dev

Deleting strings from text file

From Dev

Deleting a record from text file

From Dev

error in fetching splitted string from rich text box

From Dev

Format a text box as a number in a report

From Dev

text into a text box from a query

From Dev

How to validate text and number within text box?

From Dev

error message inside text box

From Dev

Inputting data from a drop box into a text box?

From Dev

Calling ghostscript from a script produces error

From Dev

Python Printing Elements from a dictionary produces error

From Dev

Preserve number format when copying very many digits from a Text Box

From Dev

Deleting text from a LaTeX file with sed

From Dev

Disabling user from deleting text in tkinter scrolledtext

From Dev

Deleting specific lines from text file

From Dev

deleting data from text file (c programing)

From Dev

Disabling user from deleting text in tkinter scrolledtext

From Dev

deleting a text label from image imagej

From Dev

Deleting generalised strings from a text file

From Dev

Deleting a tag from text in Notepad++

From Dev

Android Invalid Int error when parsing number from text file

From Dev

jqGrid pagination page number text box not updating

From Dev

Number formatting for PHP generated text box

From Dev

Required a number in text box in Inno Setup

From Dev

Number formatting for PHP generated text box

Related Related

HotTag

Archive