473,652 Members | 2,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to limit textbox to text only

Hi,

what is the code to limit a textbox that will accept only text and not number

private void button1_Click(o bject sender, EventArgs e)
{
if (textBox1.Text = "numbers")
{
// do something
}
}

thanks
Jun 22 '06 #1
6 3194
cp

Avi G wrote:
Hi,

what is the code to limit a textbox that will accept only text and not number

private void button1_Click(o bject sender, EventArgs e)
{
if (textBox1.Text = "numbers")
{
// do something
}
}

thanks


youre probably better off preventing the user from entering text chars
in the box in the first place rather than the button click - derive
your own control, check the characters you want to allow in the
keypress event and set e.Handled to true if its not a number.

cp

Jun 22 '06 #2
cp

Avi G wrote:
Hi,

what is the code to limit a textbox that will accept only text and not number

private void button1_Click(o bject sender, EventArgs e)
{
if (textBox1.Text = "numbers")
{
// do something
}
}

thanks


PS forgot to mention ... you can test for a number using 'TryParse'

bool isNumber = Double.TryParse ( textBox1.Text, NumberStyles.An y,
NumberFormatInf o.InvariantInfo , out result );

cp

Jun 22 '06 #3
Kim
You should use the KeyPress or KeyDown (or both as in the example)
event on your textbox. This way you can prevent non-text to be entered,
hence the user will NOT see the invalid characters.
The following code has been taken from the from MSDN on KeyPress event:

// Boolean flag used to determine when a character other than a number
is entered.
private bool nonNumberEntere d = false;

// Handle the KeyDown event to determine the type of character entered
into the control.
private void textBox1_KeyDow n(object sender,
System.Windows. Forms.KeyEventA rgs e)
{
// Initialize the flag to false.
nonNumberEntere d = false;

// Determine whether the keystroke is a number from the top of the
keyboard.
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
// Determine whether the keystroke is a number from the keypad.
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
// Determine whether the keystroke is a backspace.
if(e.KeyCode != Keys.Back)
{
// A non-numerical keystroke was pressed.
// Set the flag to true and evaluate in KeyPress event.
nonNumberEntere d = true;
}
}
}
}

// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPre ss(object sender,
System.Windows. Forms.KeyPressE ventArgs e)
{
// Check for the flag being set in the KeyDown event.
if (nonNumberEnter ed == true)
{
// Stop the character from being entered into the control since
it is non-numerical.
e.Handled = true;
}
}

Jun 22 '06 #4
what if i do need the code for that, what it would be?

private void button1_Click(o bject sender, EventArgs e)
{
if (textBox1.Text = "numbers between 0-9")
{
// popup message
}
}
"cp" wrote:

Avi G wrote:
Hi,

what is the code to limit a textbox that will accept only text and not number

private void button1_Click(o bject sender, EventArgs e)
{
if (textBox1.Text = "numbers")
{
// do something
}
}

thanks


PS forgot to mention ... you can test for a number using 'TryParse'

bool isNumber = Double.TryParse ( textBox1.Text, NumberStyles.An y,
NumberFormatInf o.InvariantInfo , out result );

cp

Jun 22 '06 #5
Note that this doesn't cope with paste via the mouse (right-click); to deal
with this you would presumably need to look at TextChanged or Validating

Marc
Jun 22 '06 #6
Avi G wrote:
Hi,

what is the code to limit a textbox that will accept only text and not
number

private void button1_Click(o bject sender, EventArgs e)
{
if (textBox1.Text = "numbers")
{
// do something
}
}

thanks


Hi Avi,

Try this:

///
private void button1_Click ( object s, EventArgs e )
{
bool isNumber = true;
foreach ( char c in textBox1.Text )
if ( ! char.IsDigit( c ) )
{
isNumber = false;
break;
}

if ( isNumber )
{
// Do something, if the textbox is a number.
}
else
{
// Do something, if the textbox isn't a number.
}
}
///

You could use 'TryParse' but if the number is too big to fit in the testing
data structure, then you're up the creek. This will ensure only numeric
digits appear in the textbox.

-- Tom Spink
Jun 22 '06 #7

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

Similar topics

3
6914
by: Quack Boy | last post by:
Hi all, I've worked out that the limit of a textbox is 64Kb. What can I do if the I want to work with text which is, say four or five times as much? Can I extend the limit (maxlength??) of the text box or do I have to change the textbox for some other component? And yes, I'm very very new to VB ;) Thank you.
1
5010
by: SpreadPeace | last post by:
I'm hitting the 255 character limit of a text box on a form and was wondering if anyone know how to get around this. Here the scenario.... - Access 2000 front end with Sql Server backend. - The textbound is bound to a field which is nvarchar*4000 (I know with an Access DB you have to use a memo field to get around the size limit, I would think nvarchar*4000 would be good enough) - The 255 character limit only occurs in the VBA code. ...
2
1895
by: Parintas Themis STE Kardias | last post by:
I have a textbox, and i wand to put a limit (for example only 8 characters) and if i entry characters not longer of 8 then add spaces for the rest of textbox characters
4
4847
by: Paul E Collins | last post by:
I'm writing an application with a TextBox control on its main form. I'm running the application under WinXP Pro, so (as I understand it) there should be no practical limit to the number of characters in a text box. However, when I paste in a large amount of text, the text box truncates it at exactly 32K characters. This is a problem, because I need to know exactly how many of each character were pasted; besides, I wasn't expecting the...
4
4367
by: Mark | last post by:
Can you limit the number of characters in a multi-line textbox? My tests in indicate that the MaxLength property causes client side limits only on single line textboxes. Thanks in advance! Mark
6
6499
by: Aaron Smith | last post by:
Is there a way to put a limit on the text size of a datagrid column? Thanks, Aaron -- --- Aaron Smith Remove -1- to E-Mail me. Spam Sucks.
7
6892
by: Andrew | last post by:
VB .NET 2003, WinXP Pro: Adding text to a text box with the TextBox.AppendText method limits the amount of text in the textbox to 32K. I have a short program that uses the GetFiles function of the directory object, then iterates through the returned array and appends the strings to the textbox. However, if the character count is greater than 32k, no additional text is displayed in the textbox, though no error is raised.
12
2434
by: MLH | last post by:
Can I somehow set a max length of chars entered into an unbound textbox control?
1
3165
by: | last post by:
Hello everyone, I am using a textbox for dumping information generated by the file system watch object. I am having this problem that always at some stage new new information stops being entered into the textbox even though there should be. If I clear the textbox, new information will start tp appear again. I am not receiving any error message, so is there some sort of internal limit on the amount of text which can be added/pasted into a...
0
8367
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
8279
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
8703
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
8467
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
8589
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...
0
7302
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
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
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.