按个别复选框标题阅读复选框列表状态

时间:2017-05-31 17:24:14

标签: inno-setup pascal pascalscript

我有几个有条件可见的复选框,这意味着它们的索引不是静态的。在这种情况下,将动作绑定到例如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;

1 个答案:

答案 0 :(得分: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 }