There are several methods (KeyPress handling, SHLimitInput, ...), but you should use a NumericUpDown
Help, how to Make textbox accept numbers only windows form c++
how to Make textbox accept numbers only windows form c++
Developer technologies C++
3 answers
Sort by: Most helpful
-
-
Minxin Yu 13,501 Reputation points Microsoft External Staff
2021-12-22T01:39:31.027+00:00 Hi, @Hesham AbdMalek Ramadan
Try to use"[^0-9.]" // added one point
if (System::Text::RegularExpressions::Regex::IsMatch(textBox1->Text, "[^0-9.]"))
{
MessageBox::Show("Please enter only numbers.");
textBox1->Clear();
}Best regards,
Minxin Yu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. -
Minxin Yu 13,501 Reputation points Microsoft External Staff
2021-12-21T05:35:13.1+00:00 You could try to use the snippet below in textbox keypress event. It can only accept the input of numbers 0-9, backspace and decimal point.
8 stands for backspace,
48 stands for number 0,
57 stands for number 9,
46 stands for decimal point.private: System::Void textBox1_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) { if (!(e->KeyChar == 8 || (e->KeyChar >= 48 && e->KeyChar <= 57) || e->KeyChar == 46)) { e->Handled = true; } }
Best regards,
Minxin Yu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.