Discussion:
D6pe: how to use Object in TListbox.Additem?
csroberts
2002-08-05 16:31:26 UTC
Permalink
I am thinking of writing a simple todo list application (as kind of a
learning experience). A list box would have multiple columns, with a
Done checkbox, a Due Date, and a task name. If the user double
clicked, a Notes box (TMemo) would appear. I noticed you can store
objects with a Listbox. Here is the AddItem method:

procedure AddItem(Item: String; AObject: TObject);

What can AObject be? Does it have to be something descended from a
TObject?
Can it be a simple data type like a string or integer?
Can it be a complex data type like TStringList?

I looked at the Delphi help and it wasn't very useful. I'm trying to
store this todo list and notes in a file for later use.

Thanks.
msbeausang
2002-08-05 16:36:19 UTC
Permalink
You can add anything that is descended from TObject.. In your case it
will not accept a simple data type like integer or string, but a
TStrinlist is a descendent of TObject so you can add a Stringlist.

hth
Stephen B
Post by csroberts
I am thinking of writing a simple todo list application (as kind of a
learning experience). A list box would have multiple columns, with a
Done checkbox, a Due Date, and a task name. If the user double
clicked, a Notes box (TMemo) would appear. I noticed you can store
procedure AddItem(Item: String; AObject: TObject);
What can AObject be? Does it have to be something descended from a
TObject?
Can it be a simple data type like a string or integer?
Can it be a complex data type like TStringList?
I looked at the Delphi help and it wasn't very useful. I'm trying to
store this todo list and notes in a file for later use.
Thanks.
The Delphi Top Sites!
http://www.sandbrooksoftware.com/TS/TS2/delphi.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Martin Wynne
2002-08-05 16:59:25 UTC
Permalink
Hi,
Post by csroberts
procedure AddItem(Item: String; AObject: TObject);
What can AObject be? Does it have to be something
descended from a TObject?
yes.
Post by csroberts
Can it be a simple data type like a string or integer?
no.
Post by csroberts
Can it be a complex data type like TStringList?
Yes, like this:

n:=my_listbox.Items.AddObject( 'my_listbox_string', TStringList.Create);

Then to use the stringlist do:

TStringList(my_listbox.Items.Objects[n]).LoadFromFile(my_file);

string_m_in_the_list:=TStringList(my_listbox.Items.Objects[n]).Strings[m];

Remember to do:

TStringList(my_listbox.Items.Objects[n]).Free;

when done.

regards,

Martin.



The Delphi Top Sites!
http://www.sandbrooksoftware.com/TS/TS2/delphi.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Stephen Wood
2002-08-05 17:30:09 UTC
Permalink
Actually, you can store an integer, or any other type that is a simple type,
and has the same storage size as an TObject (pointer) or less...for
instance, you can say...

var
AList : TStringList;
begin
AList := TStringList.Create;
try
AList.AddObject ('Test 1',TObject(5));
AList.AddObject ('Test 2',TObject(10));
ShowMessage ('The value of Test 2 is ' + IntToStr
(integer(AList.Objects[AList.IndexOf ('Test 2')])));
finally
AList.Free;
end;
end;

But this won't work for strings....

----- Original Message -----
From: "Martin Wynne" <***@ision.co.uk>
To: <delphi-***@yahoogroups.com>
Sent: Monday, August 05, 2002 6:59 PM
Subject: Re: [Delphi] D6pe: how to use Object in TListbox.Additem?
Post by Martin Wynne
Hi,
Post by csroberts
procedure AddItem(Item: String; AObject: TObject);
What can AObject be? Does it have to be something
descended from a TObject?
yes.
Post by csroberts
Can it be a simple data type like a string or integer?
no.
Post by csroberts
Can it be a complex data type like TStringList?
n:=my_listbox.Items.AddObject( 'my_listbox_string',
TStringList.Create);
Post by Martin Wynne
TStringList(my_listbox.Items.Objects[n]).LoadFromFile(my_file);
string_m_in_the_list:=TStringList(my_listbox.Items.Objects[n]).Strings[m];
Post by Martin Wynne
TStringList(my_listbox.Items.Objects[n]).Free;
when done.
regards,
Martin.
The Delphi Top Sites!
http://www.sandbrooksoftware.com/TS/TS2/delphi.shtml
---------------------------------------------------------------
---------------------------------------------------------------
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
The Delphi Top Sites!
http://www.sandbrooksoftware.com/TS/TS2/delphi.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Joe Pearson
2002-08-06 07:57:50 UTC
Permalink
It can *point* to a string. But you have to allocate the string yourself:

var
MyString: PString; // PString declared in SysUtils, I think
begin
New(MyString);
ListBox1.Items.AddObject('text', TObject(MyString));

When it comes to freeing,
MyString := Pstring(Listbox1.Items[n]);
Dispose(MyString);
Listbox1.Items[n].Delete;

This code is ottomh but should work.

Joe
-----Original Message-----
I am thinking of writing a simple todo list application (as kind of a
learning experience). A list box would have multiple columns, with a
Done checkbox, a Due Date, and a task name. If the user double
clicked, a Notes box (TMemo) would appear. I noticed you can store
procedure AddItem(Item: String; AObject: TObject);
What can AObject be? Does it have to be something descended from a
TObject?
Can it be a simple data type like a string or integer?
Can it be a complex data type like TStringList?
I looked at the Delphi help and it wasn't very useful. I'm trying to
store this todo list and notes in a file for later use.
Loading...