ListBox Control for Visual Basic 6.0 Users

The ListBox control in Visual Basic 6.0 is replaced by either the ListBox control or the CheckedListBox control in Visual Basic 2008. The names of some properties, methods, events, and constants are different, and in some cases there are differences in behavior.

Conceptual Differences

Checked ListBox

In Visual Basic 6.0, the Style property of a ListBox control determines whether a check box is displayed next to each text item. Multiple items in the ListBox can be selected by selecting the check box beside them even if the MultiSelect property is set to False. There is no way to programmatically determine the checked state of an item; if you required this capability, you have to use a ListView control instead.

In Visual Basic 2008, the new CheckedListBox control displays check boxes next to each item; you can no longer display check boxes in the ListBox control. The CheckedListBox control allows you to programmatically determine the checked state of each item through a CheckedListBox..::.CheckedItemCollection collection.

Columns Property

In Visual Basic 6.0, the Columns property took an integer specifying the number of columns to display.

In Visual Basic 2008, the MultiColumn property takes a Boolean value, and the ColumnWidth property takes an integer specifying the width in pixels. If the width of the ListBox control is changed at run time, you may also need to set the ColumnWidth property for each column.

ItemCheck Event

In the Visual Basic 6.0 ListBox control, when the ItemCheck event was raised, the check state was already changed.

In the Visual Basic 2008 CheckedListBox control, when the ItemCheck event is raised the check state has not yet changed. You can get the pending value from the ItemCheckEventArgs argument passed to the event.

Code Changes for the ListBox Control

The following examples illustrate differences in coding techniques between Visual Basic 6.0 and Visual Basic 2008.

Code Changes for Adding and Removing Items in a ListBox Control

The following code demonstrates how to add and remove ListBox items.

' Visual Basic 6.0
' Add an item at the end of the list.
List1.AddItem "Tokyo"
' Insert an item at the top of the list.
List1.AddItem "Copenhagen", 0
' Remove the first item.
List1.RemoveItem 0
'Visual Basic' Add an item at the end of the list.
ListBox1.Items.Add("Tokyo")
' Insert an item at the top of the list.
ListBox1.Items.Insert(0, "Copenhagen")
' Remove the first item.
ListBox1.Items.RemoveAt(0)

Code Changes for Accessing Specific Items in a ListBox Control

The following code demonstrates how to return the value of a ListBox item.

' Visual Basic 6.0
Private Function GetItemText(i As Integer) As String
   ' Return the text of the item using the index:
   GetItemText = ListBox1.List(i)
End Function
' Visual BasicPrivateFunction GetItemText(ByVal i AsInteger) AsString   ' Return the text of the item using the index:
   GetItemText = CStr(ListBox1.Items(i))
EndFunction

Code Changes for Determining Checked Items in a CheckedListBox Control

The following code demonstrates how to determine the checked state of items in a CheckedListBox control.

' Visual Basic 6.0
' The Visual Basic 6.0 ListBox control didn't support this scenario,
' a ListView control had to be used instead.
Dim s As String
Dim i As Integer
' Loop through all items
For i = 1 To ListView1.ListItems.Count
   ' If an item is checked, add it to the string
   If ListView1.ListItems(i).Checked = True Then
      s = s & "Checked Item" & CStr(i) & " = " & _
ListView1.ListItems(i) & vbCrLf
   End If
Next
' Determine if any items are checked.
If s <> "" Then
    MsgBox s
End If
' Visual Basic' Determine if there are any items checked.If CheckedListBox1.CheckedItems.Count <> 0 Then  ' If so, loop through all checked items and print results.Dim x AsIntegerDim s AsString = ""For x = 0 To CheckedListBox1.CheckedItems.Count - 1
      s = s & "Checked Item " & CStr(x + 1) & " = " & _
      CStr(CheckedListBox1.CheckedItems(x)) & ControlChars.CrLf
  Next x
  MessageBox.Show(s)
EndIf

ListBox Control Property, Method, and Event Equivalencies

The following tables list Visual Basic 6.0 properties, methods, and events, along with their Visual Basic 2008 equivalents. Those properties, methods, and events that have the same name and behavior are not listed. Where applicable, constants are indented beneath the property or method. All Visual Basic 2008 enumerations map to the System.Windows.Forms namespace unless otherwise noted.

This table provides links to topics explaining behavior differences. Where there is no direct equivalent in Visual Basic 2008, links are provided to topics that present alternatives.

Properties

Visual Basic 6.0

Visual Basic 2008 Equivalent

Appearance

New implementation. For more information, see Appearance and BorderStyle Properties for Visual Basic 6.0 Users.

BackColor

BackColor

NoteNote:
Colors are handled differently in Visual Basic 2008. For more information, see Color Behavior for Visual Basic 6.0 Users.

Columns

MultiColumn and ColumnWidth

Container

Parent

DataChanged

DataField

DataFormat

DataMember

DataSource

New implementation. For more information, see Data Access for Visual Basic 6.0 Users.

DragIcon

DragMode

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

FontFont

FontBold

FontItalic

FontName

FontSize

FontStrikethrough

FontUnderline

Font

NoteNote:
Fonts are handled differently in Visual Basic 2008. For more information, see Font Object for Visual Basic 6.0 Users.

ForeColor

ForeColor

NoteNote:
Colors are handled differently in Visual Basic 2008. For more information, see Color Behavior for Visual Basic 6.0 Users.

Height

Height, Size

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.

HelpContextID

New implementation. For more information, see Help Support for Visual Basic 6.0 Users.

HWnd

Handle

Index

New implementation. For more information, see Control Arrays for Visual Basic 6.0 Users.

IntegralHeight

IntegralHeight

ItemData

New implementation. For more information, see ItemData property cannot be upgraded.

Left

Left

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.

List

Items

ListCount

Count (List.Count)

ListIndex

SelectedIndex

MouseIcon

New implementation. For more information, see Cannot set a custom MousePointer.

MousePointer

Cursor

For a list of constants, see MousePointer for Visual Basic 6.0 Users.

MultiSelect

SelectionMode

NewIndex

New implementation. For more information, see NewIndex property cannot be upgraded.

OLEDragMode

OLEDropMode

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

Parent

FindForm method

SelCount

Count (SelectedItem.Count)

Selected

GetSelected, SetSelected (ListBox control)

GetItemChecked, SetItemChecked (CheckedListBox control)

Style

New implementation property. 0 – Standard maps to the CheckedListBox control, 1 – Checkbox maps to the CheckedListBox control.

ToolTipText

ToolTip component

For more information, see ToolTip Support for Visual Basic 6.0 Users.

Top

Top

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.

WhatsThisHelpID

New implementation. For more information, see Help Support for Visual Basic 6.0 Users.

Width

Width, Size

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.

Methods

Name

Visual Basic 2008 Equivalent

AddItem

Add

AddRange

Insert

Clear

Clear

Drag

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

Move

SetBounds

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.

OLEDrag

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

RemoveItem

Remove

SetFocus

Focus

ShowWhatsThis

New implementation. For more information, see Help Support for Visual Basic 6.0 Users.

ZOrder

BringToFront or SendToBack method

Events

Visual Basic 6.0

Visual Basic 2008 Equivalent

DblClick

DoubleClick

DragDrop

DragOver

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

GotFocus

Enter

ItemCheck

ItemCheck (CheckedListBox only)

LostFocus

Leave

OLECompleteDrag

OLEDragDrop

OLEDragOver

OLEGiveFeedback

OLESetData

OLEStartDrag

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

Scroll

New implementation. For more information, see TopIndex Property and Scroll Event for Visual Basic 6.0 Users.

Validate

Validating

Upgrade Notes

During upgrade, if the Style property of a Visual Basic 6.0 Listbox is set to 1 – Checkbox, it is upgraded to the CheckedListBox control; otherwise, it is upgraded to the ListBox control.

See Also

Reference

ListBox Control Overview (Windows Forms)

ListBox Control Overview (Windows Forms)

CheckedListBox Control Overview (Windows Forms)