我用MoveTo方法实现节点的上移和下移
这个是上移
procedure Tfrm_main.btn_treeNodeUpClick(Sender: TObject);
var PreNode,select : TTreeNode;
tmp:Integer;
begin
if (self.tvw_List.Selected<>nil)then //检查是否有选中节点 如果有则做上移操作
begin
if (tvw_List.Selected.getPrevSibling <> nil) then //检查是否有同级上一节点如果有做上移操作
begin
select:=tvw_List.Selected; //交换节点序号 修改节点名称
PreNode := select.getPrevSibling;
tmp := PNodeInfo(PreNode.Data)^.FId;
select.MoveTo(PreNode,naInsert);
PNodeInfo(PreNode.Data)^.FId := PNodeInfo(select.Data)^.FId;
PNodeInfo(select.Data)^.FId:=tmp;
PreNode.Text:= IntToStr(PNodeInfo(PreNode.Parent.Data).FId)+'.'+IntToStr(PNodeInfo(PreNode.Data).FId)+' '+PNodeInfo(PreNode.Data).FName;
select.Text:=IntToStr(PNodeInfo(select.Parent.Data).FId)+'.'+IntToStr(PNodeInfo(select.Data).FId)+' '+PNodeInfo(select.Data).FName;
//刷新树
self.tvw_List.Refresh;
end;
end
else
begin
application.MessageBox('请先选择一个节点','提示',0);
end;
end;下移和上移差不多
procedure Tfrm_main.btn_treeNodeDownClick(Sender: TObject);
var NexNode,select : TTreeNode;
tmp:Integer;
begin
if (self.tvw_List.Selected<>nil)then //检查是否有选中节点 如果有则做下移操作
begin
if (tvw_List.Selected.getNextSibling <> nil) then //检查是否有同级上一节点如果有做上移操作
begin
select:=tvw_List.Selected; //交换节点序号 修改节点名称 NexNode := select.getNextSibling;
tmp := PNodeInfo(NexNode.Data)^.FId; if (tvw_List.Selected.getNextSibling.getNextSibling <> nil) then
select.MoveTo(NexNode.getNextSibling,naInsert)
else
select.MoveTo(NexNode,naAdd); PNodeInfo(NexNode.Data)^.FId := PNodeInfo(select.Data)^.FId;
PNodeInfo(select.Data)^.FId:=tmp;
NexNode.Text:= IntToStr(PNodeInfo(NexNode.Parent.Data).FId)+'.'+IntToStr(PNodeInfo(NexNode.Data).FId)+' '+PNodeInfo(NexNode.Data).FName;
select.Text:=IntToStr(PNodeInfo(select.Parent.Data).FId)+'.'+IntToStr(PNodeInfo(select.Data).FId)+' '+PNodeInfo(select.Data).FName;
//刷新树
self.tvw_List.Refresh;
end;
end
else
begin
application.MessageBox('请先选择一个节点','提示',0);
end;
end;现在的问题是 我的树有两层 我的第一级 第一个节点下的子节点上移下移没问题 从第二级开始下边的所有子节点一移动就会出问题 全都乱了 显示出来的名称都变成了每个节点的最后一个子节点 我跟踪了一下 发现在做完上边的操作后结果是对的 但是不知道为什么它还会继续执行 也不知道执行了甚么代码 最后出来的结果就乱了 不知道谁遇到过这样的问题 帮帮忙把!
这个是上移
procedure Tfrm_main.btn_treeNodeUpClick(Sender: TObject);
var PreNode,select : TTreeNode;
tmp:Integer;
begin
if (self.tvw_List.Selected<>nil)then //检查是否有选中节点 如果有则做上移操作
begin
if (tvw_List.Selected.getPrevSibling <> nil) then //检查是否有同级上一节点如果有做上移操作
begin
select:=tvw_List.Selected; //交换节点序号 修改节点名称
PreNode := select.getPrevSibling;
tmp := PNodeInfo(PreNode.Data)^.FId;
select.MoveTo(PreNode,naInsert);
PNodeInfo(PreNode.Data)^.FId := PNodeInfo(select.Data)^.FId;
PNodeInfo(select.Data)^.FId:=tmp;
PreNode.Text:= IntToStr(PNodeInfo(PreNode.Parent.Data).FId)+'.'+IntToStr(PNodeInfo(PreNode.Data).FId)+' '+PNodeInfo(PreNode.Data).FName;
select.Text:=IntToStr(PNodeInfo(select.Parent.Data).FId)+'.'+IntToStr(PNodeInfo(select.Data).FId)+' '+PNodeInfo(select.Data).FName;
//刷新树
self.tvw_List.Refresh;
end;
end
else
begin
application.MessageBox('请先选择一个节点','提示',0);
end;
end;下移和上移差不多
procedure Tfrm_main.btn_treeNodeDownClick(Sender: TObject);
var NexNode,select : TTreeNode;
tmp:Integer;
begin
if (self.tvw_List.Selected<>nil)then //检查是否有选中节点 如果有则做下移操作
begin
if (tvw_List.Selected.getNextSibling <> nil) then //检查是否有同级上一节点如果有做上移操作
begin
select:=tvw_List.Selected; //交换节点序号 修改节点名称 NexNode := select.getNextSibling;
tmp := PNodeInfo(NexNode.Data)^.FId; if (tvw_List.Selected.getNextSibling.getNextSibling <> nil) then
select.MoveTo(NexNode.getNextSibling,naInsert)
else
select.MoveTo(NexNode,naAdd); PNodeInfo(NexNode.Data)^.FId := PNodeInfo(select.Data)^.FId;
PNodeInfo(select.Data)^.FId:=tmp;
NexNode.Text:= IntToStr(PNodeInfo(NexNode.Parent.Data).FId)+'.'+IntToStr(PNodeInfo(NexNode.Data).FId)+' '+PNodeInfo(NexNode.Data).FName;
select.Text:=IntToStr(PNodeInfo(select.Parent.Data).FId)+'.'+IntToStr(PNodeInfo(select.Data).FId)+' '+PNodeInfo(select.Data).FName;
//刷新树
self.tvw_List.Refresh;
end;
end
else
begin
application.MessageBox('请先选择一个节点','提示',0);
end;
end;现在的问题是 我的树有两层 我的第一级 第一个节点下的子节点上移下移没问题 从第二级开始下边的所有子节点一移动就会出问题 全都乱了 显示出来的名称都变成了每个节点的最后一个子节点 我跟踪了一下 发现在做完上边的操作后结果是对的 但是不知道为什么它还会继续执行 也不知道执行了甚么代码 最后出来的结果就乱了 不知道谁遇到过这样的问题 帮帮忙把!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货