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?
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted May 21, 2013, 3:26 AM EDT
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
}
Sandeep Singh ShekhawatRank #147 · 15.6K reputationPosted May 21, 2013, 3:27 AM EDT
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);
}
Shankar MRank #874 · 2.5K reputationPosted May 21, 2013, 3:45 AM EDT
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