how to limit textbox to text only

G

Guest

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
 
C

cp

Avi said:
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
 
C

cp

Avi said:
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
 
K

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;
}
}
 
G

Guest

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
}
}
 
M

Marc Gravell

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
 
T

Tom Spink

Avi said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top