如何清除listBox中的所有数据?

时间:2011-06-22 15:05:58

标签: c# .net windows listbox

我发表的声明将清除当前列表框中的所有字符串/数据,我尝试过:

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

10 个答案:

答案 0 :(得分:48)

怎么样?
listbox1.Items.Clear();

答案 1 :(得分:6)

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

答案 2 :(得分:5)

这应该有效:

listBox1.Items.Clear();

答案 3 :(得分:5)

如果它绑定到数据源,则会使用ListBox1.Items.Clear();

引发错误

在这种情况下,您将不得不清除数据源。例如,如果它填充了数据表:

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

  lbStyles.DataSource = _dt;
  lbStyles.DisplayMember = "YourDisplayMember";
  lbStyles.ValueMember = "YourValueMember";

答案 4 :(得分:4)

这应该有效:

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

答案 5 :(得分:3)

使用此:

listBox1.Items.Clear();

答案 6 :(得分:2)

尝试

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

答案 7 :(得分:0)

如果将CListBox用作指针(*),请使用以下行: pList1-> ResetContent();

答案 8 :(得分:0)

在C#中,Core DataSource不存在,但是可以正常工作:

static_dir

答案 9 :(得分:0)

如果您的列表框连接到 LIST 作为数据源,则 listbox.Items.Clear() 将不起作用。

我通常会创建一个名为“DataAccess.cs”的文件,其中包含一个单独的类,用于使用或更改与我的表单相关的数据的代码。以下是来自 DataAccess 类的代码片段,用于清除或删除列表“exampleItems”中的所有项目

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

ExampleItem 也在一个名为“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}";
            }
        }
    }
}

在窗口窗体的代码中,以下代码片段引用了您的列表框:

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

此解决方案专门解决了数据源连接到列表的 Windows 窗体列表框。

相关问题