Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.0 C#
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 at 11-Aug-12 6:27am
   
Get the textinput and stop the event if the number of items in your textbox is larger than 2....
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 at 11-Aug-12 6:38am
   
Thanks alot..............
Kenneth Haugland at 11-Aug-12 6:39am
   
Even better :) a 5.
shambhoo kumar at 11-Aug-12 6:40am
   
yes... :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

i solve my self.
 
textBox1.MaxLength = 2;
  Permalink  
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

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



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