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
56

Practical lesson

Textbook: p. 126
GoalBuilds applications with ListBox, ComboBox and SpinEdit: fills a list with a loop, moves an item to another list and totals values.
New words
Items.Delete: a method that removes the item with the given number from a list · Items.DeleteItems.Count: the number of items in a list · Items.CountOnCreate: the event when the form is created, used for initial values · OnCreateClear: a method that empties a list · Clear
Explanation

In the practical lesson three applications are created. 1) “Even numbers”: in the OnCreate procedure of Form1 the loop for i := 1 to 12 do ListBox1.Items.Add(IntToStr(2 * i)); fills the list with 12 items. 2) “Moving”: a button adds the item chosen in ListBox1 to ListBox2 and removes it from ListBox1: ListBox2.Items.Add(ListBox1.Items[i]); ListBox1.Items.Delete(i); where i := ListBox1.ItemIndex. 3) “Day plan”: a subject is chosen from a ComboBox and minutes (10 to 90) from a SpinEdit; when the button is pressed a “subject – minutes” line is added to ListBox2 and the total time is summed in a Label. In every application check the number of items through Items.Count and calculate the result in advance and write it in your notebook.

💡 Where will I use this?
In real life

When forming teams, a teacher moves invented names from a “waiting” list to a “team” list. After each move the total of names in the two lists stays the same as before. If the total changes, a name has been lost or added twice.

Who uses it

App developers write such moving for adding things to a basket or building a playlist; organisers also divide participants into groups.

Try it today

Write invented numbers on 8 cards, move 3 of them to another pile and check that the two piles still add up to 8 cards.

Worked examples
Pascal
1procedure TForm1.FormCreate(Sender: TObject);
2var i: Integer;
3begin
4  for i := 1 to 12 do
5    ListBox1.Items.Add(IntToStr(2 * i));
6end;
OnCreate – code above. As a result ListBox1.Items.Count = 12, Items[0] = 2 and Items[11] = 24.
ListBox1ListBox2Total
before12012
after “18” is moved11112
Before moving, ListBox1 has 12 items and ListBox2 has 0. After “18” (i = 8) is moved, ListBox1 has 11 and ListBox2 has 1; in total 11+1 = 12 – no items are lost. If 45, 30 and 20 minutes are chosen in the day plan, the total is 45+30+20 = 95 minutes.
Class activity

“The sorter”: a group moves five of the 12 numbers one by one and shows that the sum of the item counts of the two lists stays 12 at every step.

Practice
1
How many items does the list have after for i := 1 to 12 do ListBox1.Items.Add(...)?
2
ListBox1 has 12 items; 4 of them are moved to ListBox2. How many remain in ListBox1?
3
In the list 2, 4, 6, …, what is the index of “18” in Items?
4
Why is Add run before Delete when moving an item?
Story of the Week · The Khwarizmi WayA short circuit — the best way?Close the week: all of this week’s subjects in one storyRead →