- // DLL调用注册
- [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
- private static extern IntPtr GetFocus();
- /// <summary>
- /// 当前拥有焦点的控件
- /// </summary>
- /// <param name="formControl"></param>
- /// <returns></returns>
- public static Control GetFocusedControl(this Control formControl)
- {
- Control focusedControl = null;
- try
- {
- IntPtr focusedHandle = GetFocus();
- if (focusedHandle != IntPtr.Zero)
- {
- focusedControl = Control.FromChildHandle(focusedHandle);
- }
- }
- catch { }
- return focusedControl;
- }
- /// <summary>
- /// 设置当前获得焦点的控件的下一控件(Tab顺序)为当前焦点控件
- /// </summary>
- public static void SetNextControlFocused(this Control formControl)
- {
- Control ctlFocused = GetFocusedControl(formControl);
- if (ctlFocused != null)
- {
- Control ctl = formControl.FindForm().GetNextControl(ctlFocused, false);
- if (ctl == null)
- {
- ctl = formControl.FindForm().GetNextControl(ctlFocused, true);
- }
- if (ctl != null)
- {
- ctl.Focus();
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2407201513230.html
来源: http://www.codesnippet.cn/detail/2407201513230.html