仰望它的标题的复选框看起来像一个可怕的想法。
这的确是可行的:
Index := CheckListBox.Items.IndexOf('Prog 1');
if (Index >= 0) and CheckListBox.Checked[Index] then
begin
{ checked }
end
else
begin
{ does not exist or unchecked }
end;
但这不是正确的方法。
TCheckListBox
的目的是允许从一些数据中产生一个复选框列表,在一个循环中。什么确实是way you are using it。
您试图通过其标题查找复选框表示您想要为每个复选框编写专用代码。这违反了TCheckListBox
的目的。
而是,在处理用户选择时,使用与生成列表时相同的方法,使用循环。
code I have shown you to generate the checkbox list,按照目的生成Dirs: TStringList
中具有相同索引的关联路径的列表。
所以遍历该列表与复选框一起处理路径:
{ Iterate the path list }
for Index := 0 to Dirs.Count - 1 do
begin
{ Is the associated checkbox checked? }
if CheckListBox.Checked[Index] then
begin
{ Process the path here }
MsgBox(Format('Processing path %s', [Dirs[Index]]), mbInformation, MB_OK);
{ In your case, you delete the folder }
DelTree(Dirs[Index], True, True, True);
end;
end;
以上其实是相似的代码,你已经在我以前的答案。
这是一个概念,我已经在你的另一个问题中向你展示了:Inno Setup - Check if multiple folders exist。
如果个别复选框真的需要特殊处理(即它们不代表定性同一项目的列表),正确的方法是在时间记住他们的指数你生成它们:
if ShouldAddItem1 then
Item1Index := CheckListBox.AddCheckBox(...)
else
Item1Index := -1;
if ShouldAddItem2 then
Item2Index := CheckListBox.AddCheckBox(...)
else
Item2Index := -1;
if (Item1Index >= 0) and CheckListBox.Checked[Item1Index] then
{ Process item 1 }
if (Item2Index >= 0) and CheckListBox.Checked[Item2Index] then
{ Process item 2 }
不会'CheckListBox.Items.IndexOf( '项目说明')'帮助找到索引? – Victoria
你能给我们一些背景吗?你需要什么特别的? [我的回答您的上一个问题](https://stackoverflow.com/a/44254371/850848)显示如何处理复选框。 –
你实际上一直在问类似的问题:https://stackoverflow.com/q/44274005/850848 - 看起来你正从错误的一面看问题 - 所以你想要做什么,一旦你得到一个参考复选框?向我们展示一些代码!通过标题查找复选框看起来像一个可怕的想法。 –