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

Worked examples
“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; procedure TForm1.Button1Click(Sender: TObject); ⏎ var i: Integer; ⏎ begin ⏎  i := ListBox1.ItemIndex; ⏎  if i >= 0 then ⏎  begin ⏎    ListBox2.Items.Add(ListBox1.Items[i] + ' x ' + IntToStr(SpinEdit1.Value)); ⏎    total := total + p[i] * SpinEdit1.Value; ⏎    Label1.Caption := 'Jami: ' + IntToStr(total); ⏎  end; ⏎ end;
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?