Page 1 of 1

How to add items to TListBox or TComboBox

Posted: 16 Sep 2016 17:29
by kbieb86
I created a combobox as shown (I tried with a listbox too):

Code: Select all

TForm myForm = new TForm(nil);
myForm.Caption = "Form name";
myForm.BorderStyle = bsDialog;
myForm.Position = poScreenCenter;

TComboBox myComboBox = TComboBox.Create(myForm);
myComboBox.Parent = myForm;

myForm.ShowModal;
myForm.Free;
Trying to add an item to the combobox using:

Code: Select all

myComboBox.Items[0] = "Hello world!";
This gives me a "List index out of bounds (0)" error which leads me to believe that I have to declare the capacity of the list array somewhere but I can't figure out where. Does it have something to do with TStringList?

A working example would be much appreciated!

Re: How to add items to TListBox or TComboBox

Posted: 16 Sep 2016 20:19
by pjj
Here ya go:

Code: Select all

myComboBox.Items.Add("Red");
myComboBox.Items.Add("Green");
myComboBox.Items.Add("Blue");
myComboBox.Items.Add("Random Color");
myComboBox.ItemIndex = 2;
This is Pascal, you can look for a hint e.g. at Free pascal wiki as I do.

Re: How to add items to TListBox or TComboBox

Posted: 20 Sep 2016 15:29
by kbieb86
Thank you, I'll check it out. I haven't done any Pascal since high school in the eighties!