扫描输入后自动定位到下一个输入框

code

public delegate void ScanComplete(string text);
public class ScanFormBase : Form
    {
private Dictionary<TextBox, ScanComplete> tbHandler;
private Dictionary<TextBox, Control> nextFocus;
protected void registerScanControl(TextBox tb, ScanComplete handler, Control nextControl)
        {
            if (tb == null)
            {
                return;
            }

            tb.GotFocus += new EventHandler(tb_GotFocus);
            tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);
            if (handler != null)
            {
                if (tbHandler == null)
                {
                    tbHandler = new Dictionary<TextBox, ScanComplete>();
                }
                tbHandler[tb] = handler;
            }

            if (nextControl != null)
            {
                if (nextFocus == null)
                {
                    nextFocus = new Dictionary<TextBox, Control>();
                }
                nextFocus[tb] = nextControl;
            }
        }
void tb_GotFocus(object sender, EventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                //tb.SelectAll();
                var timer = new Timer { Interval = 100, Enabled = true };
                timer.Tick += (EventHandler)delegate
                {
                    tb.SelectAll();
                    timer.Dispose();
                };
            }
        }
void tb_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb == null)
            {
                return;
            }
            if (e.KeyChar == (char)Keys.Return)
            {
                if (tbHandler != null && tbHandler.ContainsKey(tb))
                {
                    tbHandler[tb](tb.Text);
                }
                if (nextFocus != null && nextFocus.ContainsKey(tb))
                {
                    nextFocus[tb].Focus();
                }
                e.Handled = true;
            }
        }
}

 

上一篇:ASP.NET中TextBox控件设立ReadOnly="true"后台取不到值


下一篇:javascript – 如何在点击文本框中更改文本的字体样式?