按回车键聚焦到指定控件

如图,在FlowLayoutPanel中添加用户控件UserControl,用户控件中有多个控件,包含TextBox,要实现按回车键直接聚焦下一个用户控件中的指定TextBox。

直接网上找了现成的类TabEnter,代码如下:

 /// <summary>
    /// 回车、Tab键盘切换或执行操作
    /// </summary>
    public sealed class TabEnter : IDisposable
    {
        private List<StringBuilder> ml;
        private int i = 0;
        private System.Windows.Forms.Control mc;
        /// <summary>
        /// 知否启用Tab键功能
        /// </summary>
        private bool mallowTab = false;
        /// <summary>
        /// 是否启用Tab键切换/执行.
        /// </summary>
        public bool AllowTab
        {
            get { return mallowTab; }
            set { mallowTab = value; }
        }
        public TabEnter(System.Windows.Forms.Control c)
        {
            ml = new List<StringBuilder>();
            mc = c;
        }
        public TabEnter(System.Windows.Forms.Control c, bool allowTab) : this(c)
        {
            mallowTab = allowTab;
        }
        public void Add(System.Windows.Forms.Control c)
        {
            c.KeyPress += KeyPressHandler;
            c.TabIndex = i;
            ml.Add(new StringBuilder(c.Name));
            i += 1;
        }
        /// <summary>
        /// 在需要独立处理KeyPress时间时,采用KeyUp来执行,当然可继续实现KeyDown
        /// </summary>
        /// <param name="c"></param>
        public void AddKeyUp(System.Windows.Forms.Control c)
        {
            c.KeyUp += KeyUpHandler;
            c.TabIndex = i;
            ml.Add(new StringBuilder(c.Name));   //将需要回车定位的控件名添加进来
            i += 1;
        }
        private void KeyPressHandler(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if ((e.KeyChar == (Char)13) || (e.KeyChar == (Char)9 && mallowTab == true))
            {
                int j = ((System.Windows.Forms.Control)sender).TabIndex;
                if (j >= ml.Count - 1) return;
                string cname = ml[j + 1].ToString();
                if (string.IsNullOrEmpty(cname)) return;
                System.Windows.Forms.Control[] tca = mc.Controls.Find(cname, true);
                if (tca == null || tca.Length == 0) return;
                System.Windows.Forms.Control tc = tca[0];
                if (tc == null) return;
                System.Windows.Forms.Button b = tc as System.Windows.Forms.Button;
                if (b != null)
                    b.PerformClick();
                else
                    tc.Focus();
            }
        }
        private void KeyUpHandler(Object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter) || (e.KeyCode == System.Windows.Forms.Keys.Tab && mallowTab == true))
            {
                int j = ((System.Windows.Forms.Control)sender).TabIndex;
                if (j >= ml.Count - 1) return;
                string cname = ml[j + 1].ToString();
                if (string.IsNullOrEmpty(cname)) return;
                System.Windows.Forms.Control[] tca = mc.Controls.Find(cname, true);
                if (tca == null || tca.Length == 0) return;
                System.Windows.Forms.Control tc = tca[0];
                if (tc == null) return;
                if (tc.GetType() == typeof(System.Windows.Forms.Button))
                {
                    ((System.Windows.Forms.Button)tc).PerformClick();
                }
                else
                {
                    if (tc.Visible == true) tc.Focus();   //聚焦下个控件
                    Log.WriteLog("keyup tc.typejdkfj:" + cname);
                }

            }
        }
        #region "资源释放"
        public void Dispose()
        {
            Disposing(true);
            GC.SuppressFinalize(this);
        }
        private bool m_disposed = false;
        protected void Disposing(bool disposing)
        {
            if (!m_disposed)
            {
                if (disposing)
                {
                    //Release managed resources
                    ml.Clear();
                    ml = null;
                    i = 0;
                    mc = null;
                }
                //Release unmanaged Resources
                m_disposed = true;
            }
        }

        ~TabEnter()
        {
            Disposing(false);
        }
        #endregion
    }

  使用代码如下:在该页面初始化时添加如下代码

 TabEnter tabEnter = new TabEnter(flpList,true);
  foreach (UCItem item in flpList.Controls)
            {
                tabEnter.AddKeyUp(item.txtInfo);
            }

  

原文地址:https://www.cnblogs.com/Betty-IT/p/13268294.html