shekou
asked on
How to cut and paste in Tlistview
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
sample code please.
Rgds.
Shekou
ASKER
I want to cut records in a Listview and
paste it back again.
Rgds
Shekou
paste it back again.
Rgds
Shekou
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
waiting...
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.CreatePare nted(handl e);
BufferItems := TListItems.Create(BufferLi stView);
BufferItems.BeginUpdate;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
BufferListView.free;
end;
procedure TForm1.CopyButtonClick(Sen der: TObject);
// Copy Item
var lp0 : integer;
begin
BufferItems.Clear;
for lp0 := 0 to ListView1.Items.Count-1 do begin
if ListView1.Items[lp0].Selec ted then begin
BufferItems.Add.Assign(Lis tView1.Ite ms[lp0]);
end;
end;
end;
procedure TForm1.PasteButtonClick(Se nder: TObject);
// Paste
var lp0,idx : integer;
begin
if Assigned(ListView1.Selecte d) then begin
idx := ListView1.Selected.Index;
for lp0 := 0 to BufferItems.Count-1 do begin
ListView1.Items.Insert(idx ).Assign(B ufferItems [lp0]);
end;
end else begin
for lp0 := 0 to BufferItems.Count-1 do begin
ListView1.Items.Add.Assign (BufferIte ms[lp0]);
end;
end;
end;
procedure TForm1.CutButtonClick(Send er: TObject);
// Cut
var lp0 : integer;
begin
BufferItems.Clear;
for lp0 := ListView1.Items.Count-1 downto 0 do begin
if ListView1.Items[lp0].Selec ted then begin
BufferItems.Add.Assign(Lis tView1.Ite ms[lp0]);
ListView1.Items[lp0].Delet e;
end;
end;
end;
end.
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.CreatePare
BufferItems := TListItems.Create(BufferLi
BufferItems.BeginUpdate;
end;
procedure TForm1.FormDestroy(Sender:
begin
BufferListView.free;
end;
procedure TForm1.CopyButtonClick(Sen
// Copy Item
var lp0 : integer;
begin
BufferItems.Clear;
for lp0 := 0 to ListView1.Items.Count-1 do begin
if ListView1.Items[lp0].Selec
BufferItems.Add.Assign(Lis
end;
end;
end;
procedure TForm1.PasteButtonClick(Se
// Paste
var lp0,idx : integer;
begin
if Assigned(ListView1.Selecte
idx := ListView1.Selected.Index;
for lp0 := 0 to BufferItems.Count-1 do begin
ListView1.Items.Insert(idx
end;
end else begin
for lp0 := 0 to BufferItems.Count-1 do begin
ListView1.Items.Add.Assign
end;
end;
end;
procedure TForm1.CutButtonClick(Send
// Cut
var lp0 : integer;
begin
BufferItems.Clear;
for lp0 := ListView1.Items.Count-1 downto 0 do begin
if ListView1.Items[lp0].Selec
BufferItems.Add.Assign(Lis
ListView1.Items[lp0].Delet
end;
end;
end;
end.
ASKER
Thank you very probie, it works fine.
Rgds
Shekou
Rgds
Shekou
And how do you want the result to be?
/Probie