Rich Shealer Rich Shealer - 1 month ago 5
C# Question

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

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

Answer

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;
    }
Comments