Microsoft KB Archive/327895

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

Article ID: 327895

Article Last Modified on 5/10/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q327895





For a Microsoft Visual C# .NET version of this article, see 319927.



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 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

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 Basic .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 Basic .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.vb form, immediately after the &quot;Public Class Form1&quot; statement: Dim strArray As String = {&quot;Apples&quot;, &quot;Oranges&quot;, &quot;Tomatoes&quot;}   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   Add the following code to the Button1_Click event:. Me.ListBox1.DataSource = Nothing </li>  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 </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 Nothing, but notice that the items remain visible and are still listed in the ListBox1.Items.</li> Uncomment the last five 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">