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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TMemo.Strings vs TListBox.Items

Status
Not open for further replies.

flexterful

Programmer
Mar 7, 2011
3
0
0
LT
Hi all.
I'm new to this forum so if you may excuse me for my mistakes.

I have written some lines of code :


// Object to store a sting in TStrings.Objects
TString = class(TObject)
private
fStr: String;
public
constructor Create(const AStr: String) ;
property Str: String read FStr write FStr;
end;
// </TString>

var
Form1: TForm1; // My form, contains Memo, ListBox and 2 Buttons

implementation

//-----------------------------------------------------------
// string object constructor
constructor TString.Create(const AStr: String) ;
begin
inherited Create;
FStr := AStr;
end;
//-----------------------------------------------------------
// method populates TString object passed via parameter with a
// string as a caption and a string as a object (via TString)
procedure PopulateStrings(Strings : TStrings);
const
S = 'asd';
begin
Strings.Clear;
Strings.AddObject(S, TString.Create(S));
Strings.AddObject(S, TString.Create(S));
Strings.AddObject(S, TString.Create(S));
Strings.AddObject(S, TString.Create(S));
end;
//-----------------------------------------------------------
// this one shows data, contained in Strings.Objects
// if none - prints 'nil'
procedure ShowStrings(Strings : TStrings);
var
i : Integer;
S : String;
begin
S := '';
for i := 0 to Strings.Count - 1 do
if Assigned(Strings.Objects)
then S := S + TString(Strings.Objects).Str + #13#10
else S := S + 'nil'#13#10;
ShowMessage(S);
end;
//-----------------------------------------------------------
// perform both mentioned methods for TMemo.Lines
procedure TForm1.Button1Click(Sender: TObject);
begin
PopulateStrings(Memo1.Lines);
ShowStrings(Memo1.Lines);
end;
//-----------------------------------------------------------
// perform both mentioned methods for TListBox.Items
procedure TForm1.Button2Click(Sender: TObject);
begin
PopulateStrings(ListBox1.Items);
ShowStrings(ListBox1.Items);
end;
//-----------------------------------------------------------

Result for TMemo.Lines (Button1.Click) :

---------------------------
Project1
---------------------------
nil
nil
nil
nil

---------------------------
OK
---------------------------


Result for TListBox.Items (Button2.Click) :


---------------------------
Project1
---------------------------
asd
asd
asd
asd

---------------------------
OK
---------------------------


Tested on Delphi5, Delpho7 and DelphiXE.

Why and where does TMemo (who uses same TStrings class for Lines as TListBox for his Items does) vanish all objects? Any ideas?

Thanks in advance
 
What are you trying to accomplish? I'm not really sure from what you wrote.

Anyhow, direct assignment works well as long as both the memo and the listbox are in existence.

Code:
Listbox1.Items := Memo1.Lines;

It seems if that's what you're wanting to do, you're making it harder than it needs to be.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
I'm trying to store an object among a TMemo line, required for my application localization.
And I failed.
Got a result with TListBox, but am still dreaming about TMemo.
It is a bug or a feature?
 
Got a result with TListBox, but am still dreaming about TMemo.
It is a bug or a feature?

Much easier understood. It's a feature. As the documentation states, TStrings does nothing with the Objects property. However, things are set up so descendants can.

If you look at the source for TListBox, it uses TListBoxStrings which is a descendant of TStrings. TListBoxStrings does work on the Objects property. TMemo, however, does not descend the TStrings. Since the TStrings has nothing defined, nothing happens.

Answer: Store it separately (TListBoxStrings.AddObject ultimately winds around to a TCustomListBox method).

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Thanks.
Found it myself now. TListBoxStrings work with objects, while TMemoStrings - doesn't. Don't know what to do - simply use a ListBox or upgrade a Memo...
However i think the problem is solved.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top