You are currently offline, waiting for your internet to reconnect

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

This article was previously published under Q327895
For a Microsoft Visual C# .NET version of this article, see 319927.
This step-by-step article describes how to make sure that the Items collection of a bound Windows Forms list control (such as the ListBox control, the ComboBox control, or the CheckedListBox control) is emptied when the DataSource property of the control is set to Nothing.

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


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 Basic .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 Basic .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.vb form, immediately after the "Public Class Form1" statement:
    Dim strArray As String() = {"Apples", "Oranges", "Tomatoes"}
  4. Add the following sample code to the Form1_Load event:
    Me.ListBox1.DataSource = strArray
back to the top

Clear the Items Collection When the DataSource Property Is Set to Nothing
  1. Add the following code to the Button1_Click event:.
     Me.ListBox1.DataSource = Nothing
  2. To handle the DataSourceChanged event for ListBox1, add the following code to the Form1 code:
        Private Sub ListBox1_DataSourceChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DataSourceChanged        'You must use the following code to remove         'existing items from the Items collection        'when the DataSource is set to Nothing.        'Dim ctlLIST As ListBox        'ctlLIST = sender        'If (ctlLIST.DataSource = Nothing) Then        '    ctlLIST.Items.Clear()        'End If    End Sub
  3. Press F5 to run the project. Notice that the items from the array ("Apples", "Oranges", "Tomatoes") appear in the list box.
  4. Click Button1. Notice that the DataSource property of ListBox1 is set to Nothing, but notice that the items remain visible and are still listed in the ListBox1.Items.
  5. Uncomment the last five lines of the ListBox1_DataSourceChanged method, and then run the project again.
  6. Click Button1. Notice that the list box and the Items collection are cleared.
back to the top
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
313482 INFO: Roadmap for Windows Forms Data
For additional information, visit the following Microsoft Developer Network (MSDN) Web site: Data Binding with Windows Forms and .NET

back to the top

Article ID: 327895 - Last Review: 05/10/2007 05:33:59 - Revision: 3.2

  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • kbideproject kbide kbdatabinding kbhowtomaster KB327895