getting the checked nodes from tcxtreelist

2.3k views Asked by At

I have a tcxtreelist Does anyone know how to get all the checkedNodes?

I need to go through my tcxtreelist get a certain value from the tcxtreelist and write it to a string with comma delimited

Anyone can help me with this?

Thanks Kind Regards

1

There are 1 answers

3
MartynA On

Suppose you have a cxTreeList with 3 columns, colChecked, colYear and colMonth.

If you go to colChecked in the IDE, you can set its Properties property to CheckBox and, at run-time, use it as a checkbox.

How to get at the Checked value in a given tree node is actually quite simple. If you declare a variable Node : TcxTreeList node, you can assign it to any node in the tree, as in

Node := cxTreeList1.Items[i];

Having done that, you can get at the values in the three columns of the node by accessing the Values property of the node, which is a zero-based array of variants which represent the values stored in the node and displayed in the tree. So, you can write

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;

and, of course, you can set the node's Values by assignments in the opposite direction (but don't try that with the db-aware version, TcxDBTreeList, because the displayed values are determined by the contents of the fields of the dataset connected to it).

There's no need to use the Node local variable, I've have only in the interests of clarity. You could just as easily (but not so clearly) write

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

Here's some example code that sets up a cxTreeList with a checkbox column, populates it with rows, and generates a list of the rows which have the checkbox checked:

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;