如何访问TNewComboBox类的ItemIndex属性?

问题描述 投票:0回答:1

目标

我正在帮助一位朋友使用 Inno Setup 结合 VCL-Styles 插件制作安装程序。我目前正在尝试访问一个索引,该索引据称与组合框中的每个选项相关联,为此我使用

TNewComboBox.ItemIndex

错误

由于某种原因,访问/返回该属性对我们不起作用,我们收到此错误:

代码

这是代码

Source:  "C:\Program Files (x86)\The Road To Delphi\VCL Styles Inno\VclStylesinno.dll"; DestDir: "{app}";
var
    cb: TNewComboBox;

procedure DropDownChange(Sender : TObject );
begin
    case cb.ItemIndex of  // <------- Error line 76 "Could not Call proc"
        0:
        begin
            Delete(cfgStr, Pos(cvar, cfgStr) + Length(cvar), 1);
            Insert('0', cfgStr, Pos(cvar, cfgStr) + Length(cvar));
        end;
        1:
        begin
            Delete(cfgStr, Pos(cvar, cfgStr) + Length(cvar), 1);
            Insert('1', cfgStr, Pos(cvar, cfgStr) + Length(cvar));
        end;
        2:
        begin
            Delete(cfgStr, Pos(cvar, cfgStr) + Length(cvar), 1);
            Insert('2', cfgStr, Pos(cvar, cfgStr) + Length(cvar));
        end;
        3:
        begin
            Delete(cfgStr, Pos(cvar, cfgStr) + Length(cvar), 1);
            Insert('3', cfgStr, Pos(cvar, cfgStr) + Length(cvar));
        end;
        4:
        begin
            Delete(cfgStr, Pos(cvar, cfgStr) + Length(cvar), 1);
            Insert('4', cfgStr, Pos(cvar, cfgStr) + Length(cvar));
        end;
        5:
        begin
            Delete(cfgStr, Pos(cvar, cfgStr) + Length(cvar), 1);
            Insert('0', cfgStr, Pos(cvar, cfgStr) + Length(cvar));
        end;
    end
end;

procedure CreateDropDown(page: TWizardPage; caption: String; cb: TNewComboBox; cvar: String; index: Integer; item0, item1, item2, item3, item4, item5 : String);
var
    newLabel: TLabel;
begin
    newLabel:= TLabel.Create(page);
    newLabel.Parent:= page.Surface;
    newLabel.Caption:= caption;
    case page of
        SettingsPageA:
        begin
            newLabel.Left:= curWidthA;
            newLabel.Top:= curHeightA;
            case curWidthA of
                0:
                begin
                    curWidthA:= curWidthA + 264;
                end;
                264:
                begin
                    curWidthA:= curWidthA + 264;
                end;
                528:
                begin
                    curWidthA:= 0;
                    curHeightA:= curHeightA + 66;
                end;
            end;
        end;
        SettingsPageB:
        begin
            newLabel.Left:= curWidthB;
            newLabel.Top:= curHeightB;
            case curWidthB of
                0:
                begin
                    curWidthB:= curWidthB + 264;
                end;
                264:
                begin
                    curWidthB:= curWidthB + 264;
                end;
                528:
                begin
                    curWidthB:= 0;
                    curHeightB:= curHeightB + 66;
                end;
            end;
        end;
        SettingsPageC:
        begin
            newLabel.Left:= curWidthC;
            newLabel.Top:= curHeightC;
            case curWidthC of
                0:
                begin
                    curWidthC:= curWidthC + 264;
                end;
                264:
                begin
                    curWidthC:= curWidthC + 264;
                end;
                528:
                begin
                    curWidthC:= 0;
                    curHeightC:= curHeightC + 66;
                end;
            end;
        end;
    end;
    newLabel.Width:= 247;
    newLabel.Height:= 25;
    newLabel.Transparent:= True;
    
    cb:= TNewComboBox.Create(page);
    cb.Parent:= page.Surface;
    cb.Left:= newLabel.Left;
    cb.Top:= newLabel.Top + newLabel.Height + 4;
    cb.Width:= newLabel.Width;
    cb.Height:= newLabel.Height;
    cb.Style:= csDropDownList;
    cb.Items.Add(item0);
    if Length(item1) > 0 then
    begin
        cb.Items.Add(item1);
        if Length(item2) > 0 then
        begin
            cb.Items.Add(item2);
            if Length(item3) > 0 then
            begin
                cb.Items.Add(item3);
                if Length(item4) > 0 then
                begin
                    cb.Items.Add(item4);
                    if Length(item5) > 0 then
                    begin
                        cb.Items.Add(item5);
                    end;
                end;
            end;
        end;
    end;
  cb.ItemIndex := index;
  cb.OnChange:= @DropDownChange; <---------------
end;

这只是每当组合框更改选择/索引时执行

.OnChange
的一段代码。谢谢!


我被要求提供如下所示的可重现代码的最小示例

procedure DropDownChanged(Sender: TObject);
begin
    Log(IntToStr(cb.ItemIndex);
end;

procedure CreateDropDown(page: TWizardPage; caption: String; cb: TNewComboBox; cvar: String; index: Integer; item0, item1 : String);
var
    newLabel: TLabel;
begin
    newLabel:= TLabel.Create(page);
    newLabel.Parent:= page.Surface;
    newLabel.Caption:= caption;
    newLabel.Left:= curWidth;
    newLabel.Top:= curHeight;
    
    newLabel.Width:= 247;
    newLabel.Height:= 25;
    newLabel.Transparent:= True;
    
    cb:= TNewComboBox.Create(page);
    cb.Parent:= page.Surface;
    cb.Left:= newLabel.Left;
    cb.Top:= newLabel.Top + newLabel.Height + 4;
    cb.Width:= newLabel.Width;
    cb.Height:= newLabel.Height;
    cb.Style:= csDropDownList;
    cb.Items.Add(item0);
    cb.Items.Add(item1);
    cb.ItemIndex:= index;
    cb.OnChange:= DropDownChanged;
end;

并执行它:

CreateDropDown(wpReady, 'Caption', 'ComboBox', 0, 'First Item', 'Second Item');

编辑2

这是这个小项目的原作者。我已经淡化了重现问题所需的代码,如下所示:

[Code]
var
    ExamplePage: TWizardPage;
    cb: TNewComboBox;

procedure DropDownChanged(Sender: TObject);
begin
    Log(IntToStr(cb.ItemIndex));
end;

procedure CreateDropDown(page: TWizardPage; cb: TNewComboBox; index: Integer; item0, item1 : String);
begin
    cb:= TNewComboBox.Create(page);
    cb.Parent:= page.Surface;
    cb.Style:= csDropDownList;
    cb.Items.Add(item0);
    cb.Items.Add(item1);
    cb.ItemIndex:= index;
    cb.OnChange:= @DropDownChanged;
end;

procedure CreateExamplePage;
var
    ExampleComboBox: TNewComboBox;
begin
    ExamplePage:= CreateCustomPage(wpWelcome, 'Example Page', 'This is an example');
    CreateDropDown(ExamplePage, ExampleComboBox, 0, 'First Item', 'Second Item');
end;

procedure InitializeWizard;
begin
    CreateExamplePage;
end;

这是在下拉列表中选择一个项目时重现错误所需的最低限度。我希望这有助于解决问题!

inno-setup pascalscript
1个回答
2
投票

全局

cb
变量与
cb
函数的局部
CreateDropDown
参数之间存在名称冲突。您分配局部参数(这没什么意义),而全局变量从未分配。另外,不太清楚
ExampleComboBox
的目的是什么。

也许你想要这样的东西?

[Code]
var
  ExamplePage: TWizardPage;
  ExampleComboBox: TNewComboBox;

procedure DropDownChanged(Sender: TObject);
begin
  Log(IntToStr(ExampleComboBox.ItemIndex));
end;

function CreateDropDown(
  page: TWizardPage; index: Integer; item0, item1: String): TNewComboBox;
begin
  Result := TNewComboBox.Create(page);
  Result.Parent:= page.Surface;
  Result.Style:= csDropDownList;
  Result.Items.Add(item0);
  Result.Items.Add(item1);
  Result.ItemIndex:= index;
  Result.OnChange:= @DropDownChanged;
end;

procedure CreateExamplePage;
begin
  ExamplePage :=
    CreateCustomPage(wpWelcome, 'Example Page', 'This is an example');

  ExampleComboBox :=
    CreateDropDown(ExamplePage, 0, 'First Item', 'Second Item');
end;

procedure InitializeWizard;
begin
  CreateExamplePage;
end;

DropDownChanged
不必依赖全局变量。如果您希望它适用于其他组合框,请改为依赖
Sender
参数

Log(IntToStr(TNewComboBox(Sender).ItemIndex));
© www.soinside.com 2019 - 2024. All rights reserved.