How to clear all data in a listBox?
I am after a statement that will clear all strings / data that is currently in a l开发者_JAVA百科istBox, I have tried:
private void cleanlistbox(object sender, EventArgs e)
{
listBox1.ResetText();
}
What about
listbox1.Items.Clear();
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";
Try this:
private void cleanlistbox(object sender, EventArgs e)
{
listBox1.DataSource = null;
listBox1.Items.Clear();
}
private void cleanlistbox(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
This should work:
listBox1.Items.Clear();
this should work:
private void cleanlistbox(object sender, EventArgs e)
{
listBox1.Items.Clear( );
}
Use this:
listBox1.Items.Clear();
Try
private void cleanlistbox(object sender, EventArgs e)
{
ListBox1.Items.Clear();
}
If you are using CListBox as pointer (*) use this line: pList1->ResetContent();
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.
In C# Core DataSource does not exist, but this work fine:
listbox.ItemsSource = null;
listbox.Items.Clear();
精彩评论