Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my window application i have many text box and i want to enter only single digit numeric value in it.Like i only enter 2 and 3 not enter two digit 23.How i do this can any one help.

C#
 if (System.Text.RegularExpressions.Regex.IsMatch("[^0-9]", txt_Digit8.Text))
{
   MessageBox.Show("Please enter only numbers.");
   txt_Digit8.Text.Remove(txt_Digit8.Text.Length - 1);
}

I use this code but it not work.

Also i want to give range like only accept 0 to 5 number.
Posted
Updated 21-Jan-12 4:33am
v3

You probably need one of these[^].
 
Share this answer
 
Why not use a MaskedTextBox, with a Mask of "#", or a NumericUpDown, with a min on 0 and a max of 9?
Either way, the user can't enter a wrong value, so no need to validate.
 
Share this answer
 
Comments
BobJanova 20-Jan-12 10:26am    
Seconding the idea of a NumericUpDown.
fjdiewornncalwe 20-Jan-12 11:13am    
You should add this as your own answer as an alternate. +5 for that if you do.
BobJanova 20-Jan-12 12:51pm    
I haven't bothered posting a solution to this question since Griff got the right one before I got here :P
fjdiewornncalwe 20-Jan-12 11:12am    
+5. This is the way to do it with a textbox.
BillWoodruff 20-Jan-12 19:54pm    
'Thirding' the idea of using a NumericUpDown ... unless: this code is not converting the input to numbers, and then using them "as numbers."
A very simple code:
use the following event and code:
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
       {
           textBox1.MaxLength = 1;
           if (Char.IsNumber(e.KeyChar))
           {
               e.Handled = false;
           }
           else
           {
               e.Handled = true;
           }
       }
 
Share this answer
 
Comments
BillWoodruff 20-Jan-12 19:53pm    
No need to set MaxLength in every invocation of the KeyPress EventHandler: set it once at design-time.
jaideepsinh 21-Jan-12 10:31am    
perfect as i want.Thank's.
jaideepsinh 21-Jan-12 10:32am    
Can i give range of number. Like accept only 0,1,2,3,4,5.
why don't you use a dropdown with 10 digits ?
 
Share this answer
 
Comments
fjdiewornncalwe 20-Jan-12 13:38pm    
My 3. It would work, but is not really a very elegant solution. Users typically like the masked text box or numeric up/down approach better.
It's much simpler than that. Just set the MaxLength property of the textbox to 1 and then in the KeyUp event do the validation
 
Share this answer
 
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
txt_current7.MaxLength = 1;
if (e.KeyChar == '0' || e.KeyChar == '1' || e.KeyChar == '2' || e.KeyChar == '3' || e.KeyChar == '4' || e.KeyChar == '5' || e.KeyChar == '\b')
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900