问题描述
为什么私有子combobox_SelectedIndexChanged(作为对象发送,作为EventArgs发送)处理在表单出现之前调用的combobox.SelectedIndexChanged ?
以我的理解,当用户更改mycombobox的选定索引时,应将此函数称为 ONLY 。我错了吗?
why Private Sub combobox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles combobox.SelectedIndexChanged called before form appears?In my understanding, this function should be called ONLY when user change the selected index of mycombobox? Am I wrong?
如何阻止它自动运行?
推荐答案
您可以使用一个布尔标志来指示何时处理事件是安全的,也可以使用其他语法在加载表单并完成所有初始化之后添加事件处理程序。
You could either use a Boolean flag indicating when it is "safe" to handle the event, or you could use the alternative syntax to add your event handler AFTER the Form is loaded and all the initialization has been done.
为此,您使用 AddHandler
语法:
AddHandler combobox.SelectedIndexChanged, AddressOf combobox_SelectedIndexChanged
希望这会有所帮助
编辑:
使用 AddHandler
语法,必须确保不要将 Handles
子句添加到事件处理程序声明中:
Using the AddHandler
syntax, you must make sure NOT to add the Handles
clause to your event handler declaration:
Private Sub combobox_SelectedIndexChanged(sender As Object, e As EventArgs)
'you event handler code
End Sub
然后,通常在表单的 OnLoad
覆盖的末尾,哟您将使用 AddHandler
:
Then, typically at the end of the Form's OnLoad
override, you'll use the AddHandler
:
Public Class Form1
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
' Initialization code/whatever
AddHandler ComboBox1.SelectedIndexChanged, AddressOf combobox_SelectedIndexChanged
End Sub
Private Sub combobox_SelectedIndexChanged(sender As Object, e As EventArgs)
'Your event handler code
End Sub
End Class
这篇关于为什么在表单加载之前调用ComboBox SelectedIndexChanged?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!