ComboBox 控件更改事件(针对 Visual Basic 6.0 用户)
更新:2007 年 11 月
在 Visual Basic 6.0 中,如果修改了 ComboBox 控件文本输入部分的文本,则会引发该控件的 Change 事件;如果从该控件的列表部分选择某个项,则不会引发该事件。以编程方式更改列表项的文本不会引发 Change 事件。
项目迁移到 Visual Basic 2008 后,ComboBox 控件的 Change 事件映射到 Visual Basic 2008ComboBox 控件的 TextChanged 事件。TextChanged 事件的行为不同于 Change 事件的行为,此差异可能会在代码中导致意外的结果。
无论出于任何原因更改文本,都会引发 Visual Basic 2008TextChanged 事件,例如:
修改文本输入部分。
选择列表项。
以编程方式修改列表项。
调用 Add 方法。
下面的示例对行为差异进行说明。
' 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 Basic
Private Sub Form1_Load()
' Raises the TextChanged event.
ComboBox1.Items.Add("A")
End Sub
Private Sub 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")
End Sub
下一步做什么
- 在 TextChanged 事件过程中设置一个断点,然后运行代码以确定该事件在何处引发。必要时修改代码。