You are on page 1of 31

Lesson 04:

Looping / Iteration
MIT 22033: VISUAL PROGRAMMING
By: S. Sabraz Nawaz
Senior Lecturer in MIT
Department of MIT, FMC
GUI: ListBox, ComboBox and
CheckedListBox

MIT22033-Visual Programming, By: S. Sabraz Nawaz


GUI: ListBox
• The ListBox control is best used if you are trying
to display a large number of items which you can
select. By default, you can only select one item.
• If the total number of items exceeds the number
that can be displayed, a scroll bar is automatically
added to the ListBox control.

• When the MultiColumn property is false, the list box displays


items in a single column and a vertical scroll bar appears.
• When the MultiColumn property is set to true, the list box
displays items in multiple columns and a horizontal scroll bar
appears.
• The SelectionMode property determines how many list items can
be selected at a time.

MIT22033-Visual Programming, By: S. Sabraz Nawaz


ListBox: Add Items
• To add items at design time, locate the Items property in the control’s Properties
window and click the ellipsis button.
• A new window will pop up — the String Collection Editor window — in
which you can add the items you want to display in the list.
• Each item must appear on a separate text line, and blank text lines will result in
blank lines in the list.
• To programmatically add the string to the list, use the Add method. The
collection is referenced using the Items property:

MIT22033-Visual Programming, By: S. Sabraz Nawaz


ListBox: Add Items (Runtime)

MIT22033-Visual Programming, By: S. Sabraz Nawaz


Listbox’s default event

MIT22033-Visual Programming, By: S. Sabraz Nawaz


ListBox: DoubleClick event

MIT22033-Visual Programming, By: S. Sabraz Nawaz


ComboBox
• ComboBox control is another way of allowing a user to choose
from a set of options.
• The ComboBox control looks like a text box with a button in its
right side.
• When the button is clicked, the ComboBox show a drop-down box
containing the list of options/items available.
• The user can then choose from these options by clicking one of
them.
• The selected option will then be the text inside the ComboBox.

Event Description
Click Occurs when the component is clicked.
Occurs when the value of the SelectedIndex
SelectedIndexChanged
property is changed.

MIT22033-Visual Programming, By: S. Sabraz Nawaz


ComboBox: Some properties

Property Description

Items The items in the combo box.

Specifies whether the items on the combo box should be


Sorted
sorted.

Text The default text when there is no item selected.

Gets or sets the selected index. Every item has an index


SelectedIndex starting from 0 to (number of items – 1). A value of -1
indicates that no item is selected.

SelectedItem Gets or sets the item of the currently selected item.

MIT22033-Visual Programming, By: S. Sabraz Nawaz


ComboBox

Checks if none is selected

MIT22033-Visual Programming, By: S. Sabraz Nawaz


Iteration Structure

MIT22033-Visual Programming, By: S. Sabraz Nawaz


Looping
• Looping refers to the repeated execution of statements
• Comes in very handy because it means that you can repeat
operations as many times as you want without having to write the
same code each time
• A repetition statement allows you to specify that an application
should repeat an action while some condition remains true

MIT22033-Visual Programming, By: S. Sabraz Nawaz


Looping
o As a simple example, consider the following code for calculating the amount of money in
a bank account after 10 years, assuming that interest is paid each year and no other
money flows into or out of the account:

 Writing the same code 10 times


seems a bit wasteful, and what if
you wanted to change the
duration from 10 years to some
other value?
 Luckily, you don’t have to do
this; you can have a loop that
executes the instruction you want
the required number of times.
 Another important type of loop is
one in which you loop until a
certain condition is fulfilled.
MIT22033-Visual Programming, By: S. Sabraz Nawaz
The WHILE loop
• The Boolean test in a while loop takes place at the start of the loop
cycle.
• The while statement executes a statement or a block of statements
until a specified expression becomes false.
• If the test evaluates to false, then the loop cycle is never executed.
Instead, program execution jumps straight to the code following
the loop.

Syntax:

MIT22033-Visual Programming, By: S. Sabraz Nawaz


WHILE loop example I

MIT22033-Visual Programming, By: S. Sabraz Nawaz


WHILE loop example II

MIT22033-Visual Programming, By: S. Sabraz Nawaz


While loop Example III

MIT22033-Visual Programming, By: S. Sabraz Nawaz


While loop Example III…

MIT22033-Visual Programming, By: S. Sabraz Nawaz


The DO loop
• A do loop is similar to the while loop, except that it
checks its condition at the end of the loop.
• The code you have marked out for looping is executed, a
Boolean test is performed, and the code executes again if
this test evaluates to true, and so on. When the test
evaluates to false, the loop exits.
Syntax:

MIT22033-Visual Programming, By: S. Sabraz Nawaz


DO loop example

MIT22033-Visual Programming, By: S. Sabraz Nawaz


DO loop example

• The do…while statement tests the loop-continuation condition after


executing the loop’s body; therefore, the body always executes at
least once

MIT22033-Visual Programming, By: S. Sabraz Nawaz


The FOR Loop
• C# provides the for repetition statement, which specifies the
elements of counter-controlled-repetition in a single line of code.
• This type of loop executes a set number of times and maintains its
own counter.
• In general, counter-controlled repetition should be implemented
with a for statement.
• To define a for loop you need the following information:
o A starting value to initialize the counter variable
o A condition for continuing the loop, involving the counter variable
o An operation to perform on the counter variable at the end of each loop
cycle

MIT22033-Visual Programming, By: S. Sabraz Nawaz


Syntax

MIT22033-Visual Programming, By: S. Sabraz Nawaz


FOR loop example

MIT22033-Visual Programming, By: S. Sabraz Nawaz


FOR loop with IF example

MIT22033-Visual Programming, By: S. Sabraz Nawaz


FOR Loop Example: Odd or Even

MIT22033-Visual Programming, By: S. Sabraz Nawaz


FOREACH Loop
• A foreach loop enables you to address each element in an array
using this simple syntax:

• This loop will cycle through each element, placing it in the


variable <name> in turn, without danger of accessing illegal
elements.
• You don’t have to worry about how many elements are in the
array, and you can be sure that you’ll get to use each one in the
loop.

MIT22033-Visual Programming, By: S. Sabraz Nawaz


FOREACH Loop

MIT22033-Visual Programming, By: S. Sabraz Nawaz


CheckedListbox
• CheckedListBox control is similar to the ListBox
control except that each item is represented by a
CheckBox control. You can select each item like an
ordinary ListBox, but in addition, you can also check
the box beside each item.
• The CheckedBoxControl has some exclusive
properties listed below.
Properties Description

CheckedIndices A collection of checked indices of the CheckedListBox control.

CheckedItems A collection of checked items.


Specifies whether to check the box of an item if the item is
CheckOnClick
selected.

MIT22033-Visual Programming, By: S. Sabraz Nawaz


Example: CheckedListbox

MIT22033-Visual Programming, By: S. Sabraz Nawaz


End of the Lesson

MIT22033-Visual Programming, By: S. Sabraz Nawaz

You might also like