在form窗体里面 寻找当前焦点的控件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace PrintDatas
{
    public static class SelectFocusInControls
    {
        // DLL调用注册  
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
        private static extern IntPtr GetFocus();
        /// <summary>  
        /// 当前拥有焦点的控件  
        /// </summary>  
        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();
                }
            }
        }
原文地址:https://www.cnblogs.com/jcdd-4041/p/3363068.html