Answer the question
In order to leave comments, you need to log in
Checking TextBox for numbers?
Recently there was a task to do work in Windows Form, I didn’t really work with them, so I ran into such a task. There is a TextBox, it is necessary to check whether the user entered at least something and whether the entered data is numbers, if not, then it is desirable to display the MassageBox with an error, or simply display an error in the same TextBox.
I did it very stupidly, just like this:
String^S1 = textBox1->Text;
if (S1 == "") {
MessageBox::Show("Вы не ввели значение А. Значение А принято как 0", "Ошибка!");
}
else {
a = Convert::ToInt32(S1);
}
Answer the question
In order to leave comments, you need to log in
In my student years, I wrapped it in a try catch:
If the converter collapses, then it was not a number, the conversion rolled, then a number.
Enough for educational needs.
Why not use NumericUpDown ?
If using a TextBox is essential, then you can use, in addition to the above, Regex.IsMatch(input, @"^\d+$")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question