Solved

Copy all items from 1 listview to another

Posted on 2007-11-27
3
1,295 Views
Last Modified: 2008-02-01
Hi all,

I have 2 ListViews (They are exactly the same!)

I need to copy all items in listview1 to listview2

Anyone know how to do this please?

Thanks

ST3VO
0
Comment
Question by:ST3VO
3 Comments
 
LVL 5

Accepted Solution

by:
Steven-Fernandez earned 500 total points
ID: 20357013
Try this:
 

 ListView1.SelectAll;

 ListView1.CopySelection(ListView2);

Open in new window

0
 

Author Comment

by:ST3VO
ID: 20357042
Perfect...Thanks!!!! :o)
0
 
LVL 1

Expert Comment

by:carcotasu
ID: 20357045
You can save ListView1 an load in to ListView2

This cod it's not created by me.

{  This Code allows you to Load/Save items & subitems of a

  TListView in a file.
 

  The Code has been extracted & adapted from

  TSmartListView 1.01, Freeware

  by Fabio De Pasquale.

}
 

uses

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, comctrls,

  StdCtrls;
 

type

  TForm1 = class(TForm)

    ListView1: TListView;

    Button1: TButton;

    Button2: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

  private

    procedure SaveListViewToFile(AListView: TListView; sFileName: string);

    procedure LoadListViewToFile(AListView: TListView; sFileName: string);

  public

  end;
 

const

  Msg1 = 'File "%s" does not exist!';

  Msg2 = '"%s" is not a ListView file!';
 

var

  Form1: TForm1;
 

implementation
 

{$R *.DFM}
 

procedure TForm1.SaveListViewToFile(AListView: TListView; sFileName: string);

var

  idxItem, idxSub, IdxImage: Integer;

  F: TFileStream;

  pText: PChar;

  sText: string;

  W, ItemCount, SubCount: Word;

  MySignature: array [0..2] of Char;

begin

  //Initialization

  with AListView do

  begin

    ItemCount := 0;

    SubCount  := 0;

    //****

    MySignature := 'LVF';

    //  ListViewFile

    F := TFileStream.Create(sFileName, fmCreate or fmOpenWrite);

    F.Write(MySignature, SizeOf(MySignature));
 

    if Items.Count = 0 then

      // List is empty

      ItemCount := 0

    else

      ItemCount := Items.Count;

    F.Write(ItemCount, SizeOf(ItemCount));
 

    if Items.Count > 0 then

    begin

      for idxItem := 1 to ItemCount do

      begin

        with Items[idxItem - 1] do

        begin

          //Save subitems count

          if SubItems.Count = 0 then

            SubCount := 0

          else

            SubCount := Subitems.Count;

          F.Write(SubCount, SizeOf(SubCount));

          //Save ImageIndex

          IdxImage := ImageIndex;

          F.Write(IdxImage, SizeOf(IdxImage));

          //Save Caption

          sText := Caption;

          w     := Length(sText);

          pText := StrAlloc(Length(sText) + 1);

          StrPLCopy(pText, sText, Length(sText));

          F.Write(w, SizeOf(w));

          F.Write(pText^, w);

          StrDispose(pText);

          if SubCount > 0 then

          begin

            for idxSub := 0 to SubItems.Count - 1 do

            begin

              //Save Item's subitems

              sText := SubItems[idxSub];

              w     := Length(sText);

              pText := StrAlloc(Length(sText) + 1);

              StrPLCopy(pText, sText, Length(sText));

              F.Write(w, SizeOf(w));

              F.Write(pText^, w);

              StrDispose(pText);

            end;

          end;

        end;

      end;

    end;

    F.Free;

  end;

end;
 
 
 

procedure TForm1.LoadListViewToFile(AListView: TListView; sFileName: string);

var

  F: TFileStream;

  IdxItem, IdxSubItem, IdxImage: Integer;

  W, ItemCount, SubCount: Word;

  pText: PChar;

  PTemp: PChar;

  MySignature: array [0..2] of Char;

  sExeName: string;

begin

  with AListView do

  begin

    ItemCount := 0;

    SubCount  := 0;
 

    sExeName := ExtractFileName(sFileName);
 

    if not FileExists(sFileName) then

    begin

      MessageBox(Handle, PChar(Format(Msg1, [sExeName])), 'I/O Error', MB_ICONERROR);

      Exit;

    end;
 

    F := TFileStream.Create(sFileName, fmOpenRead);

    F.Read(MySignature, SizeOf(MySignature));
 

    if MySignature <> 'LVF' then

    begin

      MessageBox(Handle, PChar(Format(Msg2, [sExeName])), 'I/O Error', MB_ICONERROR);

      Exit;

    end;
 

    F.Read(ItemCount, SizeOf(ItemCount));

    Items.Clear;
 

    for idxItem := 1 to ItemCount do

    begin

      with Items.Add do

      begin

        //Read imageindex

        F.Read(SubCount, SizeOf(SubCount));

        //Read imageindex

        F.Read(IdxImage, SizeOf(IdxImage));

        ImageIndex := IdxImage;

        //Read the Caption

        F.Read(w, SizeOf(w));

        pText := StrAlloc(w + 1);

        pTemp := StrAlloc(w + 1);

        F.Read(pTemp^, W);

        StrLCopy(pText, pTemp, W);

        Caption := StrPas(pText);

        StrDispose(pTemp);

        StrDispose(pText);

        if SubCount > 0 then

        begin

          for idxSubItem := 1 to SubCount do

          begin

            F.Read(w, SizeOf(w));

            pText := StrAlloc(w + 1);

            pTemp := StrAlloc(w + 1);

            F.Read(pTemp^, W);

            StrLCopy(pText, pTemp, W);

            Items[idxItem - 1].SubItems.Add(StrPas(pText));

            StrDispose(pTemp);

            StrDispose(pText);

          end;

        end;

      end;

    end;
 

    F.Free;

  end;

end;
 

// Example:
 

procedure TForm1.Button1Click(Sender: TObject);

begin

  // Save Items and Clear the ListView

  SaveListViewToFile(ListView1, 'MyListView.sav');

  ListView1.Items.Clear;

end;
 

procedure TForm1.Button2Click(Sender: TObject);

begin

  // Load Items

  LoadListViewToFile(ListView1, 'MyListView.sav');

end;

Open in new window

0

Featured Post

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!

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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

19 Experts available now in Live!

Get 1:1 Help Now