My small windows form application has two radio buttons, and initially neither is checked. Until one of them is checked, the “Go” button should be disabled.
I found a very simple way to obtain this, but I’m not sure if I’m setting myself up for a random crash.
I added a timer component, enabled it, and in the Tick event:
private void timer1_Tick(object sender, EventArgs e)
{
bool canGo = (_usRadioButton.Checked || _intlRadioButton.Checked);
if (_goButton.Enabled != canGo)
{
_goButton.Enabled = canGo;
}
}
I know there are other ways to do this, I’m just curious if this way is valid or if I’m going to have an end user throwing exceptions when the timer is firing at the same time the form is updating the Checked property on one of the checkboxes?
If I understood you correctly, you already know about the CheckedChanged event, and are only asking about conflicts in your code. So:
As far as I know it’s not multithreading, and there’s no danger. The Tick event will actually not fire at the same time the computer is updating the Checked state.
If you place traces in Form.Load and Timer.Tick:
The results show: