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;