日期:2014-05-18  浏览次数:20799 次

c#应用程序光标自动移动问题
有相邻的文本框,要求当某文本框输入的位数满足几位时,光标自动跳到下一个文本框,就好象输入软件序列号那样,请问如何做?是在控件上设置还是在时间中控制?

------解决方案--------------------
重写 TextBox 的 OnKeyPress() 方法(类似这个名字,具体查一下MSDN)。

------解决方案--------------------
将要处理的TextBox控件的KeyPress都赋值为textBox1_KeyPress
并设置各自的MaxLength属性,即输入的最大长度
参考如下代码:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case (char)Keys.Back: // 退格键
case (char)Keys.Escape: // 取消键
return;
}
if (((TextBox)sender).SelectionLength > 0) return; // 有选中的字符
if (((TextBox)sender).Text.Length + 1 > = ((TextBox)sender).MaxLength)
{
SelectNextControl((Control)sender, true, true, true, true);
}
}