达到最大长度时如何自动更改文本框的焦点 [英] how to automatically change focus of text box when reach to max length

查看:61
本文介绍了达到最大长度时如何自动更改文本框的焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

尝试类似的方法:
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!




如下更新代码.

Hi,

Update your code as below.

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



然后在您的aspx页面中.



And in your aspx page.

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



请确保已将AutoPostBack属性设置为true.



Please make sure you have set AutoPostBack property to true.



试试这个

Hi
Try this

private void textBox1_TextChanged(object sender, EventArgs e)
     {
         if(textBox1.TextLength.Equals(textBox1.MaxLength))
             textBox2.Focus();
     }


这篇关于达到最大长度时如何自动更改文本框的焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆