473,770 Members | 4,553 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 3624
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
4264
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
13726
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
2908
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
1512
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
11187
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 instance I want to replace ASCII character 04 with the string "<EOT>". I have tried doing a simple...
13
20783
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
2864
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) Here is what I have so far: <html> <head>
1
15001
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
23390
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 in programming. Thanks.
0
9602
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10071
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6690
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.