Solved

Copy/paste items in TOutline from the same TOutline

Posted on 1997-12-08
10
497 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
ID: 1353172
There is no Moveto in the TOutline.
Are you working with TOutline or TTreeview?
0
 

Author Comment

by:Kapusta
ID: 1353173
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
ID: 1353174
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

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

Author Comment

by:Kapusta
ID: 1353176
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
 

Author Comment

by:Kapusta
ID: 1353177
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
ID: 1353178
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
ID: 1353179
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
ID: 1353180
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
ID: 1353181
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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