Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir i try to restrict textbox only contain 2 digit data but i get problem to do this task.......

i want, user put only two digit data in text box .....and when user put 3 digit of data then automaticaly one message show "u r only put 2 digit data in this text box".....

plz help me....
Posted
Comments
Kenneth Haugland 11-Aug-12 6:27am    
Get the textinput and stop the event if the number of items in your textbox is larger than 2....
Member 11191738 21-Nov-14 3:37am    
how to set fix character in textbox
Shambhoo kumar 21-Nov-14 7:32am    
fix character means??

i solve my self.

textBox1.MaxLength = 2;
 
Share this answer
 
Try This:-
First set the MaxLength Property of Textbox to 2 and then write following the following code:-
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
     if (textBox1.Text.Length > 2)
     {
         MessageBox.Show("Can Enter only two Characters in this Textbox.", "Textbox", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
}
 
Share this answer
 
v2
Hi,
You can use TextBox's MaxLength property to restrict user to enter only 2 characters. To display message to user you can use TextChanged event of TextBox.
 
Share this answer
 
Comments
Shambhoo kumar 11-Aug-12 6:38am    
Thanks alot..............
Kenneth Haugland 11-Aug-12 6:39am    
Even better :) a 5.
Shambhoo kumar 11-Aug-12 6:40am    
yes... :)

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