从tcxtreelist获取已检查的节点

时间:2016-12-17 10:49:39

标签: delphi devexpress tadoquery

我有一个tcxtreelist 有谁知道如何获取所有checkedNodes?

我需要通过我的tcxtreelist 从tcxtreelist中获取一定的值 并将其写入以逗号分隔的字符串

任何人都可以帮我这个吗?

由于 亲切的问候

1 个答案:

答案 0 :(得分:1)

假设您有cxTreeList个列,colCheckedcolYearcolMonth

如果您转到IDE中的colChecked,则可以将其Properties属性设置为 CheckBox并在运行时将其用作复选框。

如何获取给定树节点中的Checked值实际上非常简单。 如果声明变量Node : TcxTreeList node,则可以将其分配给任何变量 树中的节点,如

Node := cxTreeList1.Items[i];

完成后,您可以获取节点的三列中的值 访问节点的Values属性,这是一个从零开始的变体数组 表示存储在节点中并显示在树中的值。 所以,你可以写

var
  Node : TcxTreeListNode;
  Checked : Boolean;
  Year : Integer;
  Month : Integer;

begin
  Node := cxTreeList1.Items[i];
  Checked := Node.Values[0];
  Year := Node.Values[1];
  Month := Node.Values[2];
end;

当然,您可以通过相反的分配来设置节点的Values 方向(但不要尝试使用db-aware版本,TcxDBTreeList,因为显示的值由字段的内容决定 连接到它的数据集)。

没有必要使用Node局部变量,我只是为了清晰起见。你可以轻松(但不是那么清楚)写

  Checked := cxTreeList1.Items[i].Values[0]

下面是一些示例代码,它使用复选框列设置cxTreeList,用行填充它,并生成一个选中复选框的行列表:

uses
  [...]cxTLData, cxDBTL, cxInplaceContainer, cxTextEdit,
  cxCheckBox, cxDropDownEdit;

type
  TForm1 = class(TForm)
    cxTreeList1: TcxTreeList;
    Memo1: TMemo;
    btnGetCheckedValues: TButton;
    procedure btnGetCheckedValuesClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
  protected
    colChecked : TcxTreeListColumn;
    colYear : TcxTreeListColumn;
    colMonth : TcxTreeListColumn;
  public
    procedure GetCheckedValues;
  end;

[...]
procedure TForm1.FormCreate(Sender: TObject);
var
  i : Integer;
  Year,
  Month : Integer;
  YearNode,
  MonthNode : TcxTreeListNode;
begin
  cxTreeList1.BeginUpdate;
  try
    //  Set up the cxTreeList's columns
    colChecked := cxTreeList1.CreateColumn(Nil);
    colChecked.Caption.Text := 'Checked';
    colChecked.PropertiesClassName := 'TcxCheckBoxProperties';

    colYear := cxTreeList1.CreateColumn(Nil);
    colYear.Caption.Text := 'Year';

    colMonth := cxTreeList1.CreateColumn(Nil);
    colMonth.Caption.Text := 'Month';

    //  Set up the top level (Year) and next level (Month) nodes
    for Year := 2012 to 2016 do begin
      YearNode := cxTreeList1.Root.AddChild;
      YearNode.Values[0] := Odd(Year);
      YearNode.Values[1] := Year;
      for Month := 1 to 12 do begin
        MonthNode := YearNode.AddChild;
        MonthNode.Values[0] := False;
        MonthNode.Values[1] := Year;
        MonthNode.Values[2] := Month;
      end;
    end;

  finally
    cxTreeList1.FullExpand;
    cxTreeList1.EndUpdate;
  end;
end;

procedure TForm1.GetCheckedValues;
var
  i : Integer;
  Node : TcxTreeListNode;
  S : String;
begin
  for i := 0 to cxTreeList1.Count - 1 do begin
    Node := cxTreeList1.Items[i];
    if Node.Values[0] then begin
      S := Format('Item: %d, col0: %s col1: %s col2: %s', [i, Node.Values[0], Node.Values[1], Node.Values[2]]);
      Memo1.Lines.Add(S);
    end;
  end;
end;

procedure TForm1.btnGetCheckedValuesClick(Sender: TObject);
begin
  GetCheckedValues;
end;