C# 如何清除列表框中的所有数据?

C# 如何清除列表框中的所有数据?,c#,.net,windows,listbox,C#,.net,Windows,Listbox,我正在寻找一条语句,该语句将清除列表框中当前的所有字符串/数据,我已尝试: private void cleanlistbox(object sender, EventArgs e) { listBox1.ResetText(); } 那怎么办 listbox1.Items.Clear(); 使用以下命令: listBox1.Items.Clear(); 这应该起作用: private void cleanlistbox(object sender, EventArgs e) {

我正在寻找一条语句,该语句将清除列表框中当前的所有字符串/数据,我已尝试:

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

listbox1.Items.Clear();
使用以下命令:

listBox1.Items.Clear();
这应该起作用:

private void cleanlistbox(object sender, EventArgs e)
{
    listBox1.Items.Clear( );
}
这应该起作用:

listBox1.Items.Clear();
试一试


试试这个:

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

如果它绑定到数据源,它将使用
ListBox1.Items.Clear()抛出错误

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

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

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

\u dt.Clear()// 如果使用CListBox作为指针(*),请使用此行:
pList1->ResetContent()

在C#核心数据源中不存在,但这项工作很好:

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

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

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

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

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();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统配置;
使用System.Linq;
使用System.Windows.Forms;
名称空间//名称空间由Visual Studio自动添加
{
公共分部类YourFormName:Form
{
List exampleItems=新列表();
公共YourFormName()
{
初始化组件();
//将列表框连接到列表
UpdateExampleItemsBinding();
}
私有void UpdateUpdateItemsBinding()
{
ExampleItemsListBox.DataSource=exampleItems;
ExampleItemsListBox.DisplayMember=“FullExampleItem”;
}
私有无效按钮预览框\单击(对象发送者,事件参数e)
{
DataAccess db=新的DataAccess();
exampleItems=db.ClearExampleItems();
UpdateExampleItemsBinding();
}
}
}

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

Lol,我自己应该想到这一点:(擦亮你的第一个“漂亮答案”徽章!@Yuck-谢谢,我很惊讶我是第一个回答的人。6个答案都是一样的。应该有一个
参加合唱团
徽章。:-)我必须将数据源设置为null,然后设置为我的列表,否则它不会接收列表中的更改。也许我会更改这两个命令的顺序。因此,首先是
Clear()
然后是
null
赋值
listbox.ItemsSource = null;
listbox.Items.Clear();
public List<ExampleItem> ClearExampleItems()
       {
           List<ExampleItem> exampleItems = new List<ExampleItem>();
           exampleItems.Clear();
           return examplelistItems;
        }
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();
        }
    }
}