I recently had a similar problem. My interface is very complex with lots of panels and tabs, so none of the simpler answers that I found worked.
My solution was to programmatically add a mouse click handler for each non-focusable control in my form that would try to focus any labels on the form. Focusing a specific tag will not work if it is on a different tab, so I ended the loop and concentrated all the tags.
Code to execute:
private void HookControl(Control controlToHook) { // Add any extra "unfocusable" control types as needed if (controlToHook.GetType() == typeof(Panel) || controlToHook.GetType() == typeof(GroupBox) || controlToHook.GetType() == typeof(Label) || controlToHook.GetType() == typeof(TableLayoutPanel) || controlToHook.GetType() == typeof(FlowLayoutPanel) || controlToHook.GetType() == typeof(TabControl) || controlToHook.GetType() == typeof(TabPage) || controlToHook.GetType() == typeof(PictureBox)) { controlToHook.MouseClick += AllControlsMouseClick; } foreach (Control ctl in controlToHook.Controls) { HookControl(ctl); } } void AllControlsMouseClick(object sender, MouseEventArgs e) { FocusLabels(this); } private void FocusLabels(Control control) { if (control.GetType() == typeof(Label)) { control.Focus(); } foreach (Control ctl in control.Controls) { FocusLabels(ctl); } }
Then add this to the Form_Load event:
HookControl(this);
source share