FireMonkey 入门(44) - 控件基础: TTreeView、TTreeViewItem

减小字体 增大字体 作者:佚名  来源:万一的博客  发布时间:2011-10-21 10:31:32

TScrollBox -> TCustomTreeView -> TTreeView 子项类型是 TTreeViewItem.

测试, 先在窗体上放一个 TTreeView, 并需要它的 OnChange 事件:
 

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Layouts, FMX.TreeView;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    procedure FormCreate(Sender: TObject);
    procedure TreeView1Change(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.FormCreate(Sender: TObject);
var
  item: TTreeViewItem;
  i: Integer;
begin
  TreeView1.Align := TAlignLayout.alLeft;
  TreeView1.UseSmallScrollBars := True;
  TreeView1.AlternatingRowBackground := True;
  TreeView1.ShowCheckboxes := True;

  item := TTreeViewItem.Create(Self);
  item.Parent := TreeView1;
  item.Text := 'Item1';

  item := TTreeViewItem.Create(Self);
  item.Parent := TreeView1;
  item.Text := 'Item2';
    for i := 0 to 19 do
      with TTreeViewItem.Create(Self) do
      begin
        Parent := item;
        Text := item.Text + '_' + IntToStr(i+1);
        IsChecked := Odd(i);
      end;
  item.IsExpanded := True;

  item := TTreeViewItem.Create(Self);
  item.Parent := TreeView1;
  item.Text := 'Item3';
end;

procedure TForm1.TreeView1Change(Sender: TObject);
var
  item: TTreeViewItem;
begin
  item := TreeView1.Selected;
  if item <> nil then
  begin
    ShowMessageFmt(
      'TreeView1.GlobalCount: %d'#13#10 +
      'TreeView1.Count: %d'#13#10 +
      'TTreeViewItem.Count: %d'#13#10 +
      'TTreeViewItem.Level: %d'#13#10 +
      'TTreeViewItem.GlobalIndex: %d'#13#10 +
      'TTreeViewItem.Index: %d'#13#10
      , [TreeView1.GlobalCount, TreeView1.Count, item.Count, item.Level, item.GlobalIndex, item.Index]
    );
  end;
end;

end.


成员概览:

{ TCustomTreeView }
public
  constructor Create(...); override; //
  destructor Destroy; override;      //
  procedure EndUpdate; override;     //结束更新
  procedure Clear;                   //清空
  procedure ExpandAll;               //全部展开
  procedure CollapseAll;             //全部关闭
  function ItemByText(...): TTreeViewItem;        //根据标题获取项
  function ItemByPoint(...): TTreeViewItem;       //根据位置获取项
  function ItemByIndex(...): TTreeViewItem;       //根据索引获取项
  function ItemByGlobalIndex(...): TTreeViewItem; //根据总索引获取项
  procedure AddObject(...); override;    //
  procedure RemoveObject(...); override; //
  procedure MouseDown(...); override;    //
  procedure MouseMove(...); override;    //
  procedure MouseUp(...); override;      //
  procedure KeyDown(...); override;      //
  procedure KeyUp(...); override;        //
  procedure DragOver(...); override;     //
  procedure DragDrop(...); override;     //
  property Count: Integer ...;           //第一层项总数
  property GlobalCount: Integer ...;     //项总数
  property CountExpanded: Integer ...;   //???
  property Selected: TTreeViewItem ...;  //当前选择的项
  property Items[Index: Integer]: TTreeViewItem ...;   //索引到项
  property AllowDrag: Boolean ...;                     //是否允许拖拽
  property AlternatingRowBackground: Boolean ...;      //是否使用交替背景
  property ItemHeight: Single ...;                     //项高
  property HideSelectionUnfocused: Boolean ...;        //是否在失去焦点时隐藏选择
  property MultiSelect: Boolean ...;                   //能否多选
  property ShowCheckboxes: Boolean ...;                //是否显示多选框
  property Sorted: Boolean ...;                        //
  property OnChange: TNotifyEvent ...;                 //
  property OnChangeCheck: TNotifyEvent ...;            //
  property OnCompare: TOnCompareTreeViewItemEvent ...; //
  property OnDragChange: TOnTreeViewDragChange ...;    //
end;

{ TTreeView }
published
  property StyleLookup;                            //
  property CanFocus default True;                  //
  property DisableFocusEffect;                     //
  property TabOrder;                               //
  property AllowDrag default False;                //
  property AlternatingRowBackground default False; //
  property ItemHeight;                             //
  property HideSelectionUnfocused default False;   //
  property MultiSelect default False;              //
  property ShowCheckboxes default False;           //
  property Sorted default False;                   //
  property OnChange;                               //
  property OnChangeCheck;                          //
  property OnCompare;                              //
  property OnDragChange;                           //
end;

{ TTreeViewItem }
public
  constructor Create(...); override;        //
  procedure Paint; override;                //
  procedure AddObject(...); override;       //
  procedure RemoveObject(...); override;    //
  procedure Sort(...); override;            //
  function ItemByPoint(...): TTreeViewItem; //根据位置获取项
  function ItemByIndex(...): TTreeViewItem; //根据索引获取项
  property Count: Integer ...;              //子项数
  property GlobalIndex: Integer ...;        //全局索引
  function TreeView: TCustomTreeView;       //拥有它的 TreeView
  function Level: Integer;                  //在第几层
  function ParentItem: TTreeViewItem;       //父项
  property Items[Index: Integer]: TTreeViewItem ...; //索引到子项
published
  property IsChecked: Boolean ...;     //是否选中复选框
  property IsExpanded: Boolean ...;    //是否展开
  property IsSelected: Boolean ...;    //是否选择
  property AutoTranslate default True; //是否自动接收语言翻译
  property Font;                       //
  property StyleLookup;                //
  property Text;                       //
  property TextAlign default TTextAlign.taLeading; //
end;
 

Tags:

作者:佚名
  • 好的评价 如果您觉得此文章好,就请您
      0%(0)
  • 差的评价 如果您觉得此文章差,就请您
      0%(0)

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论

广告位置B