473,503 Members | 10,012 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(object sender, EventArgs e)
{
if (textBox1.Text = "numbers")
{
// do something
}
}

thanks
Jun 22 '06 #1
6 3166
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(object 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(object 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.Any,
NumberFormatInfo.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 nonNumberEntered = false;

// Handle the KeyDown event to determine the type of character entered
into the control.
private void textBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
// Initialize the flag to false.
nonNumberEntered = 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.
nonNumberEntered = true;
}
}
}
}

// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
// Check for the flag being set in the KeyDown event.
if (nonNumberEntered == 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(object 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(object 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.Any,
NumberFormatInfo.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(object 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
6911
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...
1
4993
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. -...
2
1887
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
4836
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...
4
4362
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! ...
6
6473
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
6879
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...
12
2426
by: MLH | last post by:
Can I somehow set a max length of chars entered into an unbound textbox control?
1
3160
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...
0
7194
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
7070
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...
1
6976
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...
0
7449
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...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
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 ...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
372
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...

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.