gpt4 book ai didi

delphi - 从 tcxtreelist 获取选中的节点

转载 作者:行者123 更新时间:2023-12-01 21:49:33 26 4
gpt4 key购买 nike

我有一个 tcxtreelist有谁知道如何获取所有已检查的节点?

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

有人可以帮我解决这个问题吗?

谢谢亲切的问候

最佳答案

假设您有一个包含 3 列的 cxTreeList,即 colCheckedcolYearcolMonth

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

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

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方向(但不要尝试使用数据库感知版本 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;

关于delphi - 从 tcxtreelist 获取选中的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41197785/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com