converting textbox.text to real number

  • Thread starter Thread starter Homer Simpson
  • Start date Start date
H

Homer Simpson

How do I convert a value in a textbox to a real number so I can perform some
math ops on it? I understand I will need to validate the textbox's input to
make sure it is a real number and not a string so any suggestions along
those lines are also appreciated.

Thanks,
Scott
 
OK, I did some more searching and found I can use the Convert class
(convert.todouble) to convert the string to a double. It works but is this
the best way?

I'm still looking for how to validate the input so any help there is
appreciated.

Thanks,
Scott
 
Hi Scott,

Convert will thrown an exception if it fails. You can use

bool success = double.TryParse(...)

You can also limit the TextBox input to numbers only + any special characters like decimal point etc. Handle the TextBox.KeyPress event for this.
 
Back
Top