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: -
private void txtSurname_KeyPress(object sender, KeyPressEventArgs e)
-
{
-
if (e.Handled = !(Char.IsDigit(e.KeyChar)))
-
{
-
e.Handled = true;
-
}
-
}
OR -
private void txtTel_KeyPress(object sender, KeyPressEventArgs e)
-
{
-
if (((e.KeyChar < '0') || (e.KeyChar > '9')) && (e.KeyChar != '\b'))
-
{
-
e.Handled = true;
-
}
-
}
-
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
3 3408
Ok I managed to get it working doing the following, -
private void txtTel_KeyPress(object sender, KeyPressEventArgs e)
-
{
-
if ((e.Handled = !(Char.IsDigit(e.KeyChar))) && (e.Handled =
-
!(Char.IsWhiteSpace(e.KeyChar))) && (e.Handled =
-
!(Char.IsPunctuation(e.KeyChar))) && (e.Handled =
-
!(Char.IsSymbol(e.KeyChar))) && (e.Handled = !(Char.IsControl(e.KeyChar))))
-
{
-
e.Handled = true;
-
}
-
}
-
this works perfectly, but does anyone know how to do th eabove in a shorter version.
Cheers,
Frinavale 9,735
Recognized Expert Moderator Expert
Ok I managed to get it working doing the following, -
private void txtTel_KeyPress(object sender, KeyPressEventArgs e)
-
{
-
if ((e.Handled = !(Char.IsDigit(e.KeyChar))) && (e.Handled =
-
!(Char.IsWhiteSpace(e.KeyChar))) && (e.Handled =
-
!(Char.IsPunctuation(e.KeyChar))) && (e.Handled =
-
!(Char.IsSymbol(e.KeyChar))) && (e.Handled = !(Char.IsControl(e.KeyChar))))
-
{
-
e.Handled = true;
-
}
-
}
-
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
Thanks mate
That will be very helpful next week when I start with the WEB stuff.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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.
|
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...
|
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?
| |
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)
|
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...
|
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
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |