How to: Convert the Text in a TextBox Control to an Integer
When you provide a TextBox control in your application to retrieve a numeric value, you often have to convert the text (a string) to a numeric value, such as an integer. This example demonstrates two methods of converting text data to integer data.
Example
int anInteger;
anInteger = Convert.ToInt32(textBox1.Text);
anInteger = int.Parse(textBox1.Text);
Compiling the Code
This example requires:
- A TextBox control named textBox1.
Robust Programming
The following conditions may cause an exception:
The text converts to a number that is too large or too small to store as an int.
The text may not represent a number.
See Also
Concepts
Designing a User Interface in Visual C#