23

I am after a statement that will clear all strings / data that is currently in a listBox, I have tried:

private void cleanlistbox(object sender, EventArgs e)
{
    listBox1.ResetText();
}
1
  • 12
    6 answers, and they're all the same. There should be a Participated in a choir badge. :-) Commented Jun 22, 2011 at 16:36

11 Answers 11

67

What about

listbox1.Items.Clear();
4
  • 1
    Lol, should have thought of that myself :(
    – Mike
    Commented Jun 22, 2011 at 15:11
  • 1
    Grats on your first "Nice Answer" badge!
    – Yuck
    Commented Jun 22, 2011 at 15:40
  • @Yuck - Thanks, I'm surprised I was the first to answer. Commented Jun 22, 2011 at 15:54
  • This doesn't work if the DataSource is set. Sadly clearing the DataSource (in my case a BindingList) also doesn't work. Commented Oct 17, 2023 at 14:46
10

If it is bound to a Datasource it will throw an error using ListBox1.Items.Clear();

In that case you will have to clear the Datasource instead. e.g., if it is filled with a Datatable:

  _dt.Clear();   //<-----Here's the Listbox emptied.
  _dt = _dbHelper.dtFillDataTable(_dt, strSQL);

  lbStyles.DataSource = _dt;
  lbStyles.DisplayMember = "YourDisplayMember";
  lbStyles.ValueMember = "YourValueMember";
1
  • I had to set the DataSource to null and then to my list, it was not picking up the change in the list otherwise. Commented Jun 10, 2019 at 15:44
8

Try this:

 private void cleanlistbox(object sender, EventArgs e)
  {
     listBox1.DataSource = null;
     listBox1.Items.Clear();
  }
0
7
private void cleanlistbox(object sender, EventArgs e)
{
    listBox1.Items.Clear();
}
6

This should work:

listBox1.Items.Clear();
5

this should work:

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

Use this:

listBox1.Items.Clear();
3

Try

private void cleanlistbox(object sender, EventArgs e)
{
   ListBox1.Items.Clear();
}
1

If you are using CListBox as pointer (*) use this line: pList1->ResetContent();

0

If your listbox is connected to a LIST as the data source, listbox.Items.Clear() will not work.

I typically create a file named "DataAccess.cs" containing a separate class for code that uses or changes data pertaining to my form. The following is a code snippet from the DataAccess class that clears or removes all items in the list "exampleItems"

public List<ExampleItem> ClearExampleItems()
       {
           List<ExampleItem> exampleItems = new List<ExampleItem>();
           exampleItems.Clear();
           return examplelistItems;
        }

ExampleItem is also in a separate class named "ExampleItem.cs"

using System;

namespace        // The namespace is automatically added by Visual Studio
{
    public class ExampleItem
    {
        public int ItemId { get; set; }
        public string ItemType { get; set; }
        public int ItemNumber { get; set; }
        public string ItemDescription { get; set; }

        public string FullExampleItem 
        {
            get
            {
                return $"{ItemId} {ItemType} {ItemNumber} {ItemDescription}";
            }
        }
    }
}

In the code for your Window Form, the following code fragments reference your listbox:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Windows.Forms;

namespace        // The namespace is automatically added by Visual Studio
{
    
    public partial class YourFormName : Form
    {
        
        List<ExampleItem> exampleItems = new List<ExampleItem>();

        public YourFormName()
        {
            InitializeComponent();

            // Connect listbox to LIST
            UpdateExampleItemsBinding();
        }

        private void UpdateUpdateItemsBinding()
        {
            ExampleItemsListBox.DataSource = exampleItems;
            ExampleItemsListBox.DisplayMember = "FullExampleItem";
        }

        private void buttonClearListBox_Click(object sender, EventArgs e)
        {
            DataAccess db = new DataAccess();
            exampleItems = db.ClearExampleItems();
            
            UpdateExampleItemsBinding();
        }
    }
}

This solution specifically addresses a Windows Form listbox with the datasource connected to a list.

-1

In C# Core DataSource does not exist, but this work fine:

listbox.ItemsSource = null;
listbox.Items.Clear();
1
  • 1
    Maybe I would change the order of the both commands. So first Clear() then the null assignment
    – KargWare
    Commented Dec 4, 2020 at 14:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.