AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Contents · Computing

ListBox, ComboBox and SpinEdit

Lessons 55–56 · 2 lessons · N. I. Taylaqov (general editor), A. B. Akhmedov, M. D. Pardayeva, A. A. Abdug‘aniyev, U. M. Mirsanov. Informatics and Information Technologies, Grade 10, 1st edition. “Extremum-Press”, Tashkent, 2017
55

The ListBox and ComboBox objects

Textbook: pp. 121–126
GoalUses the ListBox, ComboBox and SpinEdit objects, fills a list with Items.Add, gets the chosen item with ItemIndex and Items[i] and sets the Value, MinValue and MaxValue properties of a SpinEdit.
New words
ListBox: a box that shows a list of items, one or more of which can be chosen · ListBoxComboBox: an input field with a drop-down list · ComboBoxSpinEdit: an input field for whole numbers with up and down arrows · SpinEditItems.Add: a method that adds a new line at the end of a list · Items.Add
Explanation

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.

ObjectFeature
RadioGroupall visible, one is chosen
ListBoxlong list, scrollable
ComboBoxlittle room, drop-down list
SpinEdita whole number, Value property
💡 Where will I use this?
In real life

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.

Who uses it

Form designers and web and mobile app developers use drop-down lists to fit many options into a small space.

Try it today

On paper draw a closed drop-down box (with a triangle on its right) and below it the opened list of 5 invented options.

Worked examples
Pascal
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;
“Fruit”: ListBox1 – Olma, Nok, Uzum, Anor (price of 1 kg: 12000, 15000, 20000, 25000 so‘m); SpinEdit1 – kg (MinValue = 1, MaxValue = 10, Value = 1); ListBox2 – the order. global: var p: array[0..3] of Integer; total: Integer; – code above.
The prices p[0] := 12000; p[1] := 15000; p[2] := 20000; p[3] := 25000; and total := 0; are written in OnCreate. Olma 3 kg (3·12000 = 36000), then Uzum 2 kg (2·20000 = 40000): ListBox2 has two lines and Jami: 76000. The third item is ListBox1.Items[2] = Uzum, because counting starts at 0.
Class activity

“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.

Practice
1
Which property holds the number of the chosen item in a ListBox?
2
If ListBox1.Items.Add('Anor') is run when the list holds 4 items, how many items are there?
3
Nok costs 15000 so‘m/kg. What is the total for 4 kg of Nok and 2 kg of Anor (25000)?
4
Why is StrToInt not needed for SpinEdit.Value?
Story of the Week · The Khwarizmi WayA short circuit — the best way?Close the week: all of this week’s subjects in one storyRead →