如何找到以前的活动控件c#

vol*_*ody 3 .net c# winforms

我正在开发键盘控制,非常简单地嵌入在表单上.使用sendkey类来执行char条目.要使这个功能需要知道以前选择的控件.

Mar*_*haw 5

像下面这样的东西应该做的伎俩:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DragDropTest
{
    public partial class LostFocusTestForm : Form
    {
        private Control _lastControl;

        public LostFocusTestForm()
        {
            InitializeComponent();

            TrapLostFocusOnChildControls(this.Controls);
        }
        private void finalTextBox_Enter(object sender, EventArgs e)
        {
            MessageBox.Show("From " + _lastControl.Name + " to " + this.ActiveControl.Name);
        }

        private void AllLostFocus(object sender, EventArgs e)
        {
            _lastControl = (Control)sender;
        }

        private void TrapLostFocusOnChildControls(Control.ControlCollection controls)
        {
            foreach (Control control in controls)
            {
                control.LostFocus += new EventHandler(AllLostFocus);

                Control.ControlCollection childControls = control.Controls;
                if (childControls != null)
                    TrapLostFocusOnChildControls(childControls);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)