Udostępnij za pośrednictwem


ComboBox Control Change Event for Visual Basic 6.0 Users

In Visual Basic 6.0, the Change event of the ComboBox control is raised when the text in the text-entry portion of the control is modified; it 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.

When a project is migrated 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.

The Visual Basic 2008 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 Add method is called.

The following example illustrates the behavior differences.

' Visual Basic 6.0
Private Sub Form_Load()
   ' Does not raise the Change event.
   Combo1.AddItem "A"
End Sub
Private Sub Form_Click()
   ' Does not raise the Change event.
   Combo1.List(0) = "B"
End If
' Visual BasicPrivateSub Form1_Load()
   ' Raises the TextChanged event.
   ComboBox1.Items.Add("A")
EndSubPrivateSub Form1_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs)
   ' Uses the SetItemString method from the VB6 compatibility library;    ' there is no equivalent method in Visual Basic.   ' Raises the TextChanged event.
   Microsoft.VisualBasic.Compatibility.VB6. _
      SetItemString(ComboBox1, ComboBox1.Items.Count, "B")
EndSub

What to do next

  • Set a breakpoint in the TextChanged event procedure and then run your code to determine where it is raised. Modify your code as necessary.

See Also

Concepts

ComboBox Control for Visual Basic 6.0 Users