How to Add and Remove Items in C# Listbox

How to Add and Remove Items in C# Listbox

Problem:

Create a C# program that allows the user to add and remove items in a listbox control.

About the program

We will first define what a listbox control is.

A listbox is a graphical control that displays the list of items and allows you to select an item from the list.

This tutorial will teach us on how to add, remove, load and clear items from the listbox. The program will allow you to enter a string, a name to be specific and add or save it in the listbox, the listbox will display all the names added by the user. You can also select an item you want to remove or delete from the list.

Let the tutorial begin.

  1. Open your Microsoft Visual Studio 2010
  2. Create a New Project.
  3. Select Visual C# then Windows Form Application.
  4. This is a sample layout of the form.

The form needs the following controls:

1 label
1 textbox
1 listbox
4 buttons

listbox1

  1. We will first write a code for adding an item. Double click the Add to List button and paste the following code.
if (textBox1.Text == "")
{
MessageBox.Show("please enter a name");
textBox1.Focus();
}
else
{
listBox1.Items.Add(textBox1.Text);
textBox1.Text = "";
textBox1.Focus();
}

Code Explanation:

The user must not leave the field empty or else the program will prompt you a message, the program will then add the name entered by the user to the listbox and clear the textbox for another entry.

  1. Next is the code to remove an item from the listbox, double click the Remove from list button and paste the code.
listBox1.Items.Remove(listBox1.SelectedItem);

Code Explanation:

The code will remove the item that is being selected by the user.

Note: the user must first select what item from the list he or she wants to remove or delete.

  1. We will now load data or items to the listbox, double click the Load list button and paste the code below.
listBox1.Items.Add("John");
listBox1.Items.Add("Mark");
listBox1.Items.Add("Joe");
listBox1.Items.Add("Philip");

Code Explanation:

This will add four items or names to the listbox. You can add other names if you want.

  1. To clear the listbox (remove all items in one click), double click the clear list button and paste the code. The code below will empty or remove all the list from the listbox.
listBox1.Items.Clear();
  1. What if you want to display the selected item in a messagebox? The code below is the answer to that problem.
MessageBox.Show("selected: " + listBox1.SelectedItem.ToString());

Note: you can place the code above in the click or double click event of the listbox

  • Select first the listbox.
  • In the properties window, select the Events button (refer to the image below).
  • Select or double click the DoubleClick event of the listbox.

The code should be like this:

private void listBox1_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show("selected: " + listBox1.SelectedItem.ToString());
}

listbox2

Double click the item from the listbox that you want to be displayed in a messagebox.

  1. Save the project, press F5 to run the program.

Everything that you have added to the listbox will be deleted once the program is terminated.

The next version of the program is a dynamic version with database.
Download

Comments

comments

, , , , ,

Post navigation