How to find the previous active C # control

I am developing a keyboard, a very simple built-in form. using the sendkey class to write char. so that this functionality is necessary in order to know the previous control selected.

+3
source share
6 answers

Something like the following should do the trick:

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);
            }
        }
    }
}
+5
source

Extension of David's answer. So you can use the Enter event and a variable to store the last control:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Control lastControlEntered = null;

        public Form1()
        {
            InitializeComponent();

            foreach (Control c in Controls)
                if (!(c is Button)) c.Enter += new EventHandler(c_Enter);
        }

        void c_Enter(object sender, EventArgs e)
        {
            if (sender is Control)
                lastControlEntered = (Control)sender;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = lastControlEntered == null ? "No last control" : lastControlEntered.Name;
        }
    }
}

, Visual Studio button1_Click. , , , . .

+2

. "Enter"

0

Control.ControlCollection, () , "Enter", . Active Control Active Control, , ... . , FrameWork 3.5 4.0.

// :

using System;
using System.Collections.Generic;
using System.Windows.Forms;

private static EventHandler _event;

    // extension method on Control.ControlCollection
    public static void SetEnterEvent(this Control.ControlCollection theCollection, EventHandler theEvent)
    {
        _event = theEvent;
        recurseSetEnter(theCollection);
    }

    // recurse all the controls and add the Enter Event :

    public static void recurseSetEnter(Control.ControlCollection aCollection)
    {
        foreach (Control theControl in aCollection)
        {
            // "weed out" things like internal controls of the NumericUpDown Control
            // String.IsNullOrWhiteSpace is FrameWork 4.0
            // use Trim() followed by String.IsNullOrEmpty for FrameWork 3.5
            if (!String.IsNullOrWhiteSpace(theControl.Name))
            {
                Console.WriteLine("setting enter handler for : " + theControl.Name + " : " + theControl.GetType().ToString());

                theControl.Enter += _event;
            }

            if (theControl.Controls.Count > 0) recurseSetEnter((Control.ControlCollection)theControl.Controls);
        }
    }

, : :

, , Enter :

:

public static Control theActiveControl = null;

public static Control thePreviousControl = null;

, :

private void control_enter(object sender, EventArgs e)
{
    thePreviousControl = theActiveControl;
    theActiveControl = sender as Control;
    Console.WriteLine("Active Control is now : " + theActiveControl.Name);
}

Form_Load :

// in a Form**

    // define a delegate for the enter Event
    private event EventHandler enter = delegate { };

// in the form load even or somewhere : assign an actual event handler to the delegate
    enter += new EventHandler(control_enter);

, :

this.Controls.SetEnterEvent(enter);

: WinForm ActiveControl: , , :... (, ) , "/"... ActiveControl, // / .. , "ActiveControlChanged".

[edit] "", , , - "" ( ), , ... ... . []

[edit] , , PictureBox, "Enter event", : - , , ", : , PictureBox" ". " "" "(" ControlContainer " ). , , , " ", - . []

, . , , , , , "", :)

0

string activeControl = this.ActiveControl.Name

0

. UIElement.LostFocus, " ", .

. WPF. , WinForms. WPF, .

-2

Source: https://habr.com/ru/post/1724017/


All Articles