By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,433 Members | 776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,433 IT Pros & Developers. It's quick & easy.

Verify/cancel entered text in textbox

P: n/a
What I want to be able to do:

A textbox is available that the user can enter information into.
Specifically (for the purposes of this post), the user is asked to
enter a number, and that number has an upper limit. I want to do
validation on what the user enters as they type it in.

I set up an event handler for KeyPress that restricts them from being
able to enter non-numeric characters (only allowing numbers, decimal
point, control keys (for backspace)), and setting e.Handled to true to
stop any further processing.

Setting up a textbox.Validating event is not desirable since that
requires that 1) the user leaves the textbox, and 2) that the user
activates a part of the dialog that will trigger a Validation attempt.
Since that is not guaranteed, this is not an optimal way of handling
this.

Once data is entered, I need to be sure that the data itself is valid.
If the current limit on the number is 1000, they can't enter 1001. I
can do this check either at the KeyPress event or at the TextChanged
event. The KeyPress event is cancellable, and allows me to hold the
previous value. The TextChanged event is not cancellable (that I can
tell), and does not allow rollback to the previous value if the new
data is determined to be invalid. Therefore my first target is
validating during the KeyPress event.

Validating during the KeyPress event, I find that there's a problem. I
cannot assume that the new key value will be appended to the existing
text. If the current text is selected (in part or in whole), there
seems to be no way for me to determine what the new value will be. I
can find what the selected text is, but I can't tell what portion of
the original text is selected. Supposing the current text is 1000, if
the selected text is '0' and the user enters '5', will the new value is
1500, 1050, or 1005? If no text is selected, is the new value 51000,
15000, 10500, 10050, or 10005? Thus it appears that I cannot validate
the newly entered value from this event and must move to the
TextChanged event.

Once back in the TextChanged event, I'm presented with the dilemma of
not knowing what the previous value was (and thus not able to restore
to that), nor being able to cancel the change outright. I could,
perhaps, keep an internal variable to store the old value, but in that
case I'm going to start needing numerous redundant variables in order
to track the "last value" of several textboxes. That's the only way I
can think of to handle it, though.

Is there a way around this problem?

Thank you,
David Smith

Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Why not just use a NumericUpDown control, or any number of available
third-party masked edit or numeric only text box controls?

DalePres
MCAD, MCDBA, MCSE

"David Smith" <ds****@iftd.org> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
What I want to be able to do:

A textbox is available that the user can enter information into.
Specifically (for the purposes of this post), the user is asked to
enter a number, and that number has an upper limit. I want to do
validation on what the user enters as they type it in.

I set up an event handler for KeyPress that restricts them from being
able to enter non-numeric characters (only allowing numbers, decimal
point, control keys (for backspace)), and setting e.Handled to true to
stop any further processing.

Setting up a textbox.Validating event is not desirable since that
requires that 1) the user leaves the textbox, and 2) that the user
activates a part of the dialog that will trigger a Validation attempt.
Since that is not guaranteed, this is not an optimal way of handling
this.

Once data is entered, I need to be sure that the data itself is valid.
If the current limit on the number is 1000, they can't enter 1001. I
can do this check either at the KeyPress event or at the TextChanged
event. The KeyPress event is cancellable, and allows me to hold the
previous value. The TextChanged event is not cancellable (that I can
tell), and does not allow rollback to the previous value if the new
data is determined to be invalid. Therefore my first target is
validating during the KeyPress event.

Validating during the KeyPress event, I find that there's a problem. I
cannot assume that the new key value will be appended to the existing
text. If the current text is selected (in part or in whole), there
seems to be no way for me to determine what the new value will be. I
can find what the selected text is, but I can't tell what portion of
the original text is selected. Supposing the current text is 1000, if
the selected text is '0' and the user enters '5', will the new value is
1500, 1050, or 1005? If no text is selected, is the new value 51000,
15000, 10500, 10050, or 10005? Thus it appears that I cannot validate
the newly entered value from this event and must move to the
TextChanged event.

Once back in the TextChanged event, I'm presented with the dilemma of
not knowing what the previous value was (and thus not able to restore
to that), nor being able to cancel the change outright. I could,
perhaps, keep an internal variable to store the old value, but in that
case I'm going to start needing numerous redundant variables in order
to track the "last value" of several textboxes. That's the only way I
can think of to handle it, though.

Is there a way around this problem?

Thank you,
David Smith

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.