Board index » delphi » Populate combo box from array

Populate combo box from array

I am creating a basic program to store user information using arrays,
and would like to include a combo box on a form from which a user can
choose his/her name, and vierw their information.  As this is for a
university project, I have been told to use arrays, rather than DB
functions.  I anm having trouble with the code to populate the combo
box, and would much appreciate any help in this matter.

Thanks,

Dom Boyce

 

Re:Populate combo box from array


Quote
"Dom Boyce" <dombo...@totalise.co.uk> wrote in message

news:7c016cf5.0105180615.140bb436@posting.google.com...

Quote
> I am creating a basic program to store user information using arrays,
> and would like to include a combo box on a form from which a user can
> choose his/her name, and vierw their information.  As this is for a
> university project, I have been told to use arrays, rather than DB
> functions.  I anm having trouble with the code to populate the combo
> box, and would much appreciate any help in this matter.

Array of strings example:

var
    a : array [0..100] of string;
begin
    {...fill the array}
    Combobox1.Items.Clear;
    for i := 1 to 100 do
        Combobox1.Items.Add(a);
end;

to get what item is selected in the combo use Combobox1.ItemIndex

Smola

Re:Populate combo box from array


Maybe a slight correction:

var
    a : array [0..100] of string;
begin
    {...fill the array}
    Combobox1.Items.Clear;
   // Check to ensure that there are elements in the array!
   if numItems > 0 then
   // Start with 0 index, and only add the elements with a value
       for i := 0 to (numItems - 1) do  
         Combobox1.Items.Add(a[i]); // Add the correct element!
end;

Also, as the array is not necessarily full, you're best to have a
variable called numItems or something, that contains the number of
items in the array, for that the for..do loop only adds those elements
of an array that actually contain a valid value.

The array above was also created with a 0 first index (0..100), but
accessed from index value 1. Either value will work, but it  is
necessary to be consistent.

Hope this helps.

Re:Populate combo box from array


The message <9e3c4c$st...@sunce.iskon.hr>
  from  "Smola" <A...@inet.hr> contains these words:

Quote
> var
>     a : array [0..100] of string;
> begin
>     {...fill the array}
>     Combobox1.Items.Clear;
>     for i := 1 to 100 do <----
>         Combobox1.Items.Add(a);<----
> end;

Ahem... Obviously checking if the class was still awake

      for i := 0 to 100 do <----array starts from zero
          Combobox1.Items.Add(a[i]);<---[i]

--
Sincerely,

Andreas Kyriacou
----------------
http://www.andrikkos.co.uk (Imagine! Image Viewer)

Re:Populate combo box from array


In article <2aibgtg6hgbgm6lhflugkojsghe3bhg...@4ax.com>, Gurble

Quote
<gurbleREM...@THISclear.net.nz> writes:
>Maybe a slight correction:

>var
>    a : array [0..100] of string;
>begin
>    {...fill the array}
>    Combobox1.Items.Clear;
>   // Check to ensure that there are elements in the array!
>   if numItems > 0 then
>   // Start with 0 index, and only add the elements with a value
>       for i := 0 to (numItems - 1) do  
>         Combobox1.Items.Add(a[i]); // Add the correct element!
>end;

Perhaps an even better technique for general use where the bounds of an array
may vary, is to add only the number of elements in the array using the High()
function. One could also have a parameter to ignore empty strings ...

procedure AddFromArray(Strings : array of string;
                       IntoList : TStrings;
                       AddEmptyStr : boolean);
var
  i : integer;
begin
  for i := 0 to High(Strings) do
    if AddEmptyStr or (Length(Strings[i]) > 0) then
      IntoList.Add(Strings[i]);
end;

Use as ...
var
  A : array[0..3] of string;
begin
...
AddFromArray(A, ComboBox1.Items, true);

Note that array indexes for "open" arrays (ie a parameter without an explicit
size) _must_ start from zero.

Alan Lloyd
alangll...@aol.com

Alan Lloyd
alangll...@aol.com

Re:Populate combo box from array


On 18 May 2001 07:15:26 -0700, dombo...@totalise.co.uk (Dom Boyce)
wrote:

Quote
>I am creating a basic program to store user information using arrays,
>and would like to include a combo box on a form from which a user can
>choose his/her name, and vierw their information.  As this is for a
>university project, I have been told to use arrays, rather than DB
>functions.  I anm having trouble with the code to populate the combo
>box, and would much appreciate any help in this matter.

Well, it's not at all clear exactly where the array of names is
supposed to come from, and it seems quite clear that an array
of names is not the best thing regardless - whereever the
names come from sticking them into a TSTringlist or into
the combo box directly makes more sense.

But there's no problem shoving an array of strings into
a combobox:

var names: array[0..3] of string; j: integer;
begin
  names[0]:= 'Tom';
  names[1]:= 'dick';
  names[2]:= 'Betty';
  names[3]:= 'Sue';
  for j:=0 to 3 do
    begin
      ComboBox1.Items.Add(names[j]);
    end;
end;

Quote
>Thanks,

>Dom Boyce

David C. Ullrich
***********************
"Sometimes you can have access violations all the
time and the program still works." (Michael Caracena,
comp.lang.pascal.delphi.misc 5/1/01)

Other Threads