解决TcxTreeList使用的几个问题

1、节点遍历

Items是按树型结构存放的,单纯遍历Items,只能得到第一层的节点,网上很多例子,都是用递归处理,其实是不必的,TcxTreeList包含了一个AbsoluteItems,保存了顺序结构的节点,可以如下遍历:

  for i := 0 to ATreeList.AbsoluteCount - 1 do
    showmessage(ATreeList.AbsoluteItems[i].Texts[0]);

  

2、为节点增加CheckBox

ATreeList.OptionsView.CheckGroups := True;  //使CheckBox有效

设置了该属性后,节点不会发生任何变化,可以使用遍历设置属性,例如:

  for i := 0 to ATreeList.AbsoluteCount - 1 do

    ATreeList.AbsoluteItems[i].CheckGroupType := ncgCheckGroup;  //多选或单选

这样处理的话,每次加载节点之后都需要执行这项操作,比较麻烦,可以直接修改源代码cxTL.pas,实现自动切换,修改如下:

为TcxCustomTreeList增加属性:

private

  FCheckGroupType:TcxTreeListNodeCheckGroupType;

public

  property CheckGroupType: TcxTreeListNodeCheckGroupType read FCheckGroupType write SetCheckGroupType; default ncgNone;

 

//修改创建方法

constructor TcxCustomTreeList.Create(AOwner: TComponent);
begin
  ...原代码...

  FCheckGroupType := ncgNone;
end;

 

//设置CheckGroupType时,修改节点状态

procedure TcxCustomTreeList.SetCheckGroupType(
  const Value: TcxTreeListNodeCheckGroupType);
var
  i: Integer;
begin
  if FCheckGroupType <> Value then begin
    FCheckGroupType := Value;
    BeginUpdate;
    try
      FRoot.CheckGroupType := Value;
      for i := 0 to AbsoluteCount - 1 do
        AbsoluteItems[i].CheckGroupType := Value;
    finally
      EndUpdate;
    end;
  end;
end;

 

 

修改节点创建事件:

constructor TcxTreeListNode.Create(AOwner: TcxCustomTreeList);
begin
  ...原代码...

 

  //创建时设默认属性,忽略OptionsView.CheckGroups的值,无影响

  if (AOwner <> nil) then 

    CheckGroupType := AOwner.CheckGroupType;
end;

    

3、遍历选中的节点

   for i := 0 to AbsoluteCount - 1 do
     if AbsoluteItems[i].Checked then showmessage(AbsoluteItems[i].Texts[0]);

   

4、几个属性

TopNode:顶节点,实际上是第一个有效的节点

Root:第一层节点数组

Items:节点的子节点数组(如果是TreeList.Items相当于Root)

Texts,Values:值数组,对应Bands的Index

    

  

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值