0% found this document useful (0 votes)
0 views

IT22-OOP1-Module-11

This document provides an overview of the ListBox control in C# within the context of a Windows Forms application. It explains how to create a ListBox both at design-time and run-time, along with details on setting properties, adding items, and binding a List to the ListBox. Additionally, it includes practice exercises for students to apply their knowledge in creating a functional application.

Uploaded by

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

IT22-OOP1-Module-11

This document provides an overview of the ListBox control in C# within the context of a Windows Forms application. It explains how to create a ListBox both at design-time and run-time, along with details on setting properties, adding items, and binding a List to the ListBox. Additionally, it includes practice exercises for students to apply their knowledge in creating a functional application.

Uploaded by

princejezreel21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

IT22-OOP (Object Oriented Programming)

College of Computer Studies

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)

IV. LESSON PROPER


A. LIST BOX

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.

- Create a ListBox at Design-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

- Create a ListBox Dynamically

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:

ListBox listBox1 = new ListBox();

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:

listBox1.Location = new System.Drawing.Point(12, 12);


listBox1.Name = "ListBox1";
listBox1.Size = new System.Drawing.Size(245, 200);
listBox1.BackColor = System.Drawing.Color.Orange;
listBox1.ForeColor = System.Drawing.Color.Black;

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.

- Add Items in a Listbox

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.

- Insert Items in a 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");

Bind a ListBox to a List

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();
}

private void Form1_Load(object sender, EventArgs e) // Load Event


{
List<string> nList = new List<string>();
nList.Add("January");
nList.Add("February");
nList.Add("March");
nList.Add("April");
listBox1.DataSource = nList; // <--- Set data source
}
}
}

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:

3. Department 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):

b) If “Martketing” department is selected, then:

c) If “IT” department is selected, then:

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:

(a) Fill in all the fields. Then click “Add Employee”.

(b) Display a message box that says “Successfully added employee!”.

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

(a) Select employee on the list and click “Remove Employee”

(b) Employee should not be visible on the list box:

You can download the executable file [here] (PC Only)

Page 11 of 12
IT22-OOP (Object Oriented Programming)
College of Computer Studies

Points Rubric:

Requirements Not Satisfied Partial Good Satisfied


Design is the same or more creative than 0 5 10 15
the given
The required functionality is fulfilled 0 10 20 30
Naming conventions of components 0 5 10 15
(combo boxes, buttons, textboxes, and
list box) are correct*
Total:

VI. ADDITIONAL RESOURCES


 Simple List Box - Basic List Box, C# visual studio - YouTube

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy