814123 2018-07-02 11:57 采纳率: 50%
浏览 1192
已采纳

delphi Treeview的节点移动和保存节点位置?

节点移动我用NowNode.MoveTo(NowNode.getPrevSibling,naInsert);可以移动了。
但是下次打开的时候,位置又复原了。
在移动的同时怎么保存它的位置到数据库里呢?

  • 写回答

4条回答 默认 最新

  • threenewbee 2018-07-03 05:40
    关注
     unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, DB, ADODB, StdCtrls, ComCtrls;
    
    type
      TForm1 = class(TForm)
        TreeView1: TTreeView;
        Button1: TButton;
        Button2: TButton;
        ADOConnection1: TADOConnection;
        ADOCommand1: TADOCommand;
        ADODataSet1: TADODataSet;
        procedure FormCreate(Sender: TObject);
        procedure LoadSub(id: Integer; node: TTreeNode);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
       PMyRc = ^TMyRc;
       TMyRc = Record
         id:Integer;
       end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    
    procedure TForm1.LoadSub(id: Integer; node: TTreeNode);
    var
      node1 : TTreeNode;
      ADOCommand2 : TADOCommand;
      ADODataSet2 : TADODataSet;
      p:PMyRc;
    begin
      ADOCommand2 := TADOCommand.Create(Self);
      ADOCommand2.Connection := ADOConnection1;
      ADOCommand2.CommandText := 'select * from table1 where parentid =' + IntToStr(id) + ' order by [order]';
      ADODataSet2 := TADODataSet.Create(Self);
      ADODataSet2.Recordset := ADOCommand2.Execute();
      while not(ADODataSet2.Eof) do
      begin
        node1 := TreeView1.Items.AddChild(node, ADODataSet2['text']);
        New(p);
        p.id:=StrToInt(ADODataSet2['id']);
        node1.Data := p;
        LoadSub(ADODataSet2['id'], node1);
        ADODataSet2.Next();
      end;
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      node : TTreeNode;
      p:PMyRc;
    begin
      ADOConnection1.Open();
      ADOCommand1.CommandText := 'select * from table1 where parentid = -1 order by [order]';
      ADODataSet1.Recordset := ADOCommand1.Execute();
      while not(ADODataSet1.Eof) do
      begin
        node := TreeView1.Items.AddChild(nil, ADODataSet1['text']);
        New(p);
        p.id:=StrToInt(ADODataSet1['id']);
        node.Data := p;
        LoadSub(ADODataSet1['id'], node);
        ADODataSet1.Next();
      end;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      Curr : TTreeNode;
      Pre : TTreeNode;
    begin
      Curr := TreeView1.Selected;
      Pre := Curr.getPrevSibling();
      if (Pre <> nil) then
        Curr.MoveTo(Pre, naInsert);
      TreeView1.SetFocus();
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    var
      Curr : TTreeNode;
      Pre : TTreeNode;
    begin
      Curr := TreeView1.Selected;
      Pre := Curr.getNextSibling();
      if (Pre <> nil) then
        Curr.MoveTo(Pre, naAdd);
      TreeView1.SetFocus();
    end;
    
    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    var
      i : Integer;
      p:PMyRc;
    begin
      for i := 0 to TreeView1.Items.Count - 1 do
      begin
         p := pmyrc(TreeView1.Items[i].Data);
         ADOCommand1.CommandText := 'update table1 set [order] = ' + IntToStr(i) + ' where id =' + IntToStr(p^.id);
         ADOCommand1.Execute();
      end;
    end;
    
    end.
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 maccms影视模板 制作影视网站失败 求
  • ¥15 stm32按键设置闹钟数进退位不正常
  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常
  • ¥15 github上的这个C语言项目如何跑起来