0% found this document useful (0 votes)
14 views28 pages

INF154 Practical 8 2025

The document outlines Practical 8 for INF 154, focusing on arrays and ListBoxes in a Windows form application. It includes objectives for declaring and using arrays, implementing loops, and managing ListBox items, along with practical exercises to create programs that manipulate colors and images. Additionally, it provides a rubric for assessment and submission guidelines for the practical assignment.

Uploaded by

Khau Motsage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views28 pages

INF154 Practical 8 2025

The document outlines Practical 8 for INF 154, focusing on arrays and ListBoxes in a Windows form application. It includes objectives for declaring and using arrays, implementing loops, and managing ListBox items, along with practical exercises to create programs that manipulate colors and images. Additionally, it provides a rubric for assessment and submission guidelines for the practical assignment.

Uploaded by

Khau Motsage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INF 154

PRACTICAL 8
2025

1
At the end of this practical you should be able to…

1. Be able to declare and use an array.


2. Be able to use repetition loops to go through the indices
of an array.
3. Be able to work with the items of a ListBox.
4. Be able to work with an Array or ImageList control as an
indexed list of images

2
What are we going to do?

– Discussion: Arrays and ListBoxes


– Practical 8a: Completed in class.
– Practical 8b: Completed in class.
– Practical 8c: Take home practical.

3
Controls Naming Convention Table
Control Prefix Example
Description
Name
Form frm frmMain Represents the main window or form.
Button btn btnSubmit Triggers an action when clicked.
Label lbl lblUsername Displays static text or information.
TextBox txt txtUsername Input field for user text entry.
RichTextBox rtxt rtbDescription Multi-line text input with formatting.
CheckBox chk chkSubscribe Represents a binary choice (checked/unchecked).
RadioButton rdo rdoMale Allows selection of one option from a group.
ComboBox cmb cmbCountry Drop-down list for selecting an option.
ListBox lst lstItems Displays a list of selectable items.
DateTimePicker dtp dtpBirthDate Used to pick dates and times.
NumericUpDown nud nudQuantity Allows numeric input with increment/decrement buttons.
Panel pnl pnlContainer Groups related controls together.

4
Controls Naming Convention Table Continued
Control Prefix Example Name Description
TrackBar (Slider) trk trkVolume Used for adjusting a range of values.
ProgressBar prg prgDownload Shows progress of an ongoing operation.
PictureBox pic picProfile Displays an image.
GroupBox grp grpPersonalInfo Groups related controls inside a labeled
box.
TabControl tab tabSettings Allows multiple tabbed pages.
DataGridView dgv dgvEmployees Displays data in a tabular format.
MenuStrip mnu mnuFile Represents the main menu bar.
ToolStrip tls tlsMain A toolbar with buttons and drop-downs.
Timer tmr tmrAutoSave Used to execute actions at set intervals.
OpenFileDialog ofd ofdSelectFile Dialog box for selecting files to open.
SaveFileDialog sfd sfdSaveFile Dialog box for saving files.
FolderBrowserDialog fbd fbdSelectFolder Dialog box for selecting folders.

5
Discussion: Arrays
Declaring an array:
• Basic declaration with fixed size:
int[] numbers = new int[3];
• Declaration with Initialization (Inline Values):
int[] numbers = new int[] { 10, 20, 30 };
• Shorthand Initialization:
int[] numbers = { 10, 20, 30 };
• Declare First, Assign Later
int[] numbers;
numbers = new int[3];
• Using a Constant or Variable for Size
const int size = 3;
int[] scores = new int[size];
Assigning values:
To assign a value to an element in an array, you need to access that specific
element using the specific index and assigning the value using an equal sign.
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;

6
Discussion: Arrays
Looping over an array:
• Always start at index 0, because arrays in C# are zero-based. array[0] is the first
item, not array[1].
• Use array.Length instead of a hardcoded number. This allows your loop to adapt to
any array size, making your code more flexible and error-resistant.
for (int i = 0; i < array.Length; i++)
{
MessageBox.Show(array[i]);
}
• Be careful not to go out of bounds. Accessing an element like array[array.Length]
will cause an index out of range error because indexing starts at 0. To access the
last element, use array.Length - 1.
• A loop can be used not just to read or access array values, but also to assign values.
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = (i + 1) * 10; // This will assign the values 10, 20, 30, ...
}
• You can also use a reverse loop to access array elements in reverse order:
for (int i = array.Length - 1; i >= 0; i--)
{
MessageBox.Show(array[i]);
}

7
Discussion: ListsBoxes and List Elements

• A ListBox is a GUI control used to display a list of items. You can use it to
show values from an array or collect items entered by the user.
• ListBox items are stored in the .Items collection, which behaves like an
indexed list. The first item is at index 0, just like in an array.
lstNames.Items[0] = "Clark Kent"; // Changes the first item
• You can loop through ListBox items using a for loop and the .Items.Count
property to avoid going out of bounds.
for (int i = 0; i < lstNames.Items.Count; i++)
{
MessageBox.Show(lstNames.Items[i].ToString());
}
• To add items to a ListBox, use .Items.Add(). This adds a new item to the
bottom of the list.
lstNames.Items.Add("Bruce Wayne");
• To remove an item, use .Items.Remove() (by value) or .Items.RemoveAt()
(by index).
lstNames.Items.Remove("Clark Kent"); // Removes by name
lstNames.Items.RemoveAt(0); // Removes first item

8
Discussion: ListsBoxes and List Elements

• To get the selected item or its index, use:


string selected = lstNames.SelectedItem.ToString();
int index = lstNames.SelectedIndex;
• Be careful when accessing the selected item. Always check that
something is selected first to avoid errors.
if (lstNames.SelectedIndex != -1)
{
MessageBox.Show("You selected: " +
lstNames.SelectedItem.ToString());
}
• You can clear the entire list using .Items.Clear().
lstNames.Items.Clear();

9
Practical Exercise 8a

Objective: In this practical section, you are tasked with creating a windows
form program which will change the background colour from red to green to
blue every second. The colours should be saved in an array.

10
Practical Exercise 8a
Expected Screen Design
Create the following form with the appropriate controls as shown below.
Remember to name your controls accordingly (e.g for the label you would
use lblHeader).

Label
Listbox

Button
Controls

11
Practical Exercise 8a
Expected Button Functionality
btnShow will be responsible for populating a listbox with the colours in the

array.
btnChangeColour will be responsible for starting the timer which will change

the background colour of the form every second. As the background colour
changes, the label should also change.

12
Practical Exercise 8a
Solution – btnShow and btnChangeColour

13
Practical Exercise 8a
Solution – tmrChangeColour event handler

14
Practical Exercise 8b

Objective: In this practical section, you are tasked with creating a windows
form program which will use multiple pictures to create a moving picture.
This should be achieved using an Array along with a timer event handler.

15
Practical Exercise 8b
Expected Functionality
Upon startup of the Windows form program (load event), the picture box

should cycle through the given list of images from frame01 to frame07 until
the picture box moves from the left to the right of the form.

Expected Screen Design

PictureBox

16
Practical Exercise 8b
Solution: Files
Add an “Assets” folder in your project by right-clicking on your project from

the solution explorer and navigate to “Add” ➔ “New Folder”.

17
Practical Exercise 8b
Solution: Files
Once the “Assets” folder has been created, Download, unzip and copy the

images into the “Assets” folder. The images can be found on ClickUP in the
“Practicals” ➔ “Week of 19 to 23 May 2025” folder. Once the images have
been copied into the newly created “Assets” folder, right-click the project in
the solutions explorer and add the images to the project using the “Add” ➔
“Existing Item…” option. Navigate to the “Assets” folder and select all the
images. If the images are not appearing, make sure that “All Files” are
selected at the bottom.

18
Practical Exercise 8b
Solution: Files
Once the files are added to the project, you need to update the “Copy to

Output Directory” property for each of the image files to either “Copy
Always” or “Copy if newer”. This adds the images and “Assets” folder to the
build process so that it exists in the same directory as the created .exe file.
Also remember to update the “SizeMode” property on the PictureBox so
that the image fits within the frame of the PictureBox.

19
Practical Exercise 8b
Solution – Similar to 8a

20
Practical Exercise 8c

Objective: In this practical section, you are tasked with creating a program
that generates an array when the form loads. Both the size of the array and
the values within it must be randomly generated.
• The array size must be a random number between 5 and 10.
• Each element in the array must be a random temperature value
between 0 and 50.
• The array must be initialised and populated in the Form_Load event.
• The generated values must be displayed in a disabled RichTextBox, with
each line representing a single temperature value.

21
Practical Exercise 8c

The application must contain two tabs:


1. Tab: "Calculations"
• Displays the size of the array.
• Contains a GroupBox with two radio buttons:
– One to calculate and display the average temperature.
– One to identify and display the second highest temperature in
the array.
• Includes a "Display Result" button that shows the output based on
the selected RadioButton.
• Provide an error message in a MessageBox if no option is selected.
2. Tab: "Temperature Readings"
• Displays the temperature values in a disabled RichTextBox.
• Contains a label that describes the content of the RichTextBox.

22
Practical Exercise 8c
Expected Screen Design and Functionality
Create the following form with the appropriate controls. Remember to
name your controls accordingly (e.g for a button you would use btnDisplay).
This program should use the tabPages control to display the Calculations
and Temperature Readings page. The RichTextBox should be populated
with the random generated temperature values.

23
Practical Exercise 8c
Expected Functionality
Provide a meaningful error when no radio button is selected when the
“Display Result” button is pressed.

24
Practical Exercise 8c
Expected Functionality
When “Average Temperature” is selected and the “Display Result” button
pressed, calculate the average temperature using the temperature values in
the array and present the average in a MessageBox.

25
Practical Exercise 8c
Expected Functionality
When “Second Highest Temperature Reading” is selected and the “Display
Result” button pressed, sort the array in ascending order using a nested for
loop. Present the second highest value in the array in a MessageBox.
Update the RichTextBox to also display the value in ascending order.

26
Practical 8 Rubric
Criteria Mark
Array is created during the Form_Load event with a random size between 5 and 10 2

Array is populated with random temperatures between 0 and 50 1


All temperatures are displayed in a disabled RichTextBox, one per line. 1
Application includes two tabs with correct names and required controls in each tab 1
The size of the array is correctly displayed on the “Calculations” tab 1
Selecting the "Average" radio button calculates and displays the correct average 2
Selecting the "Second Largest" radio button correctly identifies and displays the second 2
highest value
Total /10

27
Practical 8 Submission

Submit your 8c project as Practical Assignment 8 on ClickUP as


Follows:
–Name your project, “uXXXXXXXX_INF154_Practical_8” (where
XXXXXXXX is your student number) and compress (zip) your project
folder (the main folder that contains your .sln file and all subfolders).
–Submit it under the Practical 8 submission link.
–Due Date: 25 May 23:59

28

You might also like