我正在创建一个动态组合框并添加到表单中。我正在尝试使用ArrayList中的DataSource填充组合框,然后根据属性中的值选择组合框中的项目。
问题是,在Form_Load事件完成并且表单可见之后,组合框项才会被绑定。因此,当我尝试设置组合框的选定索引时,组合框为空。请参阅代码,了解我正在做的详细信息,并参考代码中的注释:
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)
如何在没有黑客的情况下为组合框选择正确的选择索引(加载计时器或类似的愚蠢事件)?