Delphi 使用TTreeview作为菜单

Delphi 使用TTreeview作为菜单,delphi,Delphi,我使用delphi的ttreeview作为“选项”菜单。在运行时如何选择下一个节点(如“上一个”和“下一个”按钮)?我尝试了getprev和getnext方法,但没有成功。这里有“下一步”行为。对于“上一个”,我将作为练习留给您:- procedure TForm8.btn1Click(Sender: TObject); var crt: TTreeNode; begin with tv1 do //this is our tree begin if Selecte

我使用delphi的ttreeview作为“选项”菜单。在运行时如何选择下一个节点(如“上一个”和“下一个”按钮)?我尝试了getprev和getnext方法,但没有成功。

这里有“下一步”行为。对于“上一个”,我将作为练习留给您:-

procedure TForm8.btn1Click(Sender: TObject);  
var  
  crt: TTreeNode;

begin
  with tv1 do //this is our tree
  begin
    if Selected=nil then
      crt:=Items[0] //the first one
    else
      crt:=Selected.GetNext; //for previous you'll have 'GetPrev' 

    if crt<>nil then //can be 'nil' if we reached to the end
      Selected:=crt;
  end;
end;

HTH

这里是“下一个”行为。对于“上一个”,我将作为练习留给您:-

procedure TForm8.btn1Click(Sender: TObject);  
var  
  crt: TTreeNode;

begin
  with tv1 do //this is our tree
  begin
    if Selected=nil then
      crt:=Items[0] //the first one
    else
      crt:=Selected.GetNext; //for previous you'll have 'GetPrev' 

    if crt<>nil then //can be 'nil' if we reached to the end
      Selected:=crt;
  end;
end;

HTH

可能树项中有一些空间来存储指向正确页面的指针


但是-如果您有时间-尝试探索-这是Delphi最好的treeview组件。

也许树项中有一些空间来存储指向您正确页面的指针


但是-如果您有时间-尝试探索-这是Delphi最好的treeview组件。

这里有另一种方法:

type TfrmMain = class(TForm)
...
   public
      DLLHandle : THandle;
      function GetNodePath(node: TTreeNode; delimiter: string = '\') : String;

...

function TfrmMain.GetNodePath(node: TTreeNode; delimiter: string = '\') : String;
begin
   Result:='';
   while Assigned(node) do
    begin
      Result:=delimiter+node.Text+Result;
      node:=node.Parent;
   end;
   if Result <> '' then
      Delete(Result, 1, 1);
end;

...
如果您有一个“项目1”和一个名为“项目1”的子项目,并单击项目2,则消息应为“项目1\项目2”。通过这样做,您可以更好地控制


希望这能为您提供另一种增强代码的方法

以下是另一种方法:

type TfrmMain = class(TForm)
...
   public
      DLLHandle : THandle;
      function GetNodePath(node: TTreeNode; delimiter: string = '\') : String;

...

function TfrmMain.GetNodePath(node: TTreeNode; delimiter: string = '\') : String;
begin
   Result:='';
   while Assigned(node) do
    begin
      Result:=delimiter+node.Text+Result;
      node:=node.Parent;
   end;
   if Result <> '' then
      Delete(Result, 1, 1);
end;

...
如果您有一个“项目1”和一个名为“项目1”的子项目,并单击项目2,则消息应为“项目1\项目2”。通过这样做,您可以更好地控制

希望这能为您提供另一个增强代码的想法