Calculating textboxes

Page 1 of 1

1 Replies - 12036 Views - Last Post: 14 July 2010 - 01:56 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=181590&amp;s=e5cd267114546747555ba9f46d0e39a4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 qtex

Reputation: -5
• Posts: 142
• Joined: 24-October 09

Calculating textboxes

Posted 14 July 2010 - 01:42 PM

Hello,

I'm trying to calculate values what are in textboxes, but i got some error.

Error : Input string was not in a correct format.

Example:

TextBox8 value is = 400
TextBox12 value is = 0,000555555555555555

Code :

```TextBox4.Text = Val(TextBox8.Text) * Double.Parse(TextBox12.Text)
```

Anyone know what's the problem ? and how to fix it or other way to make it ?

thanks,

EDIT: I fixed it myself

This post has been edited by qtex: 14 July 2010 - 01:53 PM

Is This A Good Question/Topic? 0

Replies To: Calculating textboxes

#2 Frinavale

Reputation: 205
• Posts: 776
• Joined: 03-June 10

Re: Calculating textboxes

Posted 14 July 2010 - 01:56 PM

TextBoxes retrieve text from the user and you can access the data that the user provided using the TextBox's Text property (which returns a String).

Naturally a String cannot be treated as a number, and so you are using the Double.Parse method to convert the string into a Double (which is a number type) so that you can do calculations with it.

If the string provided by the user is not a number (say they enter "eight" instead of "8") then the Double.Parse method will throw and exception stating that it couldn't convert the string provided into a number.

That is the error you are getting.

"0,000555555555555555" is a string that cannot be converted into a Double....
"0.000555555555555555" is a string that can be converted into a Double.

You should add a Try Catch block so that you can handle cases when the user enters any invalid data.

For example:
```Dim number12 As Double
Try
number12 =  Double.Parse(TextBox12.Text)
Catch nullEx As ArgumentNullException
'nothing was provided
Catch formatEx As FormatException
'The string was not in the correct format
Catch ovEx As OverflowException
'The value provided was too big or too small to fit into the Double type
End Try
```

If you don't want any exceptions to be thrown then consider using the Double.TryParse() method instead. This method converts the string representation of a number into a Double, but instead of throwing an exception upon failing it returns a boolean value which indicates whether the conversion succeeded or failed. If you use the TryParse method you don't have to use a Try Catch block in your code...instead you can reduce it to an If Statement:

```Dim number12 As Double

If Double.Parse(TextBox12.Text, number12) Then
'do calculations
Else
'couldn't convert the string into a Double.
End If
```

If you are expecting numbers than you can filter the user's input into the TextBox to only accept valid characters (numbers). You can do this by handling the key press event for the TextBox or you could consider using a MaskedTextBox instead of a normal TextBox.

-Frinny

This post has been edited by Frinavale: 14 July 2010 - 01:59 PM