Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tlistbox.items.addobject

1,837 views
Skip to first unread message

jordi bardají

unread,
Jul 13, 1999, 3:00:00 AM7/13/99
to
Hi! I want to add strings in Tlistbox items objects. So,
How can I retrieve the contains of Strings?
Why this code doesn't work?

Thanks in advenced,

Jordi


procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
for i:=0 to 3 do
begin
Listbox1.items.addObjects(inttostr(i),inttostr(i));

end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
showmessage(string(listbox1.Items.objects[0]));
end;

end.

Stotter Werner

unread,
Jul 13, 1999, 3:00:00 AM7/13/99
to
hi,

if you only want to add strings than use this one:
listbox1.items.add(inttostr(i));

now you can use the following to retrieve the strings:
showmessage(listbox1.items[i]);

but if you want to add an object you have to add an descendant of
tobject like this
listbox1.items.addobject(form1.caption, form1);


hth
werner

"jordi bardají" schrieb:

Mike Price

unread,
Jul 13, 1999, 3:00:00 AM7/13/99
to
Jordi

if you just want to add a string - use

Listbox1.items.add(inttostr(i))
and retrieve it with
Listbox1.items[i]
Good luck!

Mike

jordi bardají

unread,
Jul 13, 1999, 3:00:00 AM7/13/99
to

Thanks. But I need to put in tlistbox items object a string and retrieve it
later.

Jordi

Stotter Werner <wer...@input.at> escribió en artículo
<378B5AD7...@input.at>...


> hi,
>
> if you only want to add strings than use this one:
> listbox1.items.add(inttostr(i));
>
> now you can use the following to retrieve the strings:
> showmessage(listbox1.items[i]);
>
> but if you want to add an object you have to add an descendant of
> tobject like this
> listbox1.items.addobject(form1.caption, form1);
>
>
> hth
> werner
>
> "jordi bardají" schrieb:
>

jordi bardají

unread,
Jul 13, 1999, 3:00:00 AM7/13/99
to
Thanks, but I need to put a string in tlistbox items. object and retrieve
it later as a string.

Jordi.

Mike Price <Mi...@Price.org> escribió en artículo
<378B5C8E...@Price.org>...


> Jordi
>
> if you just want to add a string - use
>
> Listbox1.items.add(inttostr(i))
>
> and retrieve it with
>
> Listbox1.items[i]
>
> Good luck!
>
> Mike
>
> "jordi bardají" wrote:
>

Ralph Friedman (TeamB)

unread,
Jul 13, 1999, 3:00:00 AM7/13/99
to
In article <01becd42$f9de8cc0$0100...@proxy-ccoo.ocea.es>, Jordi
bardají stated:

> Hi! I want to add strings in Tlistbox items objects. So,
> How can I retrieve the contains of Strings?
> Why this code doesn't work?
>
Jordi,

try:

var
footoo: string = 'footoo';

procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption := EmptyStr;
ListBox1.Items.AddObject('foo', @footoo);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Label1.Caption := PString(ListBox1.Items.Objects[0])^;
end;

--
Regards
Ralph (TeamB)
--


Peter Below (TeamB)

unread,
Jul 13, 1999, 3:00:00 AM7/13/99
to
> Thanks, but I need to put a string in tlistbox items. object and retrieve
> it later as a string.
>
Jordi,

this does not really work. Delphi Strings (AnsiStrings) are
reference-counted and playing tricks like typecasting a string variable to
Pointer and storing it into the Objects list of a TStrings derivative
collides with the reference based lifetime management. The results are not
pretty <g>.

What works is to allocate memory for a Pchar, copy the string into it and
store the PChar, e.g.

procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;

s: String;


begin
for i:=0 to 3 do
begin

s:= InttoStr(i);
Listbox1.items.addObjects(S, StrNew( Pchar(S)));
end;
end;

However, you have to be very careful here not to end up with a memory leak.
Before you can delete any item from the listbox you have to explicitely
free the memory for the PChar stored into it Objects property:

If Assigned( Objects[i] ) Then
StrDispose( Pchar( Objects[i] ));

Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!


jordi bardají

unread,
Jul 14, 1999, 3:00:00 AM7/14/99
to
Thanks for your answer. This that you wrote not function commpletely but it
help me to find the solution. You are rigth that I is necessary to free
memory before to destroy the listbox or any of his items.

I think that this is the correct code:
It is mandatory to do pointer(strnew(pchar(p))).

procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;

s:string;
P:pchar;
begin
s:= 'a';
for i:= 0 to 3 do
begin
s:= s+s;
p:=Pchar(s);
Listbox1.items.addObject(inttostr(i), pointer(strnew(pchar(p))));

end;
end;

procedure TForm1.Button2Click(Sender: TObject);


var i:integer;
begin
for i:= 0 to 3 do

showmessage(pchar(listbox1.Items.objects[i]));
end;

Peter Below (TeamB) <10011...@compuXXserve.com> escribió en artículo
<VA.0000339e.00e84365@noname>...

0 new messages