473,548 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Validat ing 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 4255
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.or g> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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.Validat ing 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
18329
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 still gets entered. I've stripped the function down just to try to get it to work in Firefox. The alert does happen on the keydown event so it...
3
5563
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 database. Click on button 2 (= cancel button) = Redirect with Response.Redirect("AnotherPage.aspx") Now I have the following problem. When I click...
3
2397
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 from the current page and go to the previous page, the validation controls activate because data has not been entered, and the user cannot cancel. I...
1
2823
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 user and has a validator (reDate) attached for specific dates that can be entered. The second date (txtRptDate) is another textbox that is filled...
2
1571
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 Update or Cancel events seem to be firing and I am at a lost to explan why. My code is below, if any one can tell me what I am doing wrong then that...
21
9137
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 the cancel button, the textbox.validating is being called. I don't want it to be since they are exiting the screen the validation doesn't have to be...
8
2192
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 a table to see if that number has been entered. If it has not, then I want to warn the user and clear the data. If the number does exist in the...
5
21267
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 requires you to explicitly state all conversions. --
5
8892
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 message in the textbox validate it seems the mousedown event is never executed. In VB.Net it executes the textbox validation and then displays...
0
7438
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.