Monday, April 21, 2014

C# 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 1:

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

2 comments:

  1. Is this Work When the list is bonded with data source?

    ReplyDelete
  2. Yes it will work if list bonded with data source or manually data filed.

    ReplyDelete