473,795 Members | 2,498 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# App: Numeric and Mathimatical Operators only TextBox

62 New Member
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 3408
LoanB
62 New Member
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 Recognized Expert Moderator Expert
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 New Member
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
1421
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 but I'm thinking of using the below metaclass to accomplish this. It reads a class variable, implements_operators, to determine which methods handle various operators, then inserts closures to call the generic methods with disambiguating...
1
4660
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 unsuccessful in getting it to work in asp.net 1.1 The following code has allowCustomAttributes set and works fine for labels, but not for textboxes. This is because it is building span tags around the control. I need to embed the class tag inside the...
16
8408
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 perfectly. Some do only allow numeric - but you cannot backspace. Maybe that is "good enough" - but surely it is not that difficult to have a textbox that only accepts numeric values and backspaces.
11
4581
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 opinion of how I want to handle the 'user experience' in the application I'm creating. While I know I could allow the user to enter in number and alpha text - in a text box - and then tell them when the execuate a command "This is not numeric data", I...
1
1914
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, VB.Net automatically raise error on lost focus of textbox, but I don't want user to type anything else except Numeric, any property to be set in Textbox?
5
13105
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
1620
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 better solution to the problem. Here is what I am doing: I am using a third party library that is performing basic numerical operations (adding, multiplying, etc.) with objects of unknown type. Of course, the objects must support the numerical...
8
1465
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 question. I am doing this primarily because I enjoy playing with numbers, and also to get some experience with unit testing. At this point I have the __init__ portion finished, and am starting on
5
28699
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 AlphaNumeric = 2 Numeric = 3 End Enum
0
9672
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
9519
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
10436
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
10213
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...
1
10163
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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...
1
7538
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.