TTreeview选择项目和级别?

时间:2011-01-03 20:23:03

标签: delphi components delphi-7

我有一个TTreeView

我将两个级别的项目添加到

Test 1 Parent  
  Test 1 Child  
Test 2 Parent  
  Test 2 Child A  
  Test 2 Child B  
  Test 2 Child C  

我正在尝试根据选择的级别启用和禁用“添加”,“编辑”和“删除”按钮

示例:

procedure TfrmMain.UpdateBtns;  
begin  
btnEditParent.Enabled:= (treeview.Focused ) AND (treeview.Selected <> nil);     btnDeleteParent.Enabled:= (treeview.Focused ) AND (treeview.Selected <> nil);  
btnAddChild.Enabled:= (treeview.Focused ) AND (treeview.Selected <> nil);  
btnEditChild.Enabled:= (treeview.Focused ) AND (treeview.Selected <> nil);  
btnDeleteChild.Enabled:= (treeview.Focused ) AND (treeview.Selected <> nil);  
end;  

我如何考虑这些水平?

1 个答案:

答案 0 :(得分:4)

TTreeNode对象具有Level属性

var
 Node : TTreeNode;
begin
 Node:=TreeView1.Selected;
 //Node.Level //The first Level is 0

end;

你可以用这种方式重写你的代码

    var
     Node : TTreeNode;
    begin
     Node:=TreeView.Selected;
     if Node<>nil then
     begin
      btnEditParent.Enabled  := (Node.Level=0);
      btnDeleteParent.Enabled:= (Node.Level=0);
      btnAddChild.Enabled    := (Node.Level=0);
      btnEditChild.Enabled   := (Node.Level>0);
      btnDeleteChild.Enabled := (Node.Level>0);
     end;
    end;