The ListBox and ComboBox objects
If a RadioGroup has many options it takes a lot of space on the screen. A ListBox (Standard tab) shows a long list and adds a scroll bar if it does not fit; a ComboBox takes little room like an Edit: when the triangle on the right is pressed, a hidden list opens. In both, the items are held in the Items property, the number of the chosen item is in ItemIndex (counted from 0, –1 if none) and its text is obtained as ListBox1.Items[i]. In code the list is filled with ListBox1.Items.Add('Olma'); and the number of items is in Items.Count. A SpinEdit is meant for entering a whole number: the number is held in the Value property as an integer, so it need not be passed through StrToInt; the smallest and largest values are in MinValue and MaxValue, the step in Increment. In Delphi 7 the SpinEdit is on the Samples tab; in other environments (for example Lazarus) it may be on another tab.
| Object | Feature |
|---|---|
| RadioGroup | all visible, one is chosen |
| ListBox | long list, scrollable |
| ComboBox | little room, drop-down list |
| SpinEdit | a whole number, Value property |
In a school’s online sign-up form the list of classes is long, so it sits in a small drop-down box: press the triangle and the options appear. The number of tickets is chosen with up and down buttons. Screen space is saved and wrong entries become fewer.
Form designers and web and mobile app developers use drop-down lists to fit many options into a small space.
On paper draw a closed drop-down box (with a triangle on its right) and below it the opened list of 5 invented options.
1procedure TForm1.Button1Click(Sender: TObject); 2var i: Integer; 3begin 4 i := ListBox1.ItemIndex; 5 if i >= 0 then 6 begin 7 ListBox2.Items.Add(ListBox1.Items[i] + ' x ' + IntToStr(SpinEdit1.Value)); 8 total := total + p[i] * SpinEdit1.Value; 9 Label1.Caption := 'Jami: ' + IntToStr(total); 10 end; 11end;
“The fruit shop”: a pair extends the application: adds a new fruit and price, calculates the total for 3 orders in advance and compares it with the result in the application.