tags:

views:

87

answers:

2

hello,

I want to know if it is possible in delphi to populate a combobox component from object Tcollection.

somme codes:

// My product list procedure TfoMain.InitForm; begin FListProduct := TListeDispoProduit.Create(TProduct);

  with (FListProduct ) do
  begin
    with TProduct(Add) do
    begin
      Name := 'Product 01';
      CIP := 'A001';
      StockQty := 3;
    end;

    with TProduct(Add) do
    begin
      Name := 'Product 02';
      CIP := 'A002';
      StockQty := 5;
    end;
  end;

// need to fill a combobox (name's cbxListProduct)

procedure TfoMain.fFillCbxFromProductList(aProductList: FListProduct);
begin
      // I don't know how to do this follow 
    foMain.cbxListProduct.Items.Add()
end;

thank you.

+4  A: 

Something like this (change combobox and collection names to reflect your case):

for i := 0 to Collection.Count-1 do
    myComboBox.Items.Add(TProduct(Collection.Items[i]).Name);

And by the way, you don't need that "foMain" in

foMain.cbxListProduct.Items.Add()

It's enough to write

cbxListProduct.Items.Add()

When you're inside of TfoMain's procedure, TfoMain's contents is accessible by default.

himself
Please edit the tone of your posting. Vaguely insulting sarcasm has no place on a site whose purpose is to help out people, especially when the question is a legitimate one.
Mason Wheeler
Okay, sorry. Shouldn't have done that.
himself
In fact you should (almost) never refer to a form variable (in this case foMain) within methods of the form. It will fail badly if there is ever more than one instance of the form. If you want to disambiguate use `Self.` instead.
Gerry
A: 

In newer Delphis you can do

for item in collection do
    myComboBox.Items.Add(TProduct(item).Name)
awmross