Programming 2: List Box
Programming 2: List Box
List Box
List Box – is an ideal way of presenting a list of data or choices to the user.
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
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
{ 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();
}
A. for – loop
listBox1.BeginUpdate();
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();
Sample Program : Using Listbox, Textbox, Add, Delete, Clear, Exit buttons
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
}
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();
}