Setting the focus to a specific text box and other woes
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.
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
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 anif statement is?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Uh sure I know what if statements are but I'm still lost...
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
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.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
yeah i thought about that, was just wondering ifn there was some built-in method that could do it for me xP.
Thanks though
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
I don't think there is a built in function which will 100% validate all entries.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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
pygmalion
Junior Poster in Training
71 posts since Dec 2006
Reputation Points: 12
Solved Threads: 1
actually, there is.
Not 100% fool proof. Like I said you have to code your own me thinks.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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.
Lardmeister
Posting Virtuoso
1,749 posts since Mar 2007
Reputation Points: 407
Solved Threads: 44
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
pygmalion
Junior Poster in Training
71 posts since Dec 2006
Reputation Points: 12
Solved Threads: 1
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
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140