How to add items to TListBox or TComboBox

Ask questions about how to create a script or swap scripts with other users.
Post Reply
kbieb86
Posts: 41
Joined: 26 Aug 2016 13:49

How to add items to TListBox or TComboBox

Post 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!
User avatar
pjj
Posts: 2109
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: How to add items to TListBox or TComboBox

Post 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.
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
kbieb86
Posts: 41
Joined: 26 Aug 2016 13:49

Re: How to add items to TListBox or TComboBox

Post by kbieb86 »

Thank you, I'll check it out. I haven't done any Pascal since high school in the eighties!
Post Reply