473,406 Members | 2,352 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,406 software developers and data experts.

Only allow numbers in a textbox

I wish to restrict my textbox to only accepting numbers

I have looked the textbox KeyDown event and can inspect KeyEventArgs
parameter and interogate which key has been pressed, but I can't work out
how to stop then stop the character being added to the text box

TIA

Bisley
Nov 15 '05 #1
3 34221
"Bisley" <x> wrote in message news:eG**************@tk2msftngp13.phx.gbl...
I wish to restrict my textbox to only accepting numbers

I have looked the textbox KeyDown event and can inspect KeyEventArgs
parameter and interogate which key has been pressed, but I can't work out
how to stop then stop the character being added to the >text box


You can do this by creating your own textbox control that inherits from the
base: System.Windows.Forms.TextBox
and then you will have to override the method: WndProc to handle
WM_CHAR(=0x0102)..

a very simple example that you may extend is:

public class NumTextBox : System.Windows.Forms.TextBox
{
const int WM_CHAR = 0x0102;

private System.ComponentModel.Container components = null;
public NumTextBox()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitForm call

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case WM_CHAR:
if( !char.IsDigit((char)m.WParam) )
{
if((char)m.WParam != 8)//backspace allowness.
{
return ;
}
}

break ;
}
base.WndProc(ref m);
}
}

Nov 15 '05 #2
"Bisley" <x> wrote:
I wish to restrict my textbox to only accepting numbers

I have looked the textbox KeyDown event and can inspect KeyEventArgs
parameter and interogate which key has been pressed, but I can't work out
how to stop then stop the character being added to the text box

TIA

Bisley


Use "e.Handled = true;" to stop characters being accepted. e.g.:

private void textBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
const char Delete = (char)8;
e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
}
Nov 15 '05 #3
<"Bisley" <x>> wrote:
I wish to restrict my textbox to only accepting numbers

I have looked the textbox KeyDown event and can inspect KeyEventArgs
parameter and interogate which key has been pressed, but I can't work out
how to stop then stop the character being added to the text box


If you set the event's Handled property to true, that will stop the
character being added. However, I believe you should be handling the
KeyPress event, not KeyDown.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

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

Similar topics

9
by: Rowan Chapman | last post by:
Hey all! I'm kinda new to VB but not to programin'. So I know what it is like when you are asked trivial questions. Could some1 please tell me what the syntax would be 2 only allow numerical data...
2
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...
7
by: Greg Burns | last post by:
I have a textbox that I only want to allow "numbers". 1,000 1000 1000.0512 I don't want to prevent them from typing anything else, because I want to check their input in the Validating event...
12
by: patang | last post by:
I have written the following code which allows user to enter only numbers and only one decimal point. This works fine. However, I want that user shouldn't enter a third number after decimal...
8
by: Mike L | last post by:
In VB.NET Windows Form, how do I set my text box to only allow numbers?
7
by: Lars Netzel | last post by:
Hi How do I in a textbox only allow users to type in numbers and comma and dot ? they should of course be able to delete and backspace the stuff they type in too.. I figured keyDown would...
6
by: vandanasridhar | last post by:
hi, myself vandana. is there any property of textbox that can allow textbox to accept only numbers. i tried it by setting dataformatting property to number but nothing happens. thx in adv.
1
by: John Smith | last post by:
Hello, I'd like to create a function that will allow users to only enter numbers in some textboxes. I'm trying to create only one function and call it for each textbox.keypress event instead of...
2
Ajm113
by: Ajm113 | last post by:
I want to have a textbox that only allows Numbers, Periods, Spaces, and Minuses in it using C#. How is this accomplished? I already have the code set up like this: private void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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,...
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,...
0
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...
0
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...
0
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,...
0
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...

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.