Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

listbox.items property tstringlist?

Status
Not open for further replies.

Leonardl

Programmer
Aug 25, 2002
9
0
0
US
is there a way to change a listbox's items property from a tstrings to a tstringlist?
 
Since StringList is derived from TStrings you will be able to do the following:

//declare a string list in your class
SL : TStringList;

procedure myForm.Create(Sender : TObject);
begin
SL := TStringList.Create;
SL.Add('String1');
SL.Add('String2');
SL.Add('String3');

//now sub your listbox with SL
AListBox.Items := SL;
end;

//dont forget to free SL somewhere later in the code like onDestroy();
destructor destroy()
begin
SL.Free;
end;

 
Doing what Ulfeng proposes won't work. TListBox's Items property has this line in its Set method:

Items.Assign(Value);

so assigning SL to AListBox.Items only copies the string inside SL to the string holder of AList. The only way to do what you ask would be inheriting from TListBox and overriding the Items property with one of your own. Something like this:

TSLListBox = class(TListBox)
private
FSLItems: TStringList;
procedure SLChange(aSender: TObject);
procedure SetSLItems(const Value: TStringList);
published
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
property Items: TStringList read FSLItems write SetSLItems;
end;

implementation

{ TSLListBox }

constructor TSLListBox.Create(aOwner: TComponent);
begin
inherited;
FSLItems:= TStringList.Create;
FSLItems.OnChange:= SLChange;
end;

destructor TSLListBox.Destroy;
begin
FSLItems.Free;
inherited;
end;

procedure TSLListBox.SetSLItems(const Value: TStringList);
begin
FSLItems.Assign(Value);
end;

procedure TSLListBox.SLChange(aSender: TObject);
begin
// This line updates the original string repository with the changes made to SL
TListBox(Self).Items:= FSLItems;
end;


This approach has several disadvantages. First this is not a visual component nor a example of clean and nice inheritance. The second and more insidious problem is that properties are statically linked, meaning that if you pass a reference of your new ListBox to a procedure that expects a TListBox, the code inside the procedure will access the Items of TListBox and not those you declared. Any items added or deleted won't be reflected on FSLItems and your object will be inconsistent.

I'm not sure why you want to do this kind of modification, as almost everything available in a TStringList is also available in the TListBox. Maybe if you clarify a little more the pourpose of the mod, we can think of an alternative way of doing it.
 
Why does Leonardl want to change a listbox's items property from a tstrings to a tstringlist ?
 
Good question towerbase! Cheers,
Nico
 
Well because the stringlist has a duplicates property,and tsrtings does not. but I did get around this by using the first example above.
when i add to the listbox i first add to a globall stringlist. then i clear the list box and copy the stringlist to the listbox. works fine for what i need.
later when i learn more i will attempt to create a custom listbox with a duplicates property.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top