IT22-OOP1-Module-11
IT22-OOP1-Module-11
I. INTRODUCTION
N/A
II. OBJECTIVES
a) Explain the Basics of Listbox in C#
Module 11 b) Provide examples of Listbox applied in a
Windows C# Application.
c) The student should be creating their own
Basic Controls and Visual C# code that uses Listbox after
reading the module.
Objects (Listbox)
A C# ListBox Control provides a user interface to display a list of items. Users can select one or more
items from the list. A ListBox may be used to display multiple columns and these columns may have
images and other controls.
Creating a C# ListBox
There are two approaches to create a ListBox control in Windows Forms. Either we can use the Forms
designer to create a control at design-time or we can use the ListBox class to create a control at run-
time.
In our first approach, we are going to create a ListBox control at design-time using the Forms designer.
To create a ListBox control at design-time, we simply drag a ListBox control from the Toolbox and drop it
to a Form in Visual Studio. After you drag and drop a ListBox onto a Form, the ListBox looks as in image
below. Once a ListBox is on the Form, you can move it around and resize it using the mouse and set its
properties and events.
Page 1 of 12
IT22-OOP (Object Oriented Programming)
College of Computer Studies
The ListBox class represents a ListBox control in Windows Forms. To create a ListBox control at run-time,
we create an instance of the ListBox class, set its properties and add a ListBox object to the Form
controls.
The first step to create a dynamic ListBox is to create an instance of the ListBox class. The following code
snippet creates a ListBox control object:
In the next step, you may set the properties of a ListBox control. The following code snippet sets the
location, width, height, background color, foreground color, Text, Name, and Font properties of a
ListBox:
Page 2 of 12
IT22-OOP (Object Oriented Programming)
College of Computer Studies
Once the ListBox control is ready with its properties, the next step is to add the ListBox to a Form. To do
so, we use the Form.Controls.Add method that adds a ListBox control to the Form controls and displays
it on the Form based on the location and size of the control. The following code snippet adds a ListBox
control to the current Form:
Controls.Add(listBox1);
- C# ListBox Properties
You can set ListBox properties by using Properties Window. In order to get Properties window you can
Press F4 or by right-clicking on a control to get the "Properties" menu item.
Page 3 of 12
IT22-OOP (Object Oriented Programming)
College of Computer Studies
You can set property values in the right-side column of the property window.
In addition to display and selection functionality, the ListBox also provides features that enable you to
efficiently add items to the ListBox and to find text within the items of the list. You can use the Add or
Insert method to add items to a list box. The Add method adds new items at the end of an unsorted list
box.
listBox1.Items.Add("Sunday");
If the Sorted property of the C# ListBox is set to true, the item is inserted into the list alphabetically.
Otherwise, the item is inserted at the end of the ListBox.
You can insert an item into the list box at the specified index.
Page 4 of 12
IT22-OOP (Object Oriented Programming)
College of Computer Studies
listBox1.Items.Insert(0, "First");
listBox1.Items.Insert(1, "Second");
listBox1.Items.Insert(2, "Third");
listBox1.Items.Insert(3, "Forth");
You can bind a List to a ListBox control by create a fresh List Object and add items to the List:
List<string> nList = new List<string>();
nList.Add("January");
nList.Add("February");
nList.Add("March");
nList.Add("April");
The next step is to bind this List to the Listbox. In order to do that you should set datasource of the
Listbox:
listBox1.DataSource = nList;
(Full source):
using System;
using System.Collections.Generic;
using System.Windows.Forms;
Page 5 of 12
IT22-OOP (Object Oriented Programming)
College of Computer Studies
namespace ListBoxDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
V. PRACTICE EXERCISES/ACTIVITIES
Page 6 of 12
IT22-OOP (Object Oriented Programming)
College of Computer Studies
Create a Windows Forms application that looks something like the following:
Other Requirements:
1. When “Present Address” text box is filled in, and the “Same as present address” check box is
checked, the “Permanent Address” text box should copy the value of “Present Address”.
2. Civil Status has the following items:
Page 7 of 12
IT22-OOP (Object Oriented Programming)
College of Computer Studies
4. Supervisor combo box has the following conditions for its items:
a) If “Accounting” department is selected, then (supervisor’s items are):
Page 8 of 12
IT22-OOP (Object Oriented Programming)
College of Computer Studies
5. “Add Button” will display a message box (saying successfully added) and add “some” info of the
employee to the list box:
Page 9 of 12
IT22-OOP (Object Oriented Programming)
College of Computer Studies
(c) After clicking “OK” above, the employee will be added the list (Name | Dept | Supervisor |
Employment Status) box and all the fields should be cleared.
6. Lastly, “Remove Employee” Button will remove the employee’s info in the list:
Page 10 of 12
IT22-OOP (Object Oriented Programming)
College of Computer Studies
Page 11 of 12
IT22-OOP (Object Oriented Programming)
College of Computer Studies
Points Rubric:
VII. ASSESSMENT
N/A
VIII. REFERENCES
Fundamentals of Computer Programming with C# - (The Bulgarian C# Programming Book), Svetlin
Nakov, et al.
https://www.geeksforgeeks.org/
https://www.c-sharpcorner.com/
https://www.w3schools.com/cs/
https://www.tutorialspoint.com/csharp/
https://www.guru99.com/
https://www.programiz.com/
https://www.pluralsight.com/
Page 12 of 12