whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

55 Visitors Online


 
...copy Listbox Items to the clipboard?
Autor: Thomas Stutz
[ Print tip ]  

Tip Rating (28):  
     


uses
  
Clipbrd;

procedure ListBoxToClipboard(ListBox: TListBox;
  BufferSize: Integer;
  CopyAll: Boolean);
var
  
Buffer: PChar;
  Size: Integer;
  Ptr: PChar;
  I: Integer;
  Line: string[255];
  Count: Integer;
begin
  if not 
Assigned(ListBox) then
    
Exit;

  GetMem(Buffer, BufferSize);
  Ptr   := Buffer;
  Count := 0;
  for I := 0 to ListBox.Items.Count - 1 do
  begin
    
Line := ListBox.Items.strings[I];
    if not CopyAll and ListBox.MultiSelect and (not ListBox.Selected[I]) then
      
Continue;
    { Check buffer overflow }
    
Count := Count + Length(Line) + 3;
    if Count = BufferSize then
      
Break;
    { Append to buffer }
    
Move(Line[1], Ptr^, Length(Line));
    Ptr    := Ptr + Length(Line);
    Ptr[0] := #13;
    Ptr[1] := #10;
    Ptr    := Ptr + 2;
  end;
  Ptr[0] := #0;
  ClipBoard.SetTextBuf(Buffer);
  FreeMem(Buffer, BufferSize);
end;

procedure ClipboardToListBox(ListBox: TListbox);
begin
  if not 
Assigned(ListBox) then
    
Exit;

  if not Clipboard.HasFormat(CF_TEXT) then
    
Exit;

  Listbox.Items.Text := Clipboard.AsText;
end;


//Copy all items from Listbox1 to the clipboard
procedure TForm1.Button1Click(Sender: TObject);
begin
  
ListBoxToClipboard(ListBox1, 1024, True);
end;

//Paste items in clipboard to Listbox2
procedure TForm1.Button2Click(Sender: TObject);
begin
  
ClipboardToListBox(Listbox2);
end;

//Copy only selected items from Listbox1 to the clipboard
procedure TForm1.Button3Click(Sender: TObject);
begin
  
ListBoxToClipboard(Listbox1, 1024, False);
end;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners