动态列表

本例效果图:



代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure btnClick(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  btnArr: array[0..3] of TButton;

procedure TForm1.btnClick(Sender: TObject);
var
  i,n: Integer;
begin
  n := TButton(Sender).Tag;
  for i := 0 to Length(btnArr) - 1 do
  begin
    if i>n then
      btnArr[i].Align := alBottom
    else 
      btnArr[i].Align := alTop;
  end;

  ListBox1.Items.Add(TButton(Sender).Caption); 
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  btnArr[0] := Button1;
  btnArr[1] := Button2;
  btnArr[2] := Button3;
  btnArr[3] := Button4;

  for i := 0 to Length(btnArr) - 1 do
  begin
    btnArr[i].OnClick := btnClick;
    btnArr[i].Tag := i;
  end;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  ShowMessage(ListBox1.Items[ListBox1.ItemIndex]);
end;

end.

窗体文件:
object Form1: TForm1
  Left = 366
  Top = 238
  Caption = 'Form1'
  ClientHeight = 206
  ClientWidth = 266
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 145
    Height = 206
    Align = alLeft
    Caption = 'Panel1'
    TabOrder = 0
    object Button1: TButton
      Left = 1
      Top = 1
      Width = 143
      Height = 25
      Align = alTop
      Caption = 'Button1'
      TabOrder = 0
    end
    object Button2: TButton
      Left = 1
      Top = 130
      Width = 143
      Height = 25
      Align = alBottom
      Caption = 'Button2'
      TabOrder = 1
    end
    object Button3: TButton
      Left = 1
      Top = 155
      Width = 143
      Height = 25
      Align = alBottom
      Caption = 'Button3'
      TabOrder = 2
    end
    object Button4: TButton
      Left = 1
      Top = 180
      Width = 143
      Height = 25
      Align = alBottom
      Caption = 'Button4'
      TabOrder = 4
    end
    object ListBox1: TListBox
      Left = 1
      Top = 26
      Width = 143
      Height = 104
      Align = alClient
      ItemHeight = 13
      TabOrder = 3
      OnClick = ListBox1Click
    end
  end
end

原文地址:https://www.cnblogs.com/del/p/1176145.html