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

how to use AddObject

Status
Not open for further replies.

hayate

Programmer
May 17, 2003
7
JP
Hi,
I'm not sure how to use AddObject.

What I wanna do is...

I use 3 TEdits, 1 TListBox, and 1 TButton.
I write some words in each Edits.
When I push the Button, the name I put into Edit1 will be shown in ListBox.
Then if I choose a certain name from ListBox, the information I put into each 3Edits will be shown again.

I did it once using dynamic array but I want to use AddObject.
I made a class like
TData = class
Text1, Text2, Text3: String;
end;
and used AddObject
ListBox.Items.AddObject(Edit1.Text, Data);
but what should I do next?

Thanks in advance.


 
Code:
type
TMyThreeNames = class(TCollectionItem)
public
  Name2 : String;
  Name3 : String;
end;
....

constructor...
begin
   inherited;
   ACollection := TCollection(TMyThreeNames);
end;

destructor ...
begin
  ACollection.Free;      //  frees all the data
  inherited;
end;

procedure ...ButtonClick...
var
   ThisData : TMyThreeNames;
begin
   ThisData := ACollection.Add as TMyThreeNames;
   ThisData.Name2 := Edit2.Text;
   ThisData.Name3 := Edit3.Text;
   ListBox.AddObject(Edit1.Text, ThisData);
end;

procedure ...ListBoxChange...
begin
   with (Sender as TListBox) do
   begin
      Edit1.Text := Items[ItemIndex];
      with Items.Objects[ItemIndex] as TMyThreeName do
      begin
           Edit2.Text := Name2;
           Edit3.Text := Name3;
      end;
   end;
end;

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top