Tri-state checkboxes

If a checkbox’s state is set to Indeterminate in the IDE, the user is presented with a tri-state checkbox, else with a dual-state one.

Now as soon as the state changes in code, like that:

CheckBox1.State = CheckBox.CheckStates.Checked

…or that:

CheckBox1.State = CheckBox.CheckStates.Unchecked

… the checkbox has lost its tri-state mode.

What am I doing wrong here?

its CheckBox1.State = CheckBox.CheckedStates.Checked. Otherwise it triggers an error.

I tried placing each state in buttons and the control does go indetermined with

CheckBox1.State = CheckBox.CheckedStates.Indeterminate

What happens ? You cannot get the indeterminate state ?

You are right, thanks for pointing this out.

This is what I did:

  • Place a CheckBox on a window.
  • Set the state of the check box to Indeterminate in the window editor.
  • Place a PushButton on the window.
  • Add the following code to the button’s Action event:
CheckBox1.State = CheckBox.CheckedStates.Checked
  • Run the project. The tri-state works for the user.
  • Click on the PushButton. The check box is in dual-state mode now for the user.

You are right. Here is a workaround :

Function MouseDown(X As Integer, Y As Integer) As Boolean if X>3 and X<16 and Y>5 and Y <18 then select case me.State case CheckBox.CheckedStates.Checked me.State = CheckBox.CheckedStates.UnChecked case CheckBox.CheckedStates.UnChecked me.State = CheckBox.CheckedStates.Indeterminate case CheckBox.CheckedStates.Indeterminate me.State = CheckBox.CheckedStates.Checked end end if return true End Function

I check the values of X and Y to make sure the user clicks within the box. Otherwise a click anywhere in the checkbox label would change the state.

1 Like

I wonder if that is by intention or if it is a bug. Thanks for the work-around.

In the past, checkboxes had two states. Maybe when they implemented Indeterminate, they forgot to change the click change. I believe it is a bug, though.

Don’t think so. most of the time you only want 2 states so you donut want it to cycle through 3.

The “indeterminate” state is usually the result of the state of of something else and so most of the time needs to be set in code .

Aso in your workaround you make the use click on teh checkbox itself… I think the standard UI is having it change if the click is on the text as well.

Possibly, but Eli wanted 3 states…

Indeed, in the standard UI the state changes when clicked on the text, but not on the transparent part of the label. The workaround can be modified to accomodate that. Luxury solution calls for measuring the size of the text with StringWidth and add that to the tested max value of x. A quick fix can simply be to increase the max value of it approximatively.