How to know the focused control
-
Hi, I used the following code to know the control which has focus: System.Windows.Forms.AccessibleObject.GetFocused The error I got is 'GetFocused' is not a member of 'AccessibleObject', but Documentation says, it is a member. Do you any one know how to find the focused control. Thanks, Sreepathi
-
Hi, I used the following code to know the control which has focus: System.Windows.Forms.AccessibleObject.GetFocused The error I got is 'GetFocused' is not a member of 'AccessibleObject', but Documentation says, it is a member. Do you any one know how to find the focused control. Thanks, Sreepathi
This will return a currently focused control on a form IF the form does not have any container controls on it.
public Control GetFocused() { foreach(Control ctrl in this.Controls) { if (ctrl.Focused) { return ctrl; } } return null; }
This is a universal way of getting the currently focused control:public class MyForm : Form { [DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Winapi)] internal static extern IntPtr GetFocus(); private Control GetFocusedControl() { Control focusedControl = null; // 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; } }