How to Toggle a Tri-State CheckBox?

I have a column on my grid which is a Tri State CheckBox. I'd like the user to be able to click the checkbox and it to only toggle between checked and unchecked (i.e. no Gray state). So the behavior of a click will be:


cbUnchecked to cbChecked
cbGrayed to cbChecked
cbChecked to cbUnchecked

What's the easiest way to accomplish this?

Thanks - Steve

I have retested this here with:


  advstringgrid1.AddCheckBox(1,1, cbUnchecked);
  advstringgrid1.AddCheckBox(1,2, cbchecked);
  advstringgrid1.AddCheckBox(1,3, cbGrayed);

when clicking these checkboxes, the checkbox state cycles through cbUnchecked, cbGrayed, cbChecked.

Hi Bruno,


Let me rephrase. I'd like to display all three states but if the user edits (i.e. click on a checkbox), I only want two states i.e. checked and unchecked.

Steve

I seem to have figured it out. This works for me:


procedure TDistrictTableView.OnCheckBoxChange(Sender: TObject; ACol, ARow: Integer; State: Boolean);
var
  aState: TCheckBoxState;
  NewState: boolean;
begin
  case ACol of
    0:
    begin
      fDistrictList[ARow - 2].Checked := State;
      fGrid.RepaintRow(ARow);
    end;
    4:
    begin
      fGrid.GetCheckBoxState(ACol, ARow, aState);
      case aState of
        cbUnchecked: NewState := false;
        cbChecked: NewState := true;
        cbGrayed:
        begin
          NewState := true;
          fGrid.OnCheckBoxChange := nil;
          fGrid.SetCheckBoxState(ACol, ARow, cbChecked);
          fGrid.OnCheckBoxChange := OnCheckBoxChange;
        end;
      end;
      fCommandCenter.ChangeDistrictVisible(fDistrictList[ARow - 2], NewState);
    end;
  end;
end;

De toggling of a tristate checkbox is built-in between all 3 states, i.e. including the grayed state. I understand that in some cases it can be desirable to not allow the user to check the grayed state. We have therefore added a property grid.Navigation.ToggleTriStateCheck to control this behavior and enabling to select between the two to make this more convenient. This will be included in the next update.

Great!


I assume that the default will be "true" to maintain backward compatibility.

- Steve

Yes, the default behavior won't change.