我试图在组合框中的值发生变化时弹出MessageBox,相反,它会在加载时弹出,然后在值发生变化时弹出。不知道我在这里做错了什么。
Public Class DropDownBox
Private Sub DropDownBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dropSource As New Dictionary(Of String, String)()
dropSource.Add("", "")
dropSource.Add("1", "1")
dropSource.Add("2", "2")
dropSource.Add("3", "3")
dropSource.Add("4", "4")
dropSource.Add("5", "5")
dropSource.Add("6", "6")
dropSource.Add("7", "7")
dropSource.Add("8", "8")
dropSource.Add("9", "9")
dropSource.Add("10", "10")
cbox.DataSource = New BindingSource(dropSource, Nothing)
cbox.DisplayMember = "Value"
cbox.ValueMember = "Key"
cbox.Text = Nothing
End Sub
Private Sub cbox_TextChanged(sender As Object, e As EventArgs) Handles cbox.TextChanged
If cbox.Text IsNot Nothing Then
MsgBox("Are you sure?")
Else
End If
End Sub
End Class
感谢您的帮助。
如果您需要有关此主题的任何其他信息,请告诉我,我已经搜遍了所有内容并且无法解决这个问题。
答案 0 :(得分:3)
事件Combobox.SelectionChangeCommitted
完全符合您的要求,无需额外的解决方法。
SelectionChangeCommitted事件仅在用户时引发 更改组合框选择
ComboBox.SelectionChangeCommitted Event
Private Sub cbox_SelectionChangeCommitted(sender As Object, e As EventArgs)
Handles cbox.SelectionChangeCommitted
Dim combobox = DirectCast(sender, ComboBox)
If combobox.Text IsNot Nothing Then
MsgBox("Are you sure?")
End If
End Sub
答案 1 :(得分:2)
我喜欢Youssef的答案,并且当我有多个对象(例如控件数组)共享同一个事件处理程序时使用它。缺点是您无法在编辑器顶部的组合框中识别对象事件中的事件处理程序,也不会在代码中看到标识为处理程序的例程。
我喜欢这种方法:
Private Sub cbox_TextChanged(sender As Object, e As EventArgs) Handles cbox.TextChanged
If Not Me.IsHandleCreated Then Return
If cbox.Text IsNot Nothing Then
MsgBox("Are you sure?")
Else
End If
End Sub
您也可以使用cbox.IsHandleCreated
答案 2 :(得分:1)
我认为价值在装载表格上已经发生了变化。
无论如何,这是我的想法
首先,删除事件处理程序,这一行
Private Sub cbox_TextChanged(sender As Object, e As EventArgs) Handles cbox.TextChanged
应该是
Private Sub cbox_TextChanged(sender As Object, e As EventArgs)
其次,在此行的form_load末尾再次添加处理程序
AddHandler cbox.TextChanged, AddressOf cbox_TextChanged