Hi all,
I'm creating a dynamic combo box and adding to a form. I'm trying to fill the combo box with a DataSource from an ArrayList and then selecting an item in the combo box based off a value from a property.
Problem is, the combo box items don't get bound until after the Form_Load event has finished and the form is visible. So the combo box is empty when I try to set the selected index of the combo box. See code for what I'm doing in detail, and refer to comments in code:
Dim cboValues As New ComboBox
cboValues.Width = fieldControlWidth
cboValues.DropDownStyle = ComboBoxStyle.DropDownList
cboValues.Name = "cboResult"
For Each d As SystemTaskResult In [Enum].GetValues(GetType(SystemTaskResult))
Dim cv As New ComboBoxDisplayValue(d.ToString, d)
arrValues.Add(cv)
Next
cboValues.DataSource = arrValues
cboValues.DisplayMember = "Display"
cboValues.ValueMember = "Value"
Dim val As SystemTaskResult = DirectCast(p.GetValue(Me.Task, Nothing), SystemTaskResult)
'Was trying to get this to work, but commented out to try the below
'cboValues.SelectedIndex = cboValues.Items.IndexOf(New ComboBoxDisplayValue(val.ToString, val))
'Then this doesn't work because the combo box hasn't updated it's DataSource yet, which is probably the reason for the above not working as well.
For i = 0 To cboValues.Items.Count - 1
cboValues.SelectedIndex = i
If cboValues.SelectedValue = val Then
Exit For
End If
Next
holdPanel.Controls.Add(cboValues)
How to select the right selected index for combo box without a hack (Load timer or something stupid like that)?