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

Programming 2: List Box

The document discusses list boxes in programming and provides examples of how to use them. A list box allows a user to select one or multiple items from a list. Code samples demonstrate how to load items into a list box, transfer selected items between list boxes, and delete items. Additional examples show using loops like foreach to populate a list box with an array of values. Finally, a sample program is described that uses buttons to add, remove, and clear items in a list box, updating a text box with the item count.
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)
120 views

Programming 2: List Box

The document discusses list boxes in programming and provides examples of how to use them. A list box allows a user to select one or multiple items from a list. Code samples demonstrate how to load items into a list box, transfer selected items between list boxes, and delete items. Additional examples show using loops like foreach to populate a list box with an array of values. Finally, a sample program is described that uses buttons to add, remove, and clear items in a list box, updating a text box with the item count.
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/ 4

Programming 2 BSIT

List Box

List Box – is an ideal way of presenting a list of data or choices to the user.

Processes can be done by the user:


1. Can browse the data in the list box or select one or more items for
processing
2. Can transfer the chosen item or items from the list to a cart or transfer
of items from one list box to another.
3. Can transfer items from list box to a text box or combo box.
4. Can delete items from a listbox

Sample codes on the different processes:


1. Pre- loaded the list box with items to display using the Items.Add method
listBox1.Items.Add(“English”);
listBox1.Items.Add(“Math”);
listBox1.Items.Add(“Science”);
listBox1.Items.Add(“Filipino”);

2. Selected items from List Box 1 will be loaded to List Box 2


listBox2.Items.Add(listBox1.Text);

3. To show the selected item from a list box to a text box.


textBox1.Text = “”;
textBox1.Text =listBox1.Text;

4. Delete items
a. using Items.RemoveAt() method
sample : listBox1.Items.RemoveAt(0);
note: Zero(0) is the index or position of the item that will be
removed

b. using Items.Remove() method


sample : listBox1.Items.Remove(listBox1.SelectedItem);

Sample Program
Design and develop a simple program using looping statements that will
generate a number sequence and display it using list box.

Button

List Box

private void button1_Click(object sender, EventArgs e)

Prepared by : Estela L. Dirain, DITPage 1


Programming 2 BSIT

{ listBox1.BeginUpdate();
int[] intIDArray = new int[5];
intIDArray[0] = 16;
intIDArray[1] = 12;
intIDArray[2] = 14;
intIDArray[3] = 9;
intIDArray[4] = 4;
foreach (int x in intIDArray)
{ listBox1.Items.Add(x.ToString());
}
listBox1.EndUpdate();
}

Other codes using loop:

A. for – loop
listBox1.BeginUpdate();

for (int nCounter =1;nCounter <=5; nCounter ++)


{ listBox1.Items.Add(nCounter.ToString());
}
listBox1.EndUpdate();

B. do while loop
listBox1.BeginUpdate();
int nCounter =1;
do
{ listBox1.Items.Add(nCounter.ToString());
nCounter ++;
}while (nCounter <=5);
listBox1.EndUpdate();

C. while loop
listBox1.BeginUpdate();
int nCounter =1;
while (nCounter <=5)
{ listBox1.Items.Add(nCounter.ToString());
nCounter ++;
}
listBox1.EndUpdate();

D. Array and foreach


listBox1.BeginUpdate();
int[] intIDArray = new int[5];
intIDArray[0] = 16;
intIDArray[1] = 12;
intIDArray[2] = 14;
intIDArray[3] = 9;
intIDArray[4] = 4;
foreach (int x in intIDArray)
{ listBox1.Items.Add(x.ToString());
}
listBox1.EndUpdate();

Sample Program : Using Listbox, Textbox, Add, Delete, Clear, Exit buttons

Prepared by : Estela L. Dirain, DITPage 2


Programming 2 BSIT

Design and develop a simple program that will apply the Items.Add, 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. Sample
design. Note: The user will enter an item first at text box 1 and then clicks
the Add button to add the inputted item in the List Box. Now when the user
wants to remove an item from the list, the user should highlight it first, then
clicks the Remove button to finally delete it. Clicking the Clear button will
wipe out all the items in the List Box.

textBox1

Buttons

listBox1

textBox2

private void Form1_Load(object sender, EventArgs e)


{
textBox1.Text = "";
textBox2.Text = "";
btnAdd.Enabled = false;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
btnDelete.Enabled = listBox1.SelectedIndex != -1;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int nLen;
nLen = textBox1.Text.Length;
if(nLen >0)
{
btnAdd.Enabled = true;
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
textBox1.Text = "";
textBox1.Focus();
textBox2.Text = Convert.ToString(listBox1.Items.Count);
btnAdd.Enabled = false;
}
private void btnDelete_Click(object sender, EventArgs e)
{
int Ind;
Ind = listBox1.SelectedIndex;
if (Ind >= 0)
{
listBox1.Items.RemoveAt(Ind);
textBox2.Text = Convert.ToString(listBox1.Items.Count);

Prepared by : Estela L. Dirain, DITPage 3


Programming 2 BSIT

}
else
{
MessageBox.Show("No more items to delete.");
}
btnDelete.Enabled = (listBox1.SelectedIndex != -1);
}
private void btnClear_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
btnDelete.Enabled = false;
textBox2.Text = Convert.ToString(listBox1.Items.Count);
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}

Prepared by : Estela L. Dirain, DITPage 4

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