The fastest way to get concentrated control in WinForms C #?

I use ProcessCmdKey in my main application form window to validate certain keys and provide spatial, right, left, and several other special treatments. ProcessCmdKey in the main form is called even if the user enters a TextBox inside a nested set of user controls. I don't want to handle the Space key when they focus on the TextBox control, as they can never enter a space. How can I check the type of the current focused window based on the application?

+4
source share
2 answers

You can get a window handle with this:

[DllImport("user32.dll")] private static extern IntPtr GetFocus(); 

You can then get the .NET control associated with this handle (if any) with Control.FromHandle .

+6
source

Find something that works:

 [DllImport("user32.dll")] static extern IntPtr GetFocus(); protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { IntPtr wndHandle = GetFocus(); Control focusedControl = FromChildHandle(wndHandle); if(focusedControl is DevExpress.XtraEditors.TextBoxMaskBox) { return base.ProcessCmdKey(ref msg, keyData); } ... } 
0
source

All Articles