ComboBox Control for Visual Basic 6.0 Users

The ComboBox control in Visual Basic 6.0 is replaced by the Windows Forms ComboBox 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

Change Event

In Visual Basic 6.0, the Change event of the ComboBox control is raised when the text of the control is modified. The Change event is not raised when an item is selected from the list portion of the control. Programmatically changing the text of an item in the list does not raise the Change event.

In Visual Basic 2008, the Change event no longer exists; the TextChanged event is raised every time the text changes for any reason, such as:

  • When the text-entry portion is modified

  • When an item is selected from the list

  • When a list item is modified programmatically

  • When the AddItem method is called

ItemData Property

In Visual Basic 6.0, the ItemData property for a ComboBox control can be set at design time in the Properties window to associate an Integer with a ComboBox item.

In Visual Basic 2008, the ItemData property no longer exists. The Microsoft.VisualBasic.Compatibility library contains a SetItemData function that can be used to associate an Integer with an item; the GetItemData function can be used to retrieve the item.

Locked Property

In Visual Basic 6.0, the Locked property of a ComboBox control determines whether the text-box portion of the control can be edited.

In Visual Basic 2008, the Locked property prevents a control from being moved at design time. There is no direct equivalent for the Visual Basic 6.0 Locked property; however, you can achieve the same effect by setting the DropDownStyle property of the ComboBox control to DropDownList.

Note   In Visual Basic 6.0, setting the Locked property to True also prevents the selection from being changed. You can duplicate this behavior by canceling the selection in the MouseDown event.

NewIndex Property

In Visual Basic 6.0, the NewIndex property was used to retrieve the index of the item most recently added to aComboBox control.

In Visual Basic 2008, the NewIndex property no longer exists. You can use the return value from the Item.Add method to retrieve the index of an item as it is added.

TopIndex Property

In Visual Basic 6.0, the TopIndex property is used to return or set a value that specifies which item in a ComboBox or ListBox control is displayed in the topmost position. This property is commonly used to scroll the list without selecting an item.

In Visual Basic 2008, the ComboBox control no longer supports the TopIndex property. In most cases this should not present a problem, because setting the TopIndex property has no visible effect unless the Style property is set to 1 - SimpleCombo. In this case, you can emulate this behavior by using a ListBox control and a TextBox control; the ListBox control still supports the TopIndex property.

Scroll Event

The Scroll event in Visual Basic 6.0 is used in conjunction with the TopIndex property to perform actions when the list is scrolled. Visual Basic 2008 does not support the Scroll event; however, in most cases the SelectedIndexChanged event is a suitable replacement.

Other Differences

In addition, there are numerous conceptual differences that apply to all controls, including differences in data binding, font handling, drag-and-drop operations, Help support, and more. For more information, see Windows Forms Concepts for Visual Basic 6.0 Users.

Code Changes for the ComboBox Control

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

Code Changes for Making a ComboBox Control Read-Only

The following code demonstrates how to prevent a user from adding new items to a ComboBox control.

' Visual Basic 6.0
Combo1.Locked = True
' Visual Basic
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList

Code Changes for Selecting the Most Recently Added Item in a ComboBox Control

The following code demonstrates how to set the selection to an item that has been added programmatically to a ComboBox control.

' Visual Basic 6.0
Combo1.AddItem "This is a new item"
Combo1.ListIndex = Combo1.NewIndex
' Visual BasicDim i AsInteger
i = ComboBox1.Items.Add("This is a new item")
ComboBox1.SelectedIndex = i

Private Sub Code Changes for Associating ItemData

The following code demonstrates how to associate an employee number with each employee listed in a ComboBox control then how to retrieve that number at run time.

' Visual Basic 6.0
Private Sub Form_Load
    Combo1.AddItem "Nancy Davolio"
    Combo1.ItemData(Combo1.NewIndex) = 12345
    Combo1.AddItem "Judy Phelps"
    Combo1.ItemData(Combo1.NewIndex) = 67890
End Sub
Private Sub Combo1_Click()
    Label1.Caption = "Empoyee #" & _
    CStr(Combo1.ItemData(Combo1.ListIndex))
End Sub
' Visual BasicPrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) HandlesMyBase.Load

   ComboBox1.Items.Add("Nancy Davolio")
   Microsoft.VisualBasic.Compatibility.VB6. _
      SetItemData(ComboBox1, ComboBox1.Items.Count() - 1, 12345)

   ComboBox1.Items.Add("Judy Phelps")
   Microsoft.VisualBasic.Compatibility.VB6. _
      SetItemData(ComboBox1, ComboBox1.Items.Count() - 1, 67890)
EndSub
' Visual BasicPrivateSub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

   Label1.Text = "Employee #" & CStr( _
   Microsoft.VisualBasic.Compatibility.VB6. _
      GetItemData(ComboBox1, ComboBox1.SelectedIndex))
EndSub

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

Container

Parent

DataChanged

DataField

DataFormat

DataMember

DataSource

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

DragIcon

DragMode

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

Font

FontBold

FontItalic

FontName

FontSize

FontStrikethrough

FontUnderline

Font

NoteNote:
Fonts are handled differently in Visual Basic 2008. For more information, see Font Handling 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.

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, Coordinate System for Visual Basic 6.0 Users.

List

Items

ListCount

Count

ListIndex

SelectedIndex

Locked

DropDownStyle = DropDownList

NoteNote:
In Visual Basic 6.0, setting the Locked property to True also prevents the selection from being changed. In Visual Basic 2008, you can duplicate this behavior by canceling the selection in the MouseDown event.

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.

NewIndex

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

OLEDragMode

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

OLEDropMode

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

Parent

FindForm method

RightToLeft

RightToLeft

SelLength

SelectionLength

SelStart

SelectionStart

SelText

SelectedText

Style

DropDownStyle

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.

TopIndex

New implementation. For more information, see TopIndex Property and Scroll Event 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

Visual Basic 6.0

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

Drag and Drop for Visual Basic 6.0 Users.

RemoveItem

Items.Remove

SetFocus

Focus

ShowWhatsThis

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

ZOrder

BringToFrontSendToBack functions

Events

Visual Basic 6.0

Visual Basic 2008 Equivalent

Change

TextChanged

NoteNote:
The behavior of the TextChanged event is slightly different. For more information, see ComboBox Control Change Event for Visual Basic 6.0 Users.

Click

SelectedIndexChanged

DblClick

New implementation. Use the SelectedIndexChanged or TextChanged events.

DragDrop

DragOver

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

GotFocus

Enter

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

When a Visual Basic 6.0 project is upgraded to Visual Basic 2008, the Change event of the ComboBox control is mapped to the TextChanged event of the Visual Basic 2008 ComboBox control. The behavior of the TextChanged event differs from that of the Change event; this difference may cause unintended results in your code.

Any code that references the ItemData, Locked, NewIndex, or TopIndex properties or any code in the Scroll event procedure is not upgraded; warning comments are added to the code and the code must be removed or modified before compiling the application.

See Also

Concepts

Color Behavior for Visual Basic 6.0 Users

Font Object for Visual Basic 6.0 Users

MousePointer for Visual Basic 6.0 Users

ToolTip Support for Visual Basic 6.0 Users

Reference

ComboBox Control Overview (Windows Forms)