Main Menu
 Forums

D-TNT Forum »
Off-Topic Forum »
Open-TNT Forum »

 Software
 D-TNT » 

 Be a Member
  Coming Soon

 Comms
 Contact Jason »
Send Feedback »
My Web Portfolio
 

pro·duc·er (pr-dsr, -dy)

n.
One that produces, especially a person or an organization that produces for public presentation of a play, film, program, or similar work.


Why not link to d-tnt.co.uk?
Put this button on your site.

 
Additional Data Within A Stringlist...
Overview:

   Additional Data within a StringList


The Stuff:
{How to easily implement a 2-dimensional stringlist !
...useing the object property !
Product:
Delphi all versions Category:
VCL-General Skill Level:
 Scoring:
 Last Update:
03/30/2000
Search Keys:
delphi delphi3000 article Objects TObject typecast objects-property Times Scored:
5 Visits:
2609
Uploader: Bernhard Angerer
Company: bluestep.com IT Consulting Reference: N/A

Question/Problem/Abstract:

How to append additional data to a string in a stringlist, listbox, memo or combobox !
Answer:



To add strings in a stringlist is very easy:
}
var hSL: TStringList;

begin
  hSL:= TStringList.Create;
  hSL.Add('any string...');

  hSL.Destroy;
end;

{
Howevery - sometimes you need to add additional information
to every string. This can be done by the objects-property:

}
var hIndex: integer; // any number that specifies the line

begin
  hSL.Add('my string');
  hSL.Sort;
  hIndex:= hSL.IndexOf('my string');

  hSL.Objects[hIndex] := TStringList.Create;
  TStringList(hSL.Objects[hIndex]).Add('any substring...');
end;

{
The definition of the object property looks like this:
}
property Objects[Index: Integer]: TObject;
{
It takes an index where any object (every delphi class derives
from TObject) can be assigned respectively appended.
To get the right index we call the function 'IndexOf'. IndexOf
only works when the stringlist is sorted !

Having the index whe can append a new stringlist:
}
  hSL.Objects[hIndex] := TStringList.Create;
{
With a typecast this 'sub-stringlist' can be filled:
}
  TStringList(hSL.Objects[hIndex]).Add('any substring...');



{
If you need to parse the sub-stringlist, just do this:
}
  for i:= 0 to hSL.Count - 1 do
    for ii:= 0 to TStringList(hSL.Objects[i]).Count - 1 do
      Memo1.Lines.Add(TStringList(hSL.Objects[i]).Strings[ii]);


{
When the application terminates every substringlist has to be
destroyed explicitely:
}
  for i:= 0 to hSL.Count - 1 do
    hSL.Objects[i].Destroy;
{
Now you can destroy the main stringlist itself:
 }
  hSL.Destroy;


{

Of course you can also define your own 'data-container' that
can be appended to every line in the stringlist. Just define
a class:
}

type
  TSLData = class
    public
      X1: integer;
      x2: string;
      // etc.
  end;

  ...

  hSL.Objects[hIndex] := TSLData.Create;
  TSLData(hSL.Objects[hIndex]).X1 := 123;
  TSLData(hSL.Objects[hIndex]).X2 := '123';



{
The great thing is that this works in the same way for every VCL
class that uses TStings like:

TMemo
TListbox
TCombobox
etc.

or other classes that provide a Data property, like:

TTreeView
or
TListView

}
<%sig=
/sig%>
<%uid=4CDF-52BD/uid%>
<%snt=01/05/2002/snt%>


 
 Discuss in forum