This documentation is archived and is not being maintained.

How to: Convert the Text in a TextBox Control to an Integer

Visual Studio 2008

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.

int anInteger;
anInteger = Convert.ToInt32(textBox1.Text);
anInteger = int.Parse(textBox1.Text);

This example requires:

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.