• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一只蜗牛
有一种爱挂着泪珠,但很凄美,它叫放弃!放弃虽然痛苦,但也是一种幸福的拥有,感情是不能勉强的,如果死死抓住, 抓住的也是伤痕和痛苦.把手握紧,里面什么也没有,把手松开就拥有了一切。
博客园    首页    新随笔    联系   管理    订阅  订阅
按回车键聚焦到指定控件

如图,在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);
            }

  

一个不努力的人在努力做到努力
posted on 2020-07-09 13:24  XuPeppy  阅读(252)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3