【问题标题】:getting the checked nodes from tcxtreelist从 tcxtreelist 获取检查的节点
【发布时间】:2016-12-17 10:49:39
【问题描述】:

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

我需要查看我的 tcxtreelist 从 tcxtreelist 中获取某个值 并将其写入以逗号分隔的字符串

有人可以帮我吗?

谢谢 亲切的问候

【问题讨论】:

    标签: delphi devexpress tadoquery


    【解决方案1】:

    假设您有一个 cxTreeList 3 列,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;
    

    【讨论】:

    • 我有一个 3 列的 tcxTreeList 第一列启用了检查组的属性,它将包含带有文本的复选框,第二列包含第一个根节点的键,第三列包含所有子节点的键在根节点下。如何获取根节点的键是否被检查以及所有检查的子节点的键为2个变量。
    • 你应该发一个新的q来问这个问题。
    猜你喜欢
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多