473,386 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Verify/cancel entered text in textbox

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
1 4240
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: ryanmhuc | last post by:
I have a function which needs to cancel the input into a text field on a form. I cancel the event in IE fine and the text does not get entered. But for firefox (1.0) I cancel the event and the text...
3
by: Mvk | last post by:
Hi; I have a simple aspx form. I have put a textbox and 2 buttons on it. The textbox is linked to a requiredfieldvalidator. Click on button 1 (= ok button) = Store value in textbox in...
3
by: MB | last post by:
Hi, I am doing a project which uses asp.net to develop its forms. The form uses validation web controls to validate the data entered in text boxes. When Cancel Button is pressed which is to exit...
1
by: TN Bella | last post by:
Hello, I was looking through the older posts for a solution to this problem, but I am still confused on how to fix my problem. There are two dates on my form, one (txtInvDate) is entered by the...
2
by: andrew.roberts | last post by:
This is my second post now on this subject has I seem to be getting no where and the problem is really starting to bug me now. I can get a datagrid into an editable state but then neither the...
21
by: Darin | last post by:
I have a form w/ a textbox and Cancel button on it. I have a routine to handle textbox.validating, and I have the form setup so the Cancel button is the Cancel button. WHen the user clicks on...
8
by: SBC News Groups | last post by:
I have a field on a form that when a user enters a number, I want to verify that the number is already in another table. For example: If a user enters 100 in the control Bidder, I want to check...
5
by: YardDancer | last post by:
Hi all I don't want to use the the Cint() or CDec() or other VB.Net Functions and I dont want to restrict keystrokes in the textbox. I am also using the "strict type semantic" settings that...
5
by: Steve | last post by:
In VB6 I have a screen the has a text box and a datagrid If I click the datagrid, the 1st event is to validate the textbox and then the 2nd event is a datagrid mousedown event. If a get an error...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.