Mike
Mike

Reputation: 253

How to clear all data in a listBox?

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

Upvotes: 23

Views: 138907

Answers (12)

Shahzaib Atif
Shahzaib Atif

Reputation: 1

I want to add one thing to the previously given answers. After clearing out the listbox, if you still cannot see the changes, it is probably because it needs to be refreshed.

listBox1.DataSource = null;
listBox1.Items.Clear();
listbox1.Refresh();

Upvotes: 0

Versalytics
Versalytics

Reputation: 23

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.

Upvotes: 0

name
name

Reputation: 1

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

listbox.ItemsSource = null;
listbox.Items.Clear();

Upvotes: -1

Murali Krishna M.
Murali Krishna M.

Reputation: 77

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

Upvotes: 0

Mario
Mario

Reputation: 293

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";

Upvotes: 10

Sherif Hamdy
Sherif Hamdy

Reputation: 593

Try this:

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

Upvotes: 8

Scorpion
Scorpion

Reputation: 4585

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

Upvotes: 7

Saurabh
Saurabh

Reputation: 5727

Try

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

Upvotes: 3

RaM
RaM

Reputation: 1107

This should work:

listBox1.Items.Clear();

Upvotes: 6

Viper
Viper

Reputation: 2236

this should work:

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

Upvotes: 5

Yuck
Yuck

Reputation: 50865

Use this:

listBox1.Items.Clear();

Upvotes: 4

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25475

What about

listbox1.Items.Clear();

Upvotes: 67

Related Questions