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 SubPrivate Sub CombBox1_SelectedIndexChanged _(ByVal sender As System.Object, ByVal e As System.EventArgs) _Handles CombBox1.SelectedIndexChanged 'Event code 'Do some code here ... fires off prematurelyEnd Sub
There is a way to avoid this premature code firing?without 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