473,467 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Only numeric value in textbox

Hello,
how can i do this:
Nov 17 '05 #1
5 28663
In message <#w**************@TK2MSFTNGP12.phx.gbl>, l@?.?.invalid writes
Hello,
how can i do this:


Subclass TextBox.

--
Steve Walker
Nov 17 '05 #2
If you are only going to do this in one place then you can do something like
this:

private void textBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)

{

e.Handled = !char.IsDigit(e.KeyChar);

}
Keep in mind the above code doesn't acct. for BackSpace, ., -, +, e

--
Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein

"Steve Walker" <st***@otolith.demon.co.uk> wrote in message
news:pJ**************@otolith.demon.co.uk...
In message <#w**************@TK2MSFTNGP12.phx.gbl>, l@?.?.invalid writes
Hello,
how can i do this:


Subclass TextBox.

--
Steve Walker

Nov 17 '05 #3
ZS
The following code cam be used to validate the Text that has been input.
try
{
float myNumber = Float.Parse(txtBox1.text);
//Mynumber will have the value
}
catch
{
MessageBox.show("Invalid Number");
}
"Wayne" wrote:
If you are only going to do this in one place then you can do something like
this:

private void textBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)

{

e.Handled = !char.IsDigit(e.KeyChar);

}
Keep in mind the above code doesn't acct. for BackSpace, ., -, +, e

--
Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein

"Steve Walker" <st***@otolith.demon.co.uk> wrote in message
news:pJ**************@otolith.demon.co.uk...
In message <#w**************@TK2MSFTNGP12.phx.gbl>, l@?.?.invalid writes
Hello,
how can i do this:


Subclass TextBox.

--
Steve Walker


Nov 17 '05 #4
ZS <ZS@discussions.microsoft.com> wrote:
The following code cam be used to validate the Text that has been input.
try
{
float myNumber = Float.Parse(txtBox1.text);
//Mynumber will have the value
}
catch
{
MessageBox.show("Invalid Number");
}


IMO, it's better GUI design to not let the user input the invalid data in
the first place. Their ability to input invalid data could introduce
confusion.

This should work:

void tb_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != '\b';
}
Nov 17 '05 #5
I don't know if this helps, but I found a pertinent article on the Web (don't
remember where) which had the following two methods in it. I've implemented
them and they work great!
// Tests whether the input string is a whole number - ie. 0, 1, 2, ...
public static bool IsWholeNumber(string txt)
{
if (txt == null)
txt = "";

Regex objNotWholePattern=new Regex("[^0-9]");
return !objNotWholePattern.IsMatch(txt);
}
// Will return 'txt' as is if it's a number; otherwise will return a
null string
public static string NumericOnly(string txt)
{
if (IsWholeNumber(txt))
return txt;
else
return "";
}



--
Robert W.
Vancouver, BC
www.mwtech.com

"Cool Guy" wrote:
ZS <ZS@discussions.microsoft.com> wrote:
The following code cam be used to validate the Text that has been input.
try
{
float myNumber = Float.Parse(txtBox1.text);
//Mynumber will have the value
}
catch
{
MessageBox.show("Invalid Number");
}


IMO, it's better GUI design to not let the user input the invalid data in
the first place. Their ability to input invalid data could introduce
confusion.

This should work:

void tb_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != '\b';
}

Nov 17 '05 #6

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: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34...
5
by: Brian Robbins | last post by:
In standard C/C++ SDK, MFC, or VCL if I wanted to make a TextBox (CEditBox, TEditBox, etc.) only except numbers I have dozens of ways to handle it. But none of those methods are working in...
8
by: JIM.H. | last post by:
Hello, I am trying to allow only number in a textbox. I need to put it into database as double. So Convert.ToDouble should not fail. How should I do this? Thanks, Jim.
16
by: Keith | last post by:
Am I crazy - to be shocked that there is no Numeric, Alpha, and AlphaNumeric Property in on the Textbox control I searched and searched - and found other people's code - but they don't work...
3
by: Aaron Smith | last post by:
I found an quick and dirty example of a numeric text box that converts the string to a currency mask.. Here is the code: Public Class NumericMaskedTextBox Inherits System.Windows.Forms.TextBox ...
1
by: Luqman | last post by:
My textbox is bound to Typed Dataset, with a Numeric Field, I want the user should not type anything in textbox except Numeric, how can I ? The data type is : Sql Server Decimal(5,2) Currently,...
5
by: Reny | last post by:
can any one tell how can i restrict my user to type just numeric character in the textbox.I am using VS.NET 2003 (VB.NET)
5
lotus18
by: lotus18 | last post by:
Hello World! I have a sample code here written in vb .net that restricts the textbox to accept only alpha, alphanumeric or numeric characters. Public Enum MyOption Alpha = 1 ...
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
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.