Skip to content

Instantly share code, notes, and snippets.

@fliedonion
Created October 7, 2018 13:24
Show Gist options
  • Select an option

  • Save fliedonion/220e12370dcf73c784c8e70abcfa3d74 to your computer and use it in GitHub Desktop.

Select an option

Save fliedonion/220e12370dcf73c784c8e70abcfa3d74 to your computer and use it in GitHub Desktop.
WnForms Control Enable to ignore many clicks during controls were disabling.
namespace net.case_of_t.WinForms {
public class SomeFormBase : Form{
protected static readonly int DefaultWaitMsForTurnEnable = 1000;
protected void ControlsEnableAfter(Control target) {
ControlsEnableAfter(DefaultWaitMsForTurnEnable, target);
}
protected void ControlsEnableAfter(IEnumerable<Control> targets) {
ControlsEnableAfter(DefaultWaitMsForTurnEnable, targets);
}
protected void ControlsEnableAfter(params Control[] targets) {
ControlsEnableAfter(DefaultWaitMsForTurnEnable, targets);
}
protected void ControlsEnableAfter(int ms, Control target) {
ControlsEnableAfter(ms, new List<Control> { target });
}
protected void ControlsEnableAfter(int ms, params Control[] targets) {
if (targets == null || targets.Length == 0) return;
ControlsEnableAfter(ms, targets as IEnumerable<Control>);
}
protected void ControlsEnableAfter(int ms, IEnumerable<Control> targets) {
Task.Run(() => {
System.Threading.Thread.Sleep(ms);
Invoke(new Action(() => {
foreach (var target in targets)
target.Enabled = true;
}));
});
}
}
}
namespace net.case_of_t.WinForms {
public class UsageForm {
private void button1_Click(object sender, EventArgs e){
button1.Enabled = false;
button2.Enabled = false;
// some heavy code
// usage 1
Controls.EnableAfter(button1);
Controls.EnableAfter(button2);
// usage 2
Controls.EnableAfter(button1, button2);
// usage 3
Controls.EnableAfter(new List<Control> { button1, button2 });
}
private void button2_Click(object sender, EventArgs e){
button1.Enabled = false;
button2.Enabled = false;
Controls.EnableAfter(button1, button2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment