Delphi VirtualStringTree - 检查重复? [英] Delphi VirtualStringTree - Check for Duplicates?

查看:229
本文介绍了Delphi VirtualStringTree - 检查重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道我发表了很多问题,但是这是因为我需要确保我做的正确,我做错了什么,或者如果我完全没有意义,而且文档中找不到任何东西。无论如何,



我正在尝试检查重复的节点。这是我想要做的:



循环通过我的节点,并比较每个单个节点的文本(记录),但是如果我有很多节点,太时间和记忆力消耗?会有更好的方法吗?



谢谢!
- Jeff。



编辑:感谢Deltics,我得到它的工作!如果我们有一些人有同样的问题,这里是一些工作代码,在VST中使用两个级别的节点!

 过程UncheckDuplicates; 
Var
ParentNode,ChildNode:PVirtualNode;
I,J:整数;
SL:TStringList;
SkypeID:String;
开始

SL:= TStringlist.Create;
try
ParentNode:= frmMain.vtSkype.GetFirst;

为I:= 0 to frmMain.vtSkype.RootNodeCount - 1 do
begin
ChildNode:= ParentNode.FirstChild;
为J:= 0到ParentNode.ChildCount - 1 do
begin
如果NodeIsChecked(ChildNode)然后
begin
SkypeID:= GetData(ChildNode).SkypeID;
如果SL.IndexOf(SkypeID)<> -1然后
begin
ChildNode.CheckState:= csUncheckedNormal;
end
else
begin
SL.Add(SkypeID);
结束
结束
ChildNode:= ChildNode.NextSibling;
结束


ParentNode:= ParentNode.NextSibling;
结束


finally
SL.Free;
结束

frmMain.vtSkype.Refresh;


结束;

我不怕分享我的代码,我欠社区。 :

解决方案

这取决于您正在检查重复的内容。



如果在添加项目的位置,并且您正在同时添加所有项目(或者如果可能/适当的将您重复的检查移动到树视图的填充位置,而不是使用一个已经填充的树),然后维护一个已经添加的项目列表可能是最简单的方法,例如假设您从简单的字符串列表(在此插图代码中的字符串)中添加项目:

 已添加:= TStringList.Create; 
try
alreadyAdded.Sorted:= TRUE; // Sorting确保对IndexOf()...的

的有效的二进制查找对于i:= 0到Pred(strings.count)do
begin
如果alreadyAdded.IndexOf( strings [i])<> -1然后
CONTINUE;

AddNode(strings [i]);
alreadyAdded.Add(strings [i]);
结束
finally
alreadyAdded.Free;
结束


Yeah, I know I post a lot of questions, but thats because I either need assurance that I am doing it right, what I am doing wrong, or if I am totally clueless, and cant find anything in the documentation. Anyways,

I am trying to check for duplicate nodes. Here is how I would want to do it:

Loop thru my nodes, and compare each single node's text (record), but if I got many nodes, wouldnt that be too time and memory consuming? Would there be a better approach for this?

Thanks! - Jeff.

EDIT: Thanks to Deltics, I got it working! In case we have some people with the same question, here is some working code, using 2 levels of nodes in VST!

Procedure UncheckDuplicates;
Var
 ParentNode,ChildNode : PVirtualNode;
 I,J                  : Integer;
 SL                   : TStringList;
 SkypeID              : String;
Begin

   SL := TStringlist.Create;
   try
        ParentNode                      := frmMain.vtSkype.GetFirst;

           for I := 0 to frmMain.vtSkype.RootNodeCount - 1 do
             begin
               ChildNode                := ParentNode.FirstChild;
                 for J := 0 to ParentNode.ChildCount - 1 do
                     begin
                        if NodeIsChecked(ChildNode) then
                          begin
                            SkypeID             := GetData(ChildNode).SkypeID;
                              if SL.IndexOf(SkypeID) <> -1 then
                                begin
                                  ChildNode.CheckState          := csUncheckedNormal;
                                end
                                else 
                                begin
                                  SL.Add(SkypeID);
                                end;
                          end;                          
                     ChildNode                := ChildNode.NextSibling;   
                     end;


               ParentNode               := ParentNode.NextSibling;
             end;


   finally
     SL.Free;
   end;

frmMain.vtSkype.Refresh;


End;

I am not afraid to share my code, I owe it to the community. :)

解决方案

It depends at what point you are checking for duplicates.

If it is at the point at which you are adding items and you are adding all items at the same time, (or if it is possible/appropriate to move you duplicate check to point at which the treeview is populated, rather than working with an already populated tree) then maintaining a list of already added items as you go could be the simplest way, e.g. assuming you are adding items from a simple stringlist (in strings in this illustration code):

  alreadyAdded := TStringList.Create;
  try
    alreadyAdded.Sorted := TRUE;  // Sorting ensures an efficient binary lookup for IndexOf()...

    for i := 0 to Pred(strings.count) do
    begin
      if alreadyAdded.IndexOf(strings[i]) <> -1 then
        CONTINUE;

      AddNode(strings[i]);
      alreadyAdded.Add(strings[i]);
    end;
  finally
    alreadyAdded.Free;
  end;

这篇关于Delphi VirtualStringTree - 检查重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆