Professional Documents
Culture Documents
Programming 2-Chapter 5
Programming 2-Chapter 5
Looping Statements
- The looping statement is a program instruction that repeats some statement or
sequence of statements in a specified number of times.
- Allows a set of instructions to be performed all over and over again until a certain
condition is reached, met, proven or tested as false or true.
- Allows us to execute one or more lines of code repetitively.
increment-where we say by how much to increase the loop counter on each pass through the loop.
decrement- where we say by how much to decrease the loop counter on each pass through the loop.
foreach loop- repeats a group of statements for each element in a collection of objects or in an array
instead of repeating the statements in a specified number of times.
- especially helpful if we don’t know how many elements are in a collection
initialization;
while (condition) {
statement(s);
increment/decrement;
}
initialization;
do {
statement(s);
increment/decrement;
} while (condition);
List Box- is an ideal way of presenting a list of data or choices to the user.
- In fact, it is an effective way to present a large number of choices or data to the user in a limited
amount of space.
- The user can browse the data in the list box or select one or select one or more items for
processing purposes.
- The process could be transferring the chosen items from the list box to a cart (or a transfer of
items from one list box to another)
- The transfer can also be from a list box to a text box or to a combo box.
For example, in the computerized grocery store system, the items could be the products which the
buyer can select and buy.
- By default, the items in the list box are displayed vertically in a single column, although we can
set up multiple columns too.
- When the list of items or data is too long for the list box, the Visual C# system will simply add a
vertical scroll bar. With this, the user can then scroll up and down or left or right through the list
of items.
Example 1:
Design and develop a simple looping statement program that generates the given sequence
numbers using the for statement.
Coding Solution:
for (nCounter=1;nCounter<=5;nCounter++)
{
listBox1.Items.Add(“Loop “ + nCounter.ToString());
}
listBox1.EndUpdate();
}
Example 2:
Design and develop a simple looping statement program that generates the given sequence
numbers using the do-while loop syntax. (1 to 5).
Coding Solution:
private void button1_Click(object sender, EventArgs e)
{
listbox1.BeginUpdate();
int nCounter=1;
do {
listBox1.Items.Add(“Loop “ + nCounter.ToString());
nCounter++;
} while (nCounter<=5);
listBox1.EndUpdate();
}
Example 3:
Design and develop a simple looping statement program that generates the given inverse
sequence numbers using the for statement (5 to 1).
Coding Solution:
for (nCounter=5;nCounter>=1;nCounter- -)
{
listBox1.Items.Add(“Loop “ + nCounter.ToString());
}
listBox1.EndUpdate();
}
Example 4:
Design and develop a simple looping statement program that generates the given inverse
sequence numbers using the do-while loop syntax. (5 to 1).
Coding Solution:
private void button1_Click(object sender, EventArgs e)
{
listbox1.BeginUpdate();
int nCounter=5;
do {
listBox1.Items.Add(“Loop “ + nCounter.ToString());
nCounter--;
} while (nCounter>=1);
listBox1.EndUpdate();
}
Simple Array
Array- a special type of variable which can contain or hold one or more values of the same data
type with reference to only one variable name.
- In other words, the array variable has a common name identifier and can hold many
values at the same time, provided they have the same data type.
- An array variable can be distinguished through a pair of square brackets [] and on
specified number inside them which is called an index.
- In case of enumerated arrays, we can distinguish its declaration through a pair of curly
brackets: { }.
Data_type ArrayName[ ]
Example 1:
strIDArray[0]=”Monkey”;
strIDArray[1]=”Zebra”;
strIDArray[2]=”Elephant”;
strIDArray[3]=”Eagle”;
strIDArray[4]=”Crocodile”;
Example 2:
intIDArray[0]=6;
intIDArray[1]=12;
intIDArray[2]=14;
intIDArray[3]=9;
intIDArray[4]=4;
Design and develop a simple program that demonstrates how to use the for each loop syntax
that display each item in an array with a string data.
Coding Solution:
strIDArray[0]=”Monkey”;
strIDArray[1]=”Zebra”;
strIDArray[2]=”Elephant”;
strIDArray[3]=”Eagle”;
strIDArray[4]=”Crocodile”;
listBox1.EndUpdate();
}
Example 6:
Design and develop a simple program that demonstrates how to use the for each loop syntax
that display each item in an array with an integer data.
Coding Solution:
intIDArray[0]=6;
intIDArray[1]=12;
intIDArray[2]=14;
intIDArray[3]=9;
intIDArray[4]=4;
Example 7:
Design and develop a simple program that demonstrates how to use the for each loop syntax
that counts how many objects are there in a Form.
Coding Solution:
textBox1.Text=nIterate.ToString();
}
Example 8:
Design and develop a simple program that demonstrates how to preload a collection of items to
a list box at run time.
Example 9:
Design and develop a simple program that demonstrates how to preload the list box with items.
When the user chooses an item at the list box by double-clicking it, that particular item will be displayed
at the text box.
Coding Solution:
listBox1.Items.Add(“Chicken”);
listBox1.Items.Add(“Beef”);
listBox1.Items.Add(“Pork”);
listBox1.Items.Add(“Vegetables”);
listBox1.Items.Add(“Fish”);
listBox1.Items.Add(“Squids”);
listBox1.Items.Add(“Noodles”);
listBox1.Items.Add(“Fruits”);
}
Example 10:
Design and develop a simple program that demonstrates how to preload a collection of items into the
list box as well as adding an item into the list box which is entered by the user in the text box. The
program also will be able to remove all the items one by one, starting from the top down to the bottom.
Coding Solution:
Item.Count – the property use to determine the number of items in the list box.
- If it is equals to zero , then the list box is already empty.
Example 11:
Design and develop a simple program that demonstrates how to preload a collection of items
into the List box. The user should be able to remove a selected item on the list, one at a time. When the
user selects by clicking an item at the List box, that particular item must be deleted from the displayed
list.
Example 12: Highlight first an item before confirming to delete it using a button
Design and develop a simple program that demonstrates how to preload a collection of items
into the list box. The program should be able to remove a selected item on the list, once at a time but
with a confirmation using a button.
Design and develop a simple program that demonstrates how to preload a collection of items
into the list box. The program should be able to display one or more selected items in another list box.
When the user selects by clicking an item at the list box 1, that particular item must be displayed at the
list box 2.
Coding solution:
Design and develop a simple program that demonstrates how to preload a collection of
items into the list box. The program should be able to transfer one or more selected items on
another list box.
Coding Solution:
Design and develop a simple program that demonstrates how to preload a collection of items
into the List box. The program should be able to get the value of the currently selected item in
the list box. The selected item should display its corresponding description at the text box.
Coding Solution:
else if(listBox1.Text==”Philippines”)
{
textBox1.Text=””;
textBox1. Text=”Philippines is in Asia”;
}
else if(listBox1.Text==”Singapore”)
{
textBox1.Text=””;
textBox1. Text=”Singapore is in Asia”;
}
else if(listBox1.Text==”Japan”)
{
textBox1.Text=””;
textBox1. Text=”Japan is in Asia”;
}
else if(listBox1.Text==”Alaska”)
{
textBox1.Text=””;
textBox1. Text=”Alaska is in North America”;
}
else if(listBox1.Text==”California”)
{
textBox1.Text=””;
textBox1. Text=”California is in North America”;
}
else if(listBox1.Text==”Italy”)
{
textBox1.Text=””;
textBox1. Text=”Italy is in Europe”;
}
else if(listBox1.Text==”France”)
{
textBox1.Text=””;
textBox1. Text=”France is in Europe”;
}
else if(listBox1.Text==”Alabama”)
{
textBox1.Text=””;
textBox1. Text=”Alabama is in North America”;
}
else if(listBox1.Text==”Brazil”)
{
textBox1.Text=””;
textBox1. Text=”Brazil is in Latin America”;
}
else if(listBox1.Text==”Venezuela”)
{
textBox1.Text=””;
textBox1. Text=”Venezuela is in Latin America”;
}
Example 15: Adding, Removing, and Clearing an item in the list box
Design and develop a simple program that should apply the Items.Add,
Items.Remove, and Clear methods with the SelectedIndex and Items.Count properties
to add, remove and clear the list entries in the List box at run time.
Coding Solution:
Design and develop a simple program that should be able to select two or more
items in the list box at once and be able to transfer the collection of selected items to
another list box simultaneously. Press Shift and arrow keys to select multiple items.
Coding Solution:
listBox1.SetSelected(1, true);
listBox1.SetSelected(3, true);
listBox1.SetSelected(5, true);
}
Warning: Don’t forget to set the Multi-Select property of list box 1 with the 2-Extended
setting. It is very important, otherwise, our program will not run on our intended
purpose.
Using Combo Box
Combo box- derived from the words ”combination box”, meaning it combines the
features of both a text box and a list box.
- Allows the user to select an item either by typing text into the combo box, or by
selecting a predefined item from the list.
- Allows us to select a predefined item from a list or to enter a new item which is
not in the list.
- Like a list box, a combo box presents a list of choices to the user.
- If the number of items exceeds what can be displayed in the combo box, scroll
bars will automatically appear on the control.
A potential problem with a list box is that in some situations, you are stuck with
the entries displayed. You can’t directly edit an item in the list or select an entry that’s
not already there. Though it’s a big advantage compared to combo box is that if you
want to restrict the user, then a list box is best suited in this situation. While the
strength of a combo box is that it takes less room on a form than a normal list box,
because the full list is not displayed until the user clicks the down arrow. In other words,
a combo box can easily fit in a small space where a list box would not fit. Moreover, a
combo box contains an edit field, so choices not in the list can be typed in this field by
the user. In most program development situations, a combo box is appropriate when
there is a list of suggested choices, and a list box is appropriate when you want to limit
input to what s on the list.
Example 17:
Design and develop a simple application system that preloads a combo box with
items. The user can select an item from the list to be displayed at the text box. When
the user selects an item at the Combo box by clicking on it, this item will be displayed at
the text box. The user must click the down arrow (found at the right corner of the
combo box) to be able to see the preloaded list of items.
Coding Solution:
Example 18:
Design and develop a simple program that demonstrates how to preload a
collection of items into the combo box. The program should be able to display one or
more selected items on a list box. When the user selects an item at the Combo box by
clicking it, this item will be displayed at the list box. The user must click the down arrow
to be able to see the preloaded list of items.
Coding Solutions:
Example 19:
Design and develop a simple program that demonstrates how to preload a collection of
items into the combo box. The program should be able to display one or more selected
items on a list box with a confirmation using the button.
Coding Solution:
Lab. Ex.
1. Design and develop a simple application system that preloads a Combo box with
items. The user can select an item from the list to be displayed at the text box.
The user must click the down arrow to be able to see the list of preloaded items.
When the user selects an item at the Combo box by clicking it, this item will be
displayed at the text box.
Add the following items into the Combo box using the Items.Add method at the
Form_Load method:
Dilly Bar
Plain Dog
Cheese Dog
Chilimelt Dog
Relish Dog
Bacon n Cheese
Fudge Brownie
Banana Split
Peanut Buster
2. Design and develop a simple application system that preloads a Combo box with
items. The user can select an item from the List to be displayed at the list box.
The user must click the down arrow to be able to see the list of preloaded items.
When the user selects an item at the Combo box by clicking it, this item will be
displayed at the text box.
Add the following items into the Combo box using the Items.Add method at the
Form_Load method:
Al Tonno
Chorizo
Arrabiata
Pesto
Carbonara
Mushroom Sauce
Pasta
Mexicali
Veggies Heaven
Mushroom Galore
Neptune’s Fancy
3. Design and develop a simple application system that preloads a Combo box with
items. The user can select an item from the list to be displayed at the list box with
a confirmation using the button.
When the user selects an item at the Combo box and would like to order it, she
has to click the Order button to display the selected item into the list box.
Add the following Starbucks Café Menu into the Combo box using the Items.Add
method at the Form_Load method:
Cheese Tempura
Ceasar’s Salad
Cajun Burger
Citrus Pork
Burger Steak
Café Mocha
Café Latte
Cappuccino
Steamed Milk
Espresso
Brewed Coffee