Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void txtcnic1_TextChanged(object sender, EventArgs e)
       {
           if (txtcnic1.MaxLength == 5)
               txtcnic2.Focus();
       }
Posted
Comments
__TR__ 20-Nov-12 5:11am    
Windows or web application ?

Try something like this:
if(txtcnic1.Text.Length == txtcnic1.MaxLength) {
   txtcnic2.Focus();
}

Or somewhat more generic, so you can use the event handler for more than one textbox:
TextBox txtBox = (TextBox)this.ActiveControl;
if (txtBox.TextLength == txtBox.MaxLength) 
{
  this.SelectNextControl(this.ActiveControl, true, true, false, false);
}

Good luck!
 
Share this answer
 
Hi,

Update your code as below.

C#
private void txtcnic1_TextChanged(object sender, EventArgs e)
{
  if (txtcnic1.MaxLength == txtcnic1.Text.Length)
      txtcnic2.Focus();
}


And in your aspx page.

C#
<asp:textbox id="txtcnic1" runat="server" ontextchanged="txtcnic1_TextChanged" AutoPostBack="true"></asp:textbox>


Please make sure you have set AutoPostBack property to true.
 
Share this answer
 
v2
Hi
Try this

C#
private void textBox1_TextChanged(object sender, EventArgs e)
     {
         if(textBox1.TextLength.Equals(textBox1.MaxLength))
             textBox2.Focus();
     }
 
Share this answer
 
Here is an approach using JavaScript

Here are the 2 text boxes. The Maxlength property of txtbox1 is set to 4. So after entering 4 characters in this text box the focus will be shifted to textbox txtbox2.
XML
<asp:TextBox ID="txtbox1" runat="server" MaxLength="4"></asp:TextBox>
<asp:TextBox ID="txtbox2" runat="server"></asp:TextBox>


Here is the JavaScript that shifts the focus to textbox txtbox2.
JavaScript
<script type="text/javascript">
     
    function setFocus(MaxLength)
    {
        var txt1 =  document.getElementById('<%= txtbox1.ClientID%>');
        var txt2 =  document.getElementById('<%= txtbox2.ClientID%>');
        if(txt1.value.length == MaxLength)
        {
            txt2.focus();
        }
    }
    </script>



In your code behind file call the javascript function in page_Load.
C#
txtbox1.Attributes.Add("onkeyup", "setFocus(" + txtbox1.MaxLength +")");
 
Share this answer
 

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