I'd like to delete checked items from CheckListBox by cliicking TButton but I've found only how to delete selected item and it's not what I'm looking for. I hope you can help me
CodePudding user response:
This code will do what you want. Note that CheckBoxList1.Items.Count
is decreasing by one every time an item is deleted. For this reason we're using downto
instead of do
in the for-loop.
procedure TForm1.Button1Click(Sender: TObject);
var
index: Integer;
begin
CheckListBox1.Items.BeginUpdate;
try
for index := (CheckListBox1.Items.Count - 1) downto 0 do
if (CheckListBox1.Checked[index]) then
CheckListBox1.Items.Delete(index);
finally
CheckListBox1.Items.EndUpdate;
end;
end;
The CheckListBox1.Items.BeginUpdate;
and CheckListBox1.Items.EndUpdate;
statements ensure that the control doesn't repaint itself while we're processing its items.
CodePudding user response:
if i understand what you need, try following code:
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
count : Integer;
begin
i:= 0;
count := CheckListBox1.Items.Count - 1;
while i <= count do
if CheckListBox1.Checked[i] = true then
begin
CheckListBox1.Items.Delete(i);
count := count - 1;
end else i:=i 1;
end;
Of curse, there are better solutions, but it is ready to use for you.