How to know which component is in focus?

Asked

Viewed 2,598 times

4

I am making a form (Windows Forms) in C# and I would like to know how to get the component that is focused. In my form there is a SplitterPanel and within it, in the Panel2, has a TabControl with several: ComboBox, TextBox and Button. I tried to use the property ActiveControl, but instead of taking the component with the focus, it takes the SplitterPanel.

3 answers

3


In this question in the OS I think you have the answer you want. Correct me if I’m wrong. You’re right in your statement. I highlight the first two answers there.

Method that scans the controls to find which one is in focus (I created as extension method since it can be quite useful for all controls):

public static Control FindFocusedControl(this Control control) {
    var container = control as IContainerControl;
    while (container != null) {
        control = container.ActiveControl;
        container = control as IContainerControl;
    }
    return control;
}

I put in the Github for future reference.

Way to use:

formulario.ActiveControl.FindFocusedControl();

Or have a shape using the Win32 API:

public class MyForm : Form {
      [DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Winapi)]
      internal static extern IntPtr GetFocus();

      private Control GetFocusedControl() {
           Control focusedControl;
           // To get hold of the focused control:
           IntPtr focusedHandle = GetFocus();
           if(focusedHandle != IntPtr.Zero)
                // Note that if the focused Control is not a .Net control, then this will return null.
                focusedControl = Control.FromHandle(focusedHandle);
           return focusedControl;
      }
}

I put in the Github for future reference.

  • I had found a code similar to the first example, but it ran through all the components (I didn’t think this was cool), but the one that you went through, it only goes through the hierarchy until you get to the component with the focus, so I tested FindFocusedControl was faster than GetFocusedControl (used Stopwatch to test). Note: the extension really simplifies things, but it is in trouble and is not working

  • What problems?

  • container undefined in context; control = container.ActiveControl; makes an error when container is void; lack of ; at the end of the line of while... are simple things, only it would be good to leave the correct answer!

  • Look now. It was just a lack of attention in the rush.

  • errors still continue, if you put the code of your first answer and only do it as an extension, it works perfectly!

  • I think now I found the problem. Is that I don’t have Visual Studio to test.

  • understood, the error still continues, the problem occurring on this line control = container.ActiveControl;, because in the last iteration the container is null, I can adjust or you better arrange it yourself?

  • If you can do it, you can do it.

Show 3 more comments

1

You can test whether the control returned by the Activecontrol property is a Container type control (i.e., it implements the Icontainercontrol interface). If yes, you search the Activecontrol container.

Here’s an example I made in the form Mouseup event:

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    var formSender = (Form)sender;

    var controle = formSender.ActiveControl;

    while (controle is IContainerControl)
    {
        var container = controle as IContainerControl;
        controle = container.ActiveControl;
    }

    MessageBox.Show(controle.Name);
}

Att.

-1

I did but is presenting this message: Extension method must be defined in a non-generic Static class, on the line where I define the partial class: public partial class frmReferences : Form

Browser other questions tagged

You are not signed in. Login or sign up in order to post.