Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

How text box text automatically moving to next control when limit is reached in c# .net windows application?

Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted
Comments
Mayur Panchal 21-May-13 2:24am    
You can handle TextChanged event of textbox. put a condition there, when limit reached then transfer focus to other textbox.

Hi
It is really easily just do it :

C#
<pre lang="cs">private void textBox1_TextChanged(object sender, EventArgs e)
{

    if (((TextBox)sender).TextLength > 5)
        SendKeys.Send("{Tab}");
}


Just remember you should set TabIndex for your controls.

Best Regards.
 
Share this answer
 
v2
Comments
[no name] 21-May-13 2:35am    
Thanks Your Code is Working.
Can you help me another approarch?
[no name] 21-May-13 2:38am    
How can we this approach in sql server?
How column value automatically moving to next column when limit is reached, in sql server?
Aydin Homay 21-May-13 2:38am    
Ok Please don`t forget accept green button and the after why not sure ;)
Aydin Homay 21-May-13 2:39am    
Column value where ? in datagridview or something else ? please describe your question :P
[no name] 21-May-13 2:42am    
In sql server table,column value is a any type of string.
Hi Ankit,

I hope you want this,

Two text boxes, TextBox1 and TextBox2 with limit 4 respectively.
If I type 4 characters in TextBox1 then automatically focus should move to TextBox2.
For example,
If I want to type 12345678.
I type 1234 in TextBox1, When I press 5, focus should point to TextBox2 and 5678 should be entered in TextBox2, am i right?
If am right then here is the code for you

C#
private void textBox1_TextChanged(object sender, EventArgs e)
       {
           //validates the length of textbox starts from 0.
           if (textBox1.TextLength > 3)
           {
               textBox2.Focus();
           }
       }

I hope this would help you a bit.

Regards,
RK
 
Share this answer
 
It is very easy to do this, just use the following code:
private void textBox1_TextChanged(object sender, EventArgs e)
{
   // Determine the number of characters typed into textbox1, 
   // if greater than 5, go to textbox2            
   if(textBox1.TextLength == 5)
   {
       // textbox2 becomes the active textbox            
       textBox2.Focus();
   }
}
Note: you must use the double equals (==) for it to work, don't try to use the greater than (>) symbol.
 
Share this answer
 
Comments
CHill60 18-May-20 4:44am    
Reasons for my downvote:
- Comment says "if greater than 5, go to textbox2 " but code says "if exactly equal to 5, go to textbox2".
- Your words "you must use the double equals (==) for it to work, don't try to use the greater than (>) symbol." is absolute tosh
- You have essentially just repeated Solution 2 posted 7 years ago

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