I have an event handler for the Leave event of a text box, that checks that the user has entered an invalid value. It displays a message and tells the user that the value entered is wrong.

But here's the trouble: after if displays the message, it still allows the user to move on to another control. How do I force the focus back on that textbox so that the Leave event keeps invoking until the user gets it right?

Another thing. I'm still new to C#, and I would like to know if there is some way I can check the contents of that text box to make sure that are all alpha-numeric (ie, only "0123456789" allowed)

Thanks.

Member Avatar for iamthwee

You could set all the other event handlers to be disabled... set the focus back on the text box.

I'm still new to C#, and I would like to know if there is some way I can check the contents of that text box to make sure that are all alpha-numeric (ie, only "0123456789" allowed)

You know what an if statement is?

Uh sure I know what if statements are but I'm still lost...

Member Avatar for iamthwee

Well the way I'd do it is to loop through the string, which is what you textBox will be.

And check if each character is "0123456789".

I guess you could use the substring function to isolate each character.

yeah i thought about that, was just wondering ifn there was some built-in method that could do it for me xP.

Thanks though

Member Avatar for iamthwee

I don't think there is a built in function which will 100% validate all entries.

actually, there is. why not use the KeyPress event?

add the following as an event of your textbox. (i have added the possibility for a 'space' or the use of 'backspace' for educational purposes - so you can see how it workd and edit it to the way you like.

private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            char key = e.KeyChar;
            if (((key < '0') || (key > '9')) && (key != '\b') && (key != ' '))
                e.Handled = true;
        }

hope this helped
pygmalion

Member Avatar for iamthwee

actually, there is.

Not 100% fool proof. Like I said you have to code your own me thinks.

Not 100% fool proof. Like I said you have to code your own me thinks.

Nothing is 100% fool proof, it depends on the fool! I would say go with pygmalion's solution, unless "know-it-all" iamthwee produces code.

Nothing is 100% fool proof, it depends on the fool! I would say go with pygmalion's solution, unless "know-it-all" iamthwee produces code.

Of course nothing is 100% foolproof, but I would still stick with iamthewee's solution.

The reason that his solution would work better (or rather more efficiently) is that it would be able to be applied to more variations of user input, i.e. somone copying and pasting text into the textBox. The other way you would have to check the user input twice. Once while they were typing and once after they invoke a completed inputting my input event (i.e. button click, image hover, etc...) to make sure they collected all the input.

iamthewee's solution covers an event like: user types the letters 'D', 'D' then pastes an 'o' then types an 'S'. Pygmalion's solution would only capture the 'DDS' and not the 'o', whereas iamthewee's would.

i agree to the fact that nothing is foolproof. everything can be exploited. the question is how hard it would be to do that.

iamthwee's solution is not bad at all, i just gave an interesting alternative. now, Lardmeister, i'm not sure how you can paste 'illegal' characters into a textbox which has a specific KeyPress-event. for example, you cannot paste non-numeric characters in the above given code. neither can you use on-screen keyboard and other easy work-arounds.

just wanted to make that point clear
pygmalion

And for your other problem:

if (textBox1.Text is equal to what you want)
{
// Move on
}
else
{
// Dont
}

Just replace that with your actual code. If you are holding a password however that is very insecure

Thank you all. I did get it worked out using methods similar to posted here (borriwing from what was psoted here mainly)

My fault: I should have marked this as solved.
Thanks, and please stop flaming

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.