Home > database >  Uncheck all checkboxes in a TCheckListBox
Uncheck all checkboxes in a TCheckListBox

Time:09-23

I want the user to be able to press a button to reset a TCheckListBox to it's initial state (having no boxes checked).

See images below of an example of what I want to happen after clicking the button:

Multiple checked boxes in a TCheckBoxList

Changed to:

No boxes are checked

How would I go about doing this? I know there would probably be a loop involved, but I'm unsure where to start. Thanks for the help in advance.

CodePudding user response:

There is nice method - look at official help.

If we open TCheckListBox help page, choose Methods and filter off "inherited" ones, we'll see CheckAll method

CheckListBox1.CheckAll(cbUnchecked);

CodePudding user response:

For individual checkboxes (missed this is a TCheckListBox).

Something along these lines,

Let's assume the checkboxes are on a panel called panel1.

var n: Integer;
begin
  for n := 0 to panel1.ComponentCount - 1 do 
     if panel1.Components[n] is TCheckbox then 
        Tcheckbox(panel1.components[n]).checked := False;
end; 

Note: if there is an event associated with the checkbox, you need to set the event to nil before modifying it, and returning the event after - else, the event will trigger as if you clicked the box.

  • Related