HOW TO: Clear Items from a Bound ListBox, ComboBox, or CheckedListBox Control (327895)
The information in this article applies to:
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
This article was previously published under Q327895 For a Microsoft Visual C# .NET version of this
article, see
319927. 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 RequirementsThe 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 "Public Class Form1" statement:
Dim strArray As String() = {"Apples", "Oranges", "Tomatoes"} - 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 - 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
- Press F5 to run the project. Notice that the items from the
array ("Apples", "Oranges", "Tomatoes") appear in the list box.
- 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.
- Uncomment the last five lines of the ListBox1_DataSourceChanged method, and then run the project again.
- Click Button1. Notice that the list box and the Items
collection are cleared.
back to
the top
Modification Type: | Major | Last Reviewed: | 9/3/2003 |
---|
Keywords: | kbIDEProject kbide kbDataBinding kbHOWTOmaster KB327895 kbAudDeveloper |
---|
|