I'm mechanical engineering graduate student in Portugal. I'm familiarized with c++ but I've been having problems with the windows forms.

I'm trying to make a form that would compile diferent data with inviscid fluid flow formulas but I've got one major problem.

I've done hundreds of google searches and I haven't found any code for a c++ text box tht would accept numeric values only.

Can someone help with this last part?

I' using visual c++ 9.0 express edition by the way.


6 Years
Discussion Span
Last Post by franchico

Perhaps something like,

Int32 myInt = 0;
if ( System::Text::RegularExpressions::Regex::IsMatch(myTextBox->Text, 
	"^[-0-9]*.[.0-9].[0-9]*$") )
	myInt = System::Convert::ToInt32 ( myTextBox->Text );				
	MessageBox::Show("Not a number");

This is an excerpt from another message board on the same topic.



thanks a lot,

still there must be one tiny mistake within your code because it always displays "Not a number", whether the input is a number or not.


I'm not sure - I don't use Visual C++. I'm sure someone with more experience will hop on soon.


I suspect the format of the RegEx is not correct, but it's been so long since I've worked with them I can't be sure.

AD is usually on about now, I'm sure he'll be along with a solution soon.


Hello Paul
To make a textbox only accept only numric characters do the following:
1) Bring up the design mode of the form that has the textbox that you want to make numeric only.
2) Click on the textbox to select it.
3) In the "Properties" window click on the "Events" button.
(The one with the lightening bolt)
4) Look for the "KeyPress" event and double click on it.
This will create a KeyPress event for the Textbox and take you to the code window at the newly create event.
5)In this event handler enter the following code:

    // Accept only digits and the Backspace character
    if(!Char::IsDigit(e->KeyChar) && e->KeyChar != 0x08)
	e->Handled = true;

Now when you run your code and try to type anything but a digit or the Backspace,
the key will be completely ignored.
The Backspace is needed for editing of the textbox.


Edited by Milton Neal: n/a



thanks a lot Milton, it's working marvellously.

Still there's one thing missing. The primary objective o my program is to compute inviscid flow formulas and for that I need to use the decimal point.

How can I had the decimal point to the textbox?


Hello Paul
Of course it is not a problem to include a decimal point in the filtering. Only it requires a little more logic. A couple of issues need to be addressed.
Firstly it must restrict the user to one decimal point.
Secondly if a decimal point is in any selected text within the textbox then we should allow the decimal point because the selected text will be replaced with the completion of the key stroke.
The following code should address these issues and work as you would like.
Replace the code I posted earlier with the following:

 // Only allow 1 decimal point
 if(e->KeyChar == '.')
  if(this->YOURTEXTBOX->Text->Contains(".") && !this->YOURTEXTBOX->SelectedText->Contains("."))
      e->Handled = true;
 // Accept only digits "." and the Backspace character
 else if(!Char::IsDigit(e->KeyChar)&& e->KeyChar != 0x08)
      e->Handled = true;

Edited by Milton Neal: missing text


Hi Milton,

it worked great thanks!

Thank you all for your help.

In my case I had to switch the dot "." for a comma "," in order for it to work but that's problably because I'm using a Portuguese version of Windows 7.

Thanks again


This question has already been answered. Start a new discussion instead.
Please be thoughtful and detailed and be sure to adhere to our posting rules.