Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can have Indeterminate CheckBox changed to Checked instead of Unchecked?

Tags:

c#

winforms

I have a CheckBox that is set to CheckState.Indeterminate. When a timer expires it will be set to CheckState.Unchecked and turn off an external output signal. So far so good.

With the current code when the user clicks on the Indeterminate CheckBox it will become an Unchecked CheckBox. I would like to intercept it and make it a Checked CheckBox.

The effect for the user would be to cancel the timer and leave the output on until the user unchecks the box or the user starts a separate process that takes over and the Indeterminate CheckBox` is set again.

As it is he turns off the output and has to explicitly turn it on again.

I tried the validating event, but that doesn't happen until I leave the box.

Update 1: To clarify a little bit what the user wants to see. When the lights are off the box is always unchecked.

When the automated part of the system is running, the light is on. If the user looks at the control it is in the indeterminate state because the user did not activate it it, but it is on.

When the process stops a timer is started leaving the lights on for 2 minutes. The user still sees the indeterminate state during this time.

If the user needs the light to remain on he will check the box removing the indeterminate state. When he is through he will manually uncheck the box turning off the light or restart the automated process which will make it indeterminate again.

I wasn't aware of the AutoCheck property

like image 488
Rich Shealer Avatar asked Dec 10 '25 19:12

Rich Shealer


1 Answers

There is no need for anything drastic, you just don't like the way that Checkbox implemented its AutoCheck property. So select the control and set its AutoCheck property to False. That let's you control the check state the way you want it.

Add the Click event handler to allow the user to flip the state straight from Indeterminate to Checked with a mouse click or spacebar press:

    private void checkBox1_Click(object sender, EventArgs e) {
        checkBox1.CheckState = checkBox1.CheckState == CheckState.Checked ?
            CheckState.Unchecked : CheckState.Checked;
    }
like image 129
Hans Passant Avatar answered Dec 12 '25 09:12

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!