Click here to Skip to main content
11,484,162 members (69,487 online)
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 1: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....
Member 11191738 at 21-Nov-14 3:37am
   
how to set fix character in textbox
Shambhoo kumar at 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 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 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
0 Sergey Alexandrovich Kryukov 185
1 Frankie-C 155
2 Raje_ 155
3 OriginalGriff 147
4 CPallini 110
0 Sergey Alexandrovich Kryukov 8,484
1 OriginalGriff 7,948
2 Sascha Lefèvre 3,189
3 Maciej Los 2,601
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2015
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