473,385 Members | 1,325 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,385 software developers and data experts.

C# App: Numeric and Mathimatical Operators only TextBox

62
Hi

I'm looking to ensure that my textbox only accepts numbers .. AND ... mathematical operators + - . / *. an dmaybe a blank space

I can ensure that only number are included by using:
Expand|Select|Wrap|Line Numbers
  1. private void txtSurname_KeyPress(object sender, KeyPressEventArgs e)
  2. {
  3.      if (e.Handled = !(Char.IsDigit(e.KeyChar)))
  4.      {
  5.          e.Handled = true;
  6.      }   
  7. }
OR
Expand|Select|Wrap|Line Numbers
  1. private void txtTel_KeyPress(object sender, KeyPressEventArgs e)
  2. {
  3.       if (((e.KeyChar < '0') || (e.KeyChar > '9')) && (e.KeyChar != '\b'))    
  4.      {
  5.          e.Handled = true;
  6.      }
  7. }
  8.  
My Questions then:

1. Which one of the above is better to use to ensure that only numbers are
used?

2. How can I change the better option above to also allow me to input the
specified operators.

Thanks in advance,

Lóan
Nov 30 '07 #1
3 3381
LoanB
62
Ok I managed to get it working doing the following,
Expand|Select|Wrap|Line Numbers
  1.  private void txtTel_KeyPress(object sender, KeyPressEventArgs e)
  2. {
  3.      if ((e.Handled = !(Char.IsDigit(e.KeyChar))) && (e.Handled =   
  4.      !(Char.IsWhiteSpace(e.KeyChar))) && (e.Handled = 
  5.      !(Char.IsPunctuation(e.KeyChar))) && (e.Handled = 
  6.      !(Char.IsSymbol(e.KeyChar))) && (e.Handled = !(Char.IsControl(e.KeyChar))))
  7.      {
  8.          e.Handled = true;
  9.      }
  10. }
  11.  
this works perfectly, but does anyone know how to do th eabove in a shorter version.

Cheers,
Nov 30 '07 #2
Frinavale
9,735 Expert Mod 8TB
Ok I managed to get it working doing the following,
Expand|Select|Wrap|Line Numbers
  1.  private void txtTel_KeyPress(object sender, KeyPressEventArgs e)
  2. {
  3.      if ((e.Handled = !(Char.IsDigit(e.KeyChar))) && (e.Handled =   
  4.      !(Char.IsWhiteSpace(e.KeyChar))) && (e.Handled = 
  5.      !(Char.IsPunctuation(e.KeyChar))) && (e.Handled = 
  6.      !(Char.IsSymbol(e.KeyChar))) && (e.Handled = !(Char.IsControl(e.KeyChar))))
  7.      {
  8.          e.Handled = true;
  9.      }
  10. }
  11.  
this works perfectly, but does anyone know how to do th eabove in a shorter version.

Cheers,
The above will work for a desktop application.
If you're developing a web application please see the article on how to check if a textbox contains a number.

-Frinny
Nov 30 '07 #3
LoanB
62
Thanks mate

That will be very helpful next week when I start with the WEB stuff.
Nov 30 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: John Dell'Aquila | last post by:
I want to trap numeric operators (+ * % etc.) so that my new style classes can handle them generically, similar to __getattr__ in an old style class. I've never done any metaprogramming before...
1
by: Jose Gonzalez | last post by:
How to apply a numeric format to a textbox using xhtml? I know you have to use the "-wap-input-format" style tag in css. I can get this to work in a regular xhtml page, however, I've been...
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...
11
by: Keith | last post by:
I apologize for those of you who think I'm posting on the same topic. It is not that I don't appreciate all of your comments - and I'm definitely reading them all - but I think I have a differing...
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
by: Andreas Beyer | last post by:
There has been quite some traffic about mutable and immutable data types on this list. I understand the issues related to mutable numeric data types. However, in my special case I don't see a...
8
by: Ethan Furman | last post by:
Greetings, List! I'm working on a numeric data type for measured values that will keep track of and limit results to the number of significant digits originally defined for the values in...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.