2017-05-31 64 views
1

我有几个有条件可见的复选框,这意味着他们的索引是不是静态的。在这种情况下,将动作与例如CheckListBox.Checked[0]是无用的,因为0是每次不同的复选框。有没有办法查看是否选中了标题为foo的复选框?阅读复选框列表状态由个别复选框标题

我试图做到这一点:

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep); 
begin 
    if CurUninstallStep = usUninstall then 
    begin 
    if CheckListBox.Checked[0] then 
     DelTree(ExpandConstant('{appdata}\Dagonybte\Prog1'), True, True, True) 
    if CheckListBox.Checked[1] then 
     DelTree(ExpandConstant('{appdata}\Dagonybte\Prog2'), True, True, True) 
     { ... } 
    if CheckListBox.Checked[2] then 
     DelTree(ExpandConstant('{appdata}\Dagonybte\Prog3'), True, True, True) 
    end 
end; 
+0

不会'CheckListBox.Items.IndexOf( '项目说明')'帮助找到索引? – Victoria

+0

你能给我们一些背景吗?你需要什么特别的? [我的回答您的上一个问题](https://stackoverflow.com/a/44254371/850848)显示如何处理复选框。 –

+0

你实际上一直在问类似的问题:https://stackoverflow.com/q/44274005/850848 - 看起来你正从错误的一面看问题 - 所以你想要做什么,一旦你得到一个参考复选框?向我们展示一些代码!通过标题查找复选框看起来像一个可怕的想法。 –

回答

3

仰望它的标题的复选框看起来像一个可怕的想法。

这的确是可行的:

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 } 
+0

你说得对,对不起。在提供解决方案之前,我对“CheckListBox”或“TCheckListBox”不熟悉,因此,试图弄清楚它们如何工作会导致愚蠢的问题。现在很清楚。让我知道我是否应该删除任何不相关的问题。 –

+0

不,这没关系。这些都不是问题。请注意,即使你使用独立的'TCheckBox',你也应该这样做 - 保留一个路径列表和相关的复选框,让它们在一个循环中被处理。 –