473,581 Members | 2,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I stop invalid characters being typed into a TextBox?

Hi

At first blush this seemed like it should be so simple. Guess again.

I want to only allow numeric characters and hexadecimal values in a TextBox,
anything else should be ignored, and maybe a beep be issued.

So I figure I need a KeyDown event and I just have to mark it as Handled,
right?

Wrong!!!!

So how do I accomplish this? I have all my controls in a UserControl BTW.

As a side note, you'd think Microsoft would have added a NumericOnly
property to their text fields by now.
How many times have I had to do numeric only validation, I've lost count.
Dec 1 '05 #1
4 3606
Hi,
"Tim Osborne" <Ti*********@xc ellenet.com> wrote in message
news:Og******** ******@tk2msftn gp13.phx.gbl...
Hi

At first blush this seemed like it should be so simple. Guess again.

I want to only allow numeric characters and hexadecimal values in a
TextBox,
anything else should be ignored, and maybe a beep be issued.

So I figure I need a KeyDown event and I just have to mark it as Handled,
right?

Wrong!!!!
I could bet I have done it this way, why it's not working with you? In what
platform are you in?
So how do I accomplish this? I have all my controls in a UserControl BTW.

As a side note, you'd think Microsoft would have added a NumericOnly
property to their text fields by now.
How many times have I had to do numeric only validation, I've lost count.
You are right in this one, not only that but it's enough to make public a
protected property
public class NumberTextBox : TextBox
{
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreatePara ms;
cp.Style |= 0x2000; // ES_NUMBER
return cp;
}
}
}

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Dec 1 '05 #2
Perhaps you shoud try manipulate with MaskedTextBox
PK

Dec 1 '05 #3
Ahhhhhh, well the first part of the solution is to do the checking in the
KeyPressed handler.

That way when you say e.Handled = true; it doesn't pass the event onwards.

Now I just have to figure out how to make a Beep sound.

Any ideas on that one?
"Tim Osborne" <Ti*********@xc ellenet.com> wrote in message
news:Og******** ******@tk2msftn gp13.phx.gbl...
Hi

At first blush this seemed like it should be so simple. Guess again.

I want to only allow numeric characters and hexadecimal values in a
TextBox,
anything else should be ignored, and maybe a beep be issued.

So I figure I need a KeyDown event and I just have to mark it as Handled,
right?

Wrong!!!!

So how do I accomplish this? I have all my controls in a UserControl BTW.

As a side note, you'd think Microsoft would have added a NumericOnly
property to their text fields by now.
How many times have I had to do numeric only validation, I've lost count.

Dec 1 '05 #4
> Now I just have to figure out how to make a Beep sound.
using System.Runtime. InteropServices ;

[DllImport("kern el32.dll")]
private static extern bool Beep(int freq, int dur);
Beep(3000, 200);
Dec 2 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4236
by: Peter | last post by:
Hello: How can I validate the character encoding of the textbox in a form? For instance, when the user fills out a form, I need to be sure that he is typing in English (I mean uses Latin characters - not Russian, Hebrew etc.). Do you know any JS code sample that I could use? I would appreciate your help. Thank you,
2
13692
by: anonieko | last post by:
This applies to javascript dynamic textbox onkey > > > Newsgroups: comp.lang.javascript From: Lasse Reichstein Nielsen <l...@hotpop.com> - Find messages by this author Date: Fri, 15 Jul 2005 18:39:24 +0200 Local: Fri,Jul 15 2005 12:39 pm Subject: Re: Summer 2005 browsers' test set ?
2
2892
by: Xam | last post by:
Hello everybody Do you know of a javascript routine that can warn if there are any pre-defined invalid chars in the filename of an INPUT file box before it is submitted with the submit button. The process would be: a) User clicks the INPUT File's Browse button to select the file from their computer.
7
1504
by: supster | last post by:
I'de like to ask users if they are able to save their work before they close using the normal "Save? Yes, No, Cancel" type of message box. I've got this so far: private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if(unSaved) {
11
11129
by: Ron L | last post by:
I have a barcode scanner which uses a "keyboard wedge" program so that the data it scans comes through as if it was typed on a keyboard. I am trying to have the data in the barcode be displayed in a text box. I know that there are certain control characters embedded in the data that I want to display substitutions for in the text box, for...
13
20730
by: titan nyquist | last post by:
How do you test a string to see if it contains special characters? I want to ensure that any names typed into my form has only letters (and maybe allow a dash and an apostrophe). I can loop RealName.Contains("..."), but there must be a more elegant solution.
6
2855
by: jasmel | last post by:
Hello, Sorta new with JavaScript... so don't kill me with jargon. I am trying to make a script that will only allow text to be inputted into a textarea that is from a drop down menu. My problem is the textarea/textbox id is pre-set and unchangeable by the shopping cart system I use. (*Changing it will make it un-upgradeable in the future) ...
1
14962
by: JB | last post by:
Hi All, I'd like to display password characters in a DataGridView column, the same way I can do in a standard TextBox by using the PasswordChar property (i.e.typed characters replaced by a star for instance). Can anyone tell me how? Thanks JB
6
23370
by: uicouic | last post by:
I have a textbox named "txtName" and a button (btnSave) on a webform. After I have typed the illegal characters into the textbox and click "Save", I would like the webform to check for those characters and display a message, "Illegal Characters not allowed". How do I achieve that in JavaScript? Sorry if it seems too simple as I am kinda weak...
0
7882
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7808
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
8157
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
8312
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
7914
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
6564
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5683
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
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
3809
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...

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.