Delphi XE2 之 FireMonkey 入门(41) - 控件基础: TListBox


TScrollBox -> TCustomListBox -> TListBox; 其元素项是 TListBoxItem 类型.

TListBox 的功能在 TCustomListBox 里就完成了.

值得注意的变化是:
1、复选框(相关属性: ShowCheckboxes、TListBoxItem.IsChecked)
2、交替背景(通过继承还可以调整交替的背景色)
3、TListBoxItem 可调整大小、容纳其它对象.

{ TCustomListBox }
public
  constructor Create(...); override;          //
  destructor Destroy; override;               //
  procedure Assign(...); override;            //
  procedure Clear; virtual;                   //清空
  function DragChange(...): Boolean; dynamic; //调用 OnDragChange 事件
  procedure SelectAll;                        //全选
  procedure ClearSelection;                   //取消选择
  procedure SelectRange(...);                 //选择指定范围
  function ItemByPoint(...): TListBoxItem;    //获取指定位置的项
  function ItemByIndex(...): TListBoxItem;    //获取指定序号的项
  procedure Exchange(...);                    //交换项
  procedure AddObject(...); override;         //添加项
  procedure RemoveObject(...); override;      //删除项
  procedure Sort(...); override;              //排序
  property Count: Integer ...;                //项总数
  property Selected: TListBoxItem ...;        //当前选择的项
  property Items: TStrings ...;               //元素文本的集合
  property ListItems[Index: Integer]: TListBoxItem ...; //根据索引获取项
  property ItemIndex: Integer ...;                      //索引
end;

{ TListBox }
published
  property StyleLookup;              //
  property AllowDrag;                //是否允许拖放
  property CanFocus;                 //
  property DisableFocusEffect;       //
  property TabOrder;                 //
  property AlternatingRowBackground; //是否使用交替背景
  property Columns;                  //列数; 默认 1
  property HideSelectionUnfocused;   //在失去焦点时是否隐藏选择
  property Items;                    //
  property ItemIndex;                //
  property ItemWidth;                //项宽
  property ItemHeight;               //项高
  property ListStyle;                //列表样式; TListStyle = (lsVertical, lsHorizontal);
  property MultiSelect;              //是否允许多选; 为 True 时, 配合 Ctrl 键多选
  property Sorted;                   //
  property ShowCheckboxes;           //是否显示复选框; 默认 False
  property BindingSource;            //绑定源
  property OnChange;                 //有变化时
  property OnChangeCheck;            //调整复选框时
  property OnCompare;                //排序比较时
  property OnDragChange;             //拖放项时
end;

{ TListBoxItem }
public
  constructor Create(...); override; //
  property Data: TObject ...;        //
  property Index: Integer ...;       //
published
  property IsChecked: Boolean ...;   //复选值
  property IsSelected: Boolean ...;  //
  property AutoTranslate ...;        //
  property Font;                     //
  property StyleLookup;              //
  property Text;                     //
  property TextAlign ...;            //
  property WordWrap;                 //
end;


测试:

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  ListBox1.Align := TAlignLayout.alLeft;
  ListBox1.ShowCheckboxes := True;
  ListBox1.AlternatingRowBackground := True;
  for i := 0 to 9 do
  begin
    ListBox1.Items.Add('Itme' + IntToStr(i));
    ListBox1.ListItems[i].IsChecked := Odd(i);
  end;
end;

你可能感兴趣的:(listbox)