Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi,

I'm making a windows forms application. Throughout I have a lot of text boxes. Something I put off until now was checking the user input is a valid input.

I tried the following (taken from elsewhere):

if (! System::Text::RegularExpressions::Regex::IsMatch(this->textBox1->Text, "^[0-9afA-F]{1,3}$" ))
{
	MessageBox::Show ( "Address 2: Invalid value" );
}
else
{
	this->textBox2->Text = this->textBox1->Text;
}


But it doesn't really work as I want and I dont understand it. I want one decimal point allowed, sometimes spaces, some textboxes cant have them though, and numbers for all textboxes but no characters or other special characters like & or $.

The way I first tried was to take the text, input it to a string, read it's last input, and if the text wasnt allowed, delete the last textbox string element:

int lastChar = myStr->Length - 1; 
if( ! Char::IsDigit(myStr, lastChar))
{
	myStr = myStr->Remove(lastChar);
	this->textBox1->Text->Remove(lastChar);
}


But for some reason it doesnt actually delete it. What happens is it temporarily removes the last element but the next user input is then omitted with the first illegal input now allowed. Reiterating:

Input1 : 010f
Output1: 010
Input2 (next keystroke input): 010fa
Output2: 010f

What I need is to actually eradicate this evil input. Help me :)
Posted
Updated 21-Mar-13 6:54am
v3
Comments
Sergey Alexandrovich Kryukov 21-Mar-13 12:52pm    
Tag it properly, to avoid confusion: C++/CLI, not "C++". And tag the UI library you use.
—SA

1 solution

First of all, tag your question properly. Then, to start with, see my recent answer: number must be numeric up to 9 digits[^].

Read the whole thread; it will give you some good ideas. Please ask further questions by commenting this post, in case of any doubt.

—SA
 
Share this answer
 
Comments
lostandconfused1234 21-Mar-13 13:06pm    
I see what you mean. The text isnt actually omitted from behind the scenes. I dont understand how to implement the code you are referencing though.
Sergey Alexandrovich Kryukov 21-Mar-13 13:13pm    
Then you should explain what part is not clear. Do you know how to derive classes, override? How to handle events?
—SA
lostandconfused1234 21-Mar-13 13:12pm    
System::Windows::Input::TextCompositionEventArgs.Handled doesnt seem to be an option...

it goes System::Windows::Forms:: and then I have no idea what function to use :(
Sergey Alexandrovich Kryukov 21-Mar-13 13:15pm    
I see now. In System::Windows::Forms::TextBox, the event is KeyPressed, or override the OnKeyPressed method. Instead of handle, use eventArgs.Cancel = true; (this field is available for this and other events, not all of them).

Now you know the key point. If in doubt, you are welcome to ask me to add the detail, but I hope you will see the solution already.

Good luck,
—SA
lostandconfused1234 21-Mar-13 13:18pm    
Yeah I'm writing it all inside the event args of the windows forms app for the textboxes. I just want to stop users being able to input an invalid input but it keeps messing up every method I try. I dont know a lot about the inner workings of the windows forms to override functions

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900