Say you have a combobox being loaded at form initialization from a database and you have code in the combobox's
SelectedIndexChanged event. This may cause unwanted exceptions:
Private Sub LoadComboBox()
'Connection and SQL code
'...
ComboBox1.ValueMember = "ID"
ComboBox1.DisplayMember = "Customer"
ComboBox1.DataSource = ds.Tables("Customers")
'Clean up code
'...
End Sub
Private Sub CombBox1_SelectedIndexChanged _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles CombBox1.SelectedIndexChanged
'Event code
'Do some code here ... fires off prematurely
End Sub
There is a way to avoid this premature code firingwithout having to do any custom handling.
The combobox's Created property can determine if the control's creation process (loading) is done yet. The only problem is, the Created Property doesn't show up in the Intellisense List!
Private Sub CombBox1_SelectedIndexChanged _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles CombBox1.SelectedIndexChanged
'Prevents premature execution of event code
If Me.ComboBox1.Created = True Then
'Event code
'Do some code here
End If
End Sub