Click here to Skip to main content
12,352,089 members (31,283 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: .NET3.0 C# .NET
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 11-Aug-12 0:21am
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??
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
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... :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try This:-
First set the MaxLength Property of Textbox to 2 and then write following the following code:-
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);
     }
}
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

i solve my self.

textBox1.MaxLength = 2;
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100