class PanelScrollHelper
{
public static void InitializePanelScroll(Panel panel)
{
panel.Click += (obj, arg) => { panel.Select(); };
InitializePanelScroll(panel, panel);
return;
}
private static void InitializePanelScroll(Control container, Control panelRoot)
{
foreach (Control control in container.Controls)
{
if (control is Panel || control is GroupBox || control is SplitContainer ||
control is TabControl || control is UserControl)
{
control.Click += (obj, arg) => { panelRoot.Select(); };
InitializePanelScroll(control, panelRoot);
}
else if (control is Label)
{
control.Click += (obj, arg) => { panelRoot.Select(); };
}
}
}
}
使用方法:
private void Form1_Load(object sender, EventArgs e)
{
PanelScrollHelper.InitializePanelScroll(pnlLeft);
PanelScrollHelper.InitializePanelScroll(pnlRight);
}