Share via


How to: Add and Clear Items in a ListBox Control

This example adds the contents of a Windows Forms TextBox control to a ListBox control when you click button1, and clears the contents when you click button2.

Example

private void button1_Click(object sender, System.EventArgs e)
{
    listBox1.Items.Add("Sally");
    listBox1.Items.Add("Craig");
}

private void button2_Click(object sender, System.EventArgs e)
{
    listBox1.Items.Clear();
}

Compiling the Code

This example requires:

  • A form with a ListBox control named listBox1 and two buttons named button1 and button2. Add the button1Click event handler to button1_Click, and the button2Click event handler to button2_Click.

See Also

Concepts

Designing a User Interface in Visual C#

Other Resources

ListBox and ComboBox Controls

Visual C# Guided Tour