Microsoft KB Archive/319927

= HOW TO: Clear Items from a Bound ListBox, ComboBox, or CheckedListBox Control =

Article ID: 319927

Article Last Modified on 5/10/2007

-

APPLIES TO


 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



This article was previously published under Q319927





For a Microsoft Visual Basic .NET version of this article, see 327895.



IN THIS TASK
SUMMARY
 * Requirements
 * Create and Bind the List Control to a Data Source
 * Clear the Items Collection When the DataSource Property Is Set to Null

REFERENCES



SUMMARY
This step-by-step article demonstrates how to make sure that the Items collection of a bound Windows Forms list control (such as the ListBox, the ComboBox, or the CheckedListBox control) is emptied when the DataSource property of the control is set to null.

NOTE: Although the steps in this article use the ListBox control, you can apply the same technique to each of the following Windows Forms controls:
 * ListBox
 * CheckedListBox
 * ComboBox

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Windows XP, Microsoft Windows 2000, or Microsoft Windows NT 4.0 Service Pack 6a
 * Microsoft Data Access Components (MDAC) 2.6 or later
 * Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topics:
 * Visual C# .NET syntax
 * Windows Forms

back to the top

Create and Bind the List Control to a Data Source
 Create a new Windows-based application in Visual C# .NET. By default, Form1 is added to the application. Drag a ListBox and a Button control from the toolbox to Form1. By default, ListBox1 and Button1 are created.  Add the following declaration to the beginning of the Form1.cs form, immediately after the &quot;public class Form1&quot; statement: string[] myFruit = {&quot;Apples&quot;, &quot;Oranges&quot;, &quot;Tomatoes&quot;};   Add the following sample code to the Form1_Load event: this.listBox1.DataSource = myFruit; 

back to the top

Clear the Items Collection When the DataSource Property Is Set to Null
  Add the following code to the Button1_Click event: this.listBox1.DataSource = null; </li>  To handle the DataSourceChanged event for ListBox1, add the following code to the Form1 code: private void listBox1_DataSourceChanged(object sender, System.EventArgs e)       { /*           *The following code is required to remove *existing items from the Items collection *when the DataSource is set to null. */ //         ListBox ctlLIST = (ListBox) sender; //         if (ctlLIST.DataSource == null) //             ctlLIST.Items.Clear; }                   </li>  At the end of the InitializeComponent method of Class Form1, bind the listbox1_DataSourceChanged method to the DataSourceChanged event for ListBox1 as follows: this.listBox1.DataSourceChanged += new System.EventHandler(this.listBox1_DataSourceChanged); </li> Press F5 to run the project. Notice that the items from the array (&quot;Apples&quot;, &quot;Oranges&quot;, &quot;Tomatoes&quot;) appear in the list box.</li> Click Button1. Notice that the DataSource property of ListBox1 is set to null, but notice that the items remain visible and are still listed in the ListBox1.Items.</li> Uncomment the last three lines of the listBox1_DataSourceChanged method, and then run the project again.</li> Click Button1. Notice that the list box and the Items collection are cleared.</li></ol>

back to the top

<div class="references_section">