将对象添加到TListBox时出错?

时间:2013-11-19 20:43:52

标签: delphi listbox delphi-xe5

FMX的一个显而易见的功能是将可视对象添加到TListBox。在上一个问题中,我提到了correctly positioning objects inside the TListBox的麻烦。我发现的另一个烦恼是使用InsertObject。当将8个对象添加到TListBox时,我得到“参数超出范围”。在Android上,我可以在添加第二个对象时收到此错误消息。我可以在一个小程序中隔离这个问题:

  unit tlist_item;

  interface

  uses
    System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
    FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls;

  type
     TItem_Frame = class(TFrame)
      Button1: TButton;
      Label1: TLabel;
      SpeedButton1: TSpeedButton;

      procedure SpeedButton1Click (Sender: TObject);

     public
        FOnInsert: TNotifyEvent;
        class var FSequence: Int32;

        constructor Create (AOwner: TComponent); override;
     end; // Class: TFrame1 //

  implementation

  {$R *.fmx}

  constructor TItem_Frame.Create (AOwner: TComponent);
  begin
     inherited Create (AOwner);

     Self.Name := Format ('Frame_%d', [FSequence]);
     FSequence := FSequence + 1;
  end; // Create //

  procedure TItem_Frame.SpeedButton1Click (Sender: TObject);
  begin
     if Assigned (FOnInsert)
        then FOnInsert (Sender);
  end;

  end.

上面的代码描述了一个包含三个组件的TFrame。按下速度按钮,对象将添加到主程序的列表框中,如下所示:

  unit tlist_main;

  interface

  uses
     System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
     FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
     FMX.ListBox,
     tlist_item;

  type
     TMain_Form = class(TForm)
      ListBox: TListBox;
      procedure FormCreate(Sender: TObject);

     private
        procedure do_insert (Sender: TObject);

     public

     end; // Class: TForm1 //

  var
    Main_Form: TMain_Form;

  implementation

  {$R *.fmx}

  procedure TMain_Form.FormCreate (Sender: TObject);
  begin
     do_insert (ListBox);
  end; // FormCreate //

  procedure TMain_Form.do_insert (Sender: TObject);
  var
     frame: TItem_Frame;
  begin
     frame := TItem_Frame.Create (ListBox);
     frame.Parent := ListBox;
     frame.Position.Y := frame.FSequence * frame.Height;
     frame.Width := ListBox.Width;
     frame.FOnInsert := do_insert;
     ListBox.InsertObject (frame.FSequence - 1, frame);
  end; // do_insert //

  end.

在InsertObject调用的do_insert中发生错误,并且在Win32和Android上都会发生。我想知道这是一个错误还是我的错误。谁能帮助我?

1 个答案:

答案 0 :(得分:0)

试试这个:

procedure TMain_Form.do_insert (Sender: TObject);
  var
     frame: TItem_Frame;
  begin
     frame := TItem_Frame.Create (ListBox);
     frame.Parent := ListBox;
     frame.align := TAlignLayout.alTop;
     frame.Height := 50;
     frame.FOnInsert := do_insert;
     ListBox.InsertObject (frame.FSequence - 1, frame);
  end;

或查看此链接如何制作自定义ListBox项目: Create custom Listbox item