Solved

Copy/paste items in TOutline from the same TOutline

Posted on 1997-12-08
10
483 Views
Last Modified: 2012-05-04
Delphi 1.0 question...

Q: Does a TOutline.CopyTo method exist, similar to TOutline.MoveTo() except that instead of moving items within the outline this method would make a mirror image of all items at the "from" level and insert it into the "destination" index point?

Since I couldn't find anything in the online help file, I would have to assume the answer is 'no'.

Next question...how can one accomplish this task?  (Hint, hint...example please).

Example (before):

  111
     aaaa
  222
     bbbb
        %%%%
     cccc
  333

Task: copy 222 and all its children and place/insert them below "aaaa" so that the result looks like:


  111
     aaaa
       222
          bbbb
             %%%%
          cccc
  222
     bbbb
        %%%%
     cccc
  333

0
Comment
Question by:Kapusta
  • 6
  • 3
10 Comments
 
LVL 5

Expert Comment

by:ronit051397
Comment Utility
There is no Moveto in the TOutline.
Are you working with TOutline or TTreeview?
0
 

Author Comment

by:Kapusta
Comment Utility
Ronit asked: "Are you working with TOutline or TTreeview?"


Kapusta replied: My message stated clearly "TOutline" and the title was "Title: "Copy/paste items in TOutline from the same TOutline""

0
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 250 total points
Comment Utility
Hi

The following routine will copy outline nodes for you.  Be careful not to copy part of the tree on to one of its own branches!  I wrote this in Delphi 3, but I think it should work in Delphi 1 from looking at the Delphi 1 help file.

procedure CopyNodes(OutLine: TOutLine; FromIx, ToIx: Integer);

  procedure CopyChildNodes(var FromIx, ToIx: Integer);
  var
    ChildIx, NewNodeIx: Integer;
    Node, ChildNode: TOutlineNode;
  begin
    Node := Outline.Items[FromIx];              
    ChildIx := Node.GetFirstChild;
    while (ChildIx >= 0) do begin
      ChildNode := Outline.Items[ChildIx];
      NewNodeIx := Outline.AddChildObject(ToIx, ChildNode.Text, ChildNode.Data);
      if (FromIx > ToIx) then begin
        Inc(ChildIx);
        Inc(FromIx);
      end;
      if (Outline.Items[ChildIx].GetFirstChild >= 0) then
        CopyChildNodes(ChildIx, NewNodeIx);
      ChildIx := Node.GetNextChild(ChildIx);
    end;
  end;

var
  NewToIx: Integer;
begin
  NewToIx := Outline.AddChild(ToIx, Outline.Items[FromIx].Text);
  if (FromIx > ToIx) then
    Inc(FromIx);
  CopyChildNodes(FromIx, NewToIx);
end;

JB

P.S.  Sorry in advance about any loss of code indentation...
0
 

Author Comment

by:Kapusta
Comment Utility
Thanks for such a great example.  The TOutline "copy/clone" code works great!
0
 

Author Comment

by:Kapusta
Comment Utility
JimBob,

I spoke a bit too soon.  There is something wrong with the code sample that you posted.  I am finding in some instances that the newly copied items are missing several items from their original position.

For example, if you were to copy "Veterinarians" below to "Pet Grooming services", you would find that the newly copied items are lacking the 5 last items in the original group.

Try it with the example outline items below and you will see what I am talking about.


Pet grooming services
Veterinarians
      Stafford County
            Ferry Farm Animal Clinic, Ltd.
                  Dr. Larry Witter
                  371-5090
            White Oak Animal Hospital
                  Dr. Stacy L. Horner
                  374-0462
      Spotsylvania County
      City of Fredericksburg
            Princess Anne Animal Hospital
                  373-8181
                  Dr. Lee A. Gibbons


0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Kapusta
Comment Utility
The indentation was lost in my last post, so let's try the example outline over again...

Pet grooming services
Veterinarians
        Stafford County
                Ferry Farm Animal Clinic, Ltd.
                        Dr. Larry Witter
                        371-5090
                White Oak Animal Hospital
                        Dr. Stacy L. Horner
                        374-0462
        Spotsylvania County
        City of Fredericksburg
                Princess Anne Animal Hospital
                        373-8181
                        Dr. Lee A. Gibbons
0
 

Author Comment

by:Kapusta
Comment Utility
The indentation is still not correct.  How about contacting me at: 74170.3550@compuserve.com so that I can give you the file, or else try downloading:

http://www.pcshareware.com/outline.txt
0
 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
Hi

I've just got back from holiday, so sorry about the delay.  I will download the file you refer to on Monday and get back to you.

JB
0
 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
Hi

I'm very sorry about the delay!!!  The problem was occuring when child items had > 1 sibling under certain circumstances.  The following should fix the problem.

procedure CopyNodes(OutLine: TOutLine; FromIx, ToIx: Integer);

  procedure CopyChildNodes(var FromIx, OldFromIx, ToIx: Integer);
  var
    ChildIx, NewNodeIx: Integer;
    Node, ChildNode: TOutlineNode;
  begin
    Node := Outline.Items[FromIx];              
    ChildIx := Node.GetFirstChild;
    while (ChildIx >= 0) do begin
      ChildNode := Outline.Items[ChildIx];
      NewNodeIx := Outline.AddChildObject(ToIx, ChildNode.Text, ChildNode.Data);
      if (FromIx > ToIx) then begin
        Inc(ChildIx);
        Inc(OldFromIx);
        Inc(FromIx);
      end;
      if (Outline.Items[ChildIx].GetFirstChild >= 0) then
        CopyChildNodes(ChildIx, FromIx, NewNodeIx);
      ChildIx := Node.GetNextChild(ChildIx);
    end;
  end;

var
  NewToIx, NewFromIx: Integer;
begin
  NewToIx := Outline.AddChild(ToIx, Outline.Items[FromIx].Text);
  if (FromIx > ToIx) then
    Inc(FromIx);
  NewFromIx := FromIx;
  CopyChildNodes(FromIx, NewFromIx, NewToIx);
end;


Regards,
JB
0
 

Author Comment

by:Kapusta
Comment Utility
Yes, your update to CopyNodes does indeed appear to have fixed the problem.  I appreciate the time you have taken to assist me.  Thanks.  :-)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now