问题
I need to write a delegate for a multi-threaded program that will enable/disable a variety of controls. It seems logical that using one handler for all controls would be the best choice, but I'm not even sure this is possible in .net and if so how to implement.
回答1:
public void SetControlsEnabled(bool enabled)
{
// Make sure we're in the correct thread
if (InvokeRequired)
{
// If not, run the method on the UI thread
Invoke(new MethodInvoker(() => SetControlsEnabled(enabled)));
return;
}
// Put all control code here, e.g:
// control1.Enabled = enabled;
// control2.Enabled = enabled;
// Alternatively, do a foreach(Control c in Controls) { ... }
}
来源:https://stackoverflow.com/questions/1585163/can-i-use-a-net-control-parent-class-to-enable-disable-it