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.
Worked examples
OnCreate: procedure TForm1.FormCreate(Sender: TObject); ⏎ var i: Integer; ⏎ begin ⏎ for i := 1 to 12 do ⏎ ListBox1.Items.Add(IntToStr(2 * i)); ⏎ end; As a result ListBox1.Items.Count = 12, Items[0] = 2 and Items[11] = 24.
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(...)?
12
2
ListBox1 has 12 items; 4 of them are moved to ListBox2. How many remain in ListBox1?
8
3
In the list 2, 4, 6, …, what is the index of “18” in Items?
8
4
Why is Add run before Delete when moving an item?
If Delete removes the item first, no text is left to add; its copy is taken first.