was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

26 Visitors Online


 
...Records an TStrings (TTreeview / TListview) anhängen?
Autor: Thomas Greiner
Homepage: http://www.northcom.de
[ Tip ausdrucken ]  

Tip Bewertung (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;

 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


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