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

34 Visitors Online


 
...add records to TStrings (TTreeview / TListview)?
Autor: Thomas Greiner
Homepage: http://www.northcom.de
[ Print tip ]  

Tip Rating (10):  
     


{
  Bei der Klasse TStrings und den Komponenten TTreeview/TListview gibt es die
  Möglichkeit, neben dem einfachen einfügen eines Strings auch Objekte mitanzufügen.
  Da eine Klasse TObject erwartet wird, muss man einen kleinen Umweg programmieren.
}

{
  The Classes TStrings and the components TTreeview/TListview allow you to add an
  additional Object to a string.
  Since a TObject is expected, you need to make a little detour to achive this.
}


type
  
TMyRecord = record
    
id: Integer;
    Name: string;
    {...}
  
end;
  PMyRecord = ^TMyRecord;

  {...}


  { In this example I use a Listview component }

procedure Form1.Form1Create(Sender: TObject)
  var
  
i: Integer;
  pRec: PMyRecord;
begin
  for 
i := 0 to 10 do
  begin
    
new(pRec);
    pRec.id := i;
    pRec.Name := 'Entry' + IntToStr(i);
    {...}
    
ListView1.AddItem('Entry' + IntToStr(i), Pointer(pRec));
  end;
end;

{ To retrieve the stored records just use this: }

procedure Form1.ListView1Click(Sender: TObject);
var
  
i: Integer;
  xRec: TMyRec;
begin
  for 
i := 0 to Listview1.Count - 1 do
    if 
ListView1.Selected[i] then
    begin
      
xRec := PMyRecord(ListView1.Items.Objects[i])^;
      ShowMessage(Format('Record #%d Name: %s', [xRec.id, xRec.Name]));
    end;
end;

{ finally do not forget to free assigned memory }

procedure Form1.FormClose(Sender: TObject);
var
  
i: Integer;
begin
  for 
i := 0 to ListView1.Count - 1 do
    if 
ListView1.Items.Objects[i] <> nil then
      
Dispose(ListView1.Items.Objects[i]);
end;

 

Rate this tip:

poor
very good


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