Board index » delphi » Combobox.Items.AddObject question

Combobox.Items.AddObject question

i have a form with a combobox and a bunch of edit fields. the editfield.text
should be tied to the entry in the combobox, so that picking a new combobox
entry populates the edit fields. and if the user fills in a new entry for
the combobox and editfields, then the edit fields are remembered.

i was thinking that all the editfields would be in an object and this object
added to the combobox using the AddObject method.

i must not understand something about this, since this sample code isn't
working... can anyone help with what i am doing wrong? no runtime error,
just the editboxes aren't changing. i'm not sure if the object isn't being
saved correctly, or if it isn't being retrieved correctly.

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    Edit1: TEdit;
    btnSave: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure ComboBox1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TProfile = class
    pName : string;
    pText : string;
  end;

var
  Form1: TForm1;
  Profile: TProfile;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Profile := TProfile.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Profile.Free;
end;

procedure TForm1.btnSaveClick(Sender: TObject);
begin
   Profile.pName := ComboBox1.Text;
   Profile.pText := Edit1.Text;
   ComboBox1.Items.AddObject(ComboBox1.Text,Profile);
end;

procedure TForm1.ComboBox1Click(Sender: TObject);
begin
  Profile := TProfile(ComboBox1.Items.Objects[ComboBox1.ItemIndex]);
  Edit1.Text := Profile.pText;
end;

--
thanks,
Eric Kundl

 

Re:Combobox.Items.AddObject question


You are only working with a single instance of tProfile and at a guess I'd
say that is the root of your problem. What you need to do is create a new
profile for each item in the combobox. You will also have to remember to
release these objects when the combobox is destroyed. (For a number of
reasons I'd suggest that you either change the ancestry of tProfile (my
preference), or release the objects in the form's OnClose event.)

Type
    tProfile = class (tComponent)
        private
            fName,
            fText        : string;
        public
            constructor CreateEx (aOwner : tComponent; aName, theText :
string);
            property pName : string read fName write fName;
            property pText : string read fText write fText;
        end;
tForm1 = class (tForm)
    . . .

constructor tProfile.CreateEx (aOwner : tComponent; aName, theText :
string);

begin
Create (aOwner);
pName := aName;
pText := theText;
end;

// when you create the combobox entries
var aProfile : tProfile;

aProfile := tProfile.CreateEx (theComboBox, 'theName', 'theText');
theComboBoxl.Items.AddObject ('some text', aProfile);
. . .

By descending from tComponent and using the combobox as the Owner property,
the code does not have to free the profile objects. They will be freed by
the combobox when it is destroyed.

Re:Combobox.Items.AddObject question


thank you Bruce for the reply...

--
thanks,
Eric Kundl
Colorado Springs, CO
http://home.adelphia.net/~erickundl/

Quote
"Bruce Roberts" <b...@bounceitattcanada.xnet> wrote in message

news:_P7R7.3269$Q06.23138@tor-nn1.netcom.ca...
Quote
> You are only working with a single instance of tProfile and at a guess I'd
> say that is the root of your problem. What you need to do is create a new
> profile for each item in the combobox. You will also have to remember to
> release these objects when the combobox is destroyed. (For a number of
> reasons I'd suggest that you either change the ancestry of tProfile (my
> preference), or release the objects in the form's OnClose event.)

> Type
>     tProfile = class (tComponent)
>         private
>             fName,
>             fText        : string;
>         public
>             constructor CreateEx (aOwner : tComponent; aName, theText :
> string);
>             property pName : string read fName write fName;
>             property pText : string read fText write fText;
>         end;
> tForm1 = class (tForm)
>     . . .

> constructor tProfile.CreateEx (aOwner : tComponent; aName, theText :
> string);

> begin
> Create (aOwner);
> pName := aName;
> pText := theText;
> end;

> // when you create the combobox entries
> var aProfile : tProfile;

> aProfile := tProfile.CreateEx (theComboBox, 'theName', 'theText');
> theComboBoxl.Items.AddObject ('some text', aProfile);
> . . .

> By descending from tComponent and using the combobox as the Owner
property,
> the code does not have to free the profile objects. They will be freed by
> the combobox when it is destroyed.

Other Threads