Solved

How to cut and paste in Tlistview

Posted on 2000-04-09
7
317 Views
Last Modified: 2010-04-04
my Delphi app has listview and i want to be able to cut and paste with multivalues in it,how to do it?
sample code please.

Rgds.

Shekou
0
Comment
Question by:shekou
  • 4
  • 3
7 Comments
 
LVL 1

Expert Comment

by:Probie
ID: 2697555
What do you wan't to cut and paste?
And how do you want the result to be?

/Probie

0
 

Author Comment

by:shekou
ID: 2697565
I want to cut records in a Listview and
paste it back again.

Rgds

Shekou
0
 
LVL 1

Accepted Solution

by:
Probie earned 100 total points
ID: 2697593
Here is an example...

Basicly I create a second TListView and uses a listitem there as a buffer:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    CopyButton: TButton;
    PasteButton: TButton;
    procedure CopyButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure PasteButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  BufferItem : TListItem;
  BufferItems : TListItems;
  BufferListView : TCustomListView;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
 BufferListView := TCustomListView.CreateParented(handle);
 BufferItems := TListItems.Create(BufferListView);
 BufferItems.BeginUpdate;
 BufferItem := TListItem.Create(BufferItems);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 BufferListView.free;
end;

procedure TForm1.CopyButtonClick(Sender: TObject);
// Copy Item
begin
 if Assigned(ListView1.Selected) then begin
  bufferItem.Caption := 'test';
  BufferItem.Assign(ListView1.Selected);
 end;
end;

procedure TForm1.PasteButtonClick(Sender: TObject);
begin
 ListView1.Items.Add.Assign(BufferItem);
end;

end.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:shekou
ID: 2699220
Thank you probie:

1. you give out copy and paste sample,
   how about CUT and paste?
2. Your sample only work with single
   records, how about multivalues?
   I mean if i want cut 5 records at
   the same time and then paste them   back again, how to do it?


Thanks

Shekou
0
 

Author Comment

by:shekou
ID: 2699706
waiting...
0
 
LVL 1

Expert Comment

by:Probie
ID: 2700139
Ok, here is the code you need...

Rgds
Probie

----


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    CopyButton: TButton;
    CutButton: TButton;
    PasteButton: TButton;
    procedure CopyButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure PasteButtonClick(Sender: TObject);
    procedure CutButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  BufferItem : TListItem;
  BufferItems : TListItems;
  BufferListView : TCustomListView;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
 BufferListView := TCustomListView.CreateParented(handle);
 BufferItems := TListItems.Create(BufferListView);
 BufferItems.BeginUpdate;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 BufferListView.free;
end;

procedure TForm1.CopyButtonClick(Sender: TObject);
// Copy Item
var lp0 : integer;
begin
 BufferItems.Clear;
 for lp0 := 0 to ListView1.Items.Count-1 do begin
  if ListView1.Items[lp0].Selected then begin
   BufferItems.Add.Assign(ListView1.Items[lp0]);
  end;
 end;
end;

procedure TForm1.PasteButtonClick(Sender: TObject);
// Paste
var lp0,idx : integer;
begin
 if Assigned(ListView1.Selected) then begin
  idx := ListView1.Selected.Index;
  for lp0 := 0 to BufferItems.Count-1 do begin
   ListView1.Items.Insert(idx).Assign(BufferItems[lp0]);
  end;
 end else begin
  for lp0 := 0 to BufferItems.Count-1 do begin
   ListView1.Items.Add.Assign(BufferItems[lp0]);
  end;
 end;
end;

procedure TForm1.CutButtonClick(Sender: TObject);
// Cut
var lp0 : integer;
begin
 BufferItems.Clear;
 for lp0 := ListView1.Items.Count-1 downto 0 do begin
  if ListView1.Items[lp0].Selected then begin
   BufferItems.Add.Assign(ListView1.Items[lp0]);
   ListView1.Items[lp0].Delete;
  end;
 end;
end;

end.
0
 

Author Comment

by:shekou
ID: 2703094
Thank you very probie, it works fine.

Rgds

Shekou
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 61
LAN or WAN ? 11 81
Find and Replace Stream with 0s 8 56
Tviruailstringtree sort multi columns on header click 1 52
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

914 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

20 Experts available now in Live!

Get 1:1 Help Now