How to see if the items that are in the text box are integer
Hello all how can i see if the container of the text box is in integer?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Shankar MPosted May 21, 2013, 7:45 AM
You can use the int.TryParse method to validate that the string in the Textbox is a valid
Integer value.
The int.TryParse method converts the given string Input to int.
It returns a value false, if the Input is not a valid one.
Thanks, Shankar M
Sandeep Singh ShekhawatPosted May 21, 2013, 7:27 AM
int Num;
bool isNum = int.TryParse(Str, out Num);
if (isNum)
MessageBox.Show(Num.ToString());
else
MessageBox.Show("Invalid number");
OR
bool IsNumber(string text)
{
Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
return regex.IsMatch(text);
}
VulpesPosted May 21, 2013, 7:26 AM
int i;
if (!int.TryParse(textBox1.Text, out i))
{
MessageBox.Show("Not an integer. Try again");
textBox1.Focus();
}
else
{
// do something with 'i' which we now know contains an integer
}