HOW TO: Clear Items from a Bound ListBox, ComboBox, or CheckedListBox Control (319927)



The information in this article 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 Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition
  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# .NET (2003)

This article was previously published under Q319927
For a Microsoft Visual Basic .NET version of this article, see 327895.

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

  1. Create a new Windows-based application in Visual C# .NET. By default, Form1 is added to the application.
  2. Drag a ListBox and a Button control from the toolbox to Form1. By default, ListBox1 and Button1 are created.
  3. Add the following declaration to the beginning of the Form1.cs form, immediately after the "public class Form1" statement:
    string[] myFruit = {"Apples", "Oranges", "Tomatoes"};
    					
  4. 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

  1. Add the following code to the Button1_Click event:
    this.listBox1.DataSource = null;
    					
  2. 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();
    		}
    					
  3. 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);
    					
  4. Press F5 to run the project. Notice that the items from the array ("Apples", "Oranges", "Tomatoes") appear in the list box.
  5. 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.
  6. Uncomment the last three lines of the listBox1_DataSourceChanged method, and then run the project again.
  7. Click Button1. Notice that the list box and the Items collection are cleared.
back to the top

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

313482 INFO: Roadmap for Windows Forms Data Binding

324832 HOWTO: Bind Data to a Control at Design-Time and Convert Data Source Data to Particular .NET-Types Data

For additional information, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MajorLast Reviewed:9/24/2003
Keywords:kbDataBinding kbHOWTOmaster kbIDEProject KB319927 kbAudDeveloper