zoukankan      html  css  js  c++  java
  • 扫描输入后自动定位到下一个输入框

    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;
                }
            }
    }
  • 相关阅读:
    Minimum Inversion Number
    作业四
    牛客小白月赛18 G Forsaken的三维数点
    The Accomodation of Students HDU
    03-Bootstrap学习
    jquery 单击和双击事件冲突解决方案
    13-JS中的面向对象
    12-关于DOM操作的相关案例
    IO多路复用
    python读取excel文件
  • 原文地址:https://www.cnblogs.com/xsj1989/p/11321043.html
Copyright © 2011-2022 走看看