1: private void button1_Click(object sender, EventArgs e)
2: {
3: int Result = 0;
4: try
5: {
6: string inputString = textBox1.Text;
7: Result = Int32.Parse(inputString);
8: label3.Text = Result.ToString();
9: }
10: catch (Exception Ex)
11: {
12: MessageBox.Show(Ex.Message);
13: }
14: }
Where the textbox value is "0.0". It returns the Exception "Input string was not in correct format". For all other values it returns the equivalent Integer value including '0'.
How to Solve this problem?
Use the below code..
1: private void button1_Click(object sender, EventArgs e)
2: {
3: int Result = 0;
4: try
5: {
6: string inputString = textBox1.Text;
7: Result = Int32.Parse(inputString,NumberStyles.AllowDecimalPoint);
8: label3.Text = Result.ToString();
9: }
10: catch (Exception Ex)
11: {
12: MessageBox.Show(Ex.Message);
13: }
14: }
here NumberStyles is as Enumerator available in System.Globalization namespace.
Caution: Pass only strings with decimal points as argument
Enjoy!
Hope you guys got a quick simple way to get around the exception..
ReplyDeleteHave a nice day!
good one, continue giving tips like this
ReplyDelete