我想通过裁剪CheckListBox删除选中的项目,但是我只找到了如何删除所选的项,这不是我想要的。我希望你能帮我
发布于 2021-12-15 16:26:10
这段代码可以做你想做的事。请注意,每次删除项目时,CheckBoxList1.Items.Count都会减少一个。因此,我们在For -循环中使用downto而不是do。
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;CheckListBox1.Items.BeginUpdate;和CheckListBox1.Items.EndUpdate;语句确保控件在处理其项时不会重新绘制其自身。
发布于 2021-12-15 15:59:47
如果我理解您需要什么,请尝试以下代码:
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;诅咒,有更好的解决办法,但它已准备好供你使用。
https://stackoverflow.com/questions/70365892
复制相似问题