【发布时间】:2018-03-07 04:41:47
【问题描述】:
我试图清除表单上对组合框的选择,但是当将 .SelectedValue 属性设置为 -1 时,我得到的是第一个值。
为了解释headerControls 和controlsList,我在运行时从配置文件创建所有控件,加载到controlsList,当我将控件添加到屏幕时,我还将它添加到headerControls
Public headerControls As New Dictionary(Of String, Control)
Try
For each dynamicControl As KeyValuePair(Of Integer, CustomControl) In controlsList
Select dynamicControl.Value.ControlType.ToLower()
Case "textbox"
DirectCast(headerControls.Item(dynamicControl.Value.ControlName), TextBox).Text = ""
Case "combobox"
DirectCast(headerControls.Item(dynamicControl.Value.ControlName), ComboBox).SelectedValue = -1
End Select
Next
Catch ex As Exception
'Error Handling
End Try
创建控件的代码:
Try
For each dynamicControl As KeyValuePair(Of Integer, CustomControl) In controlsList
Select Case dynamicControl.Value.ControlType.ToLower()
Case "textbox"
Dim textBoxControl As New TextBox
With textBoxControl
'Set Textbox properties here
End With
headerControls.Add(dynamicControl.Value.ControlName, textBoxControl)
Controls.Add(textBoxControl)
Case "combobox"
Dim comboBoxControl As New ComboBox
With comboBoxControl
'Set ComboBox properties here
End With
LoadCombo(comboBoxControl)
headerControls.Add(dynamicControl.Value.ControlName, comboBoxControl)
Controls.Add(comboBoxControl)
End Select
Next
Catch ex As Exception
'Error Handling
End Try
清除文本框有效,但 ComboBox 显示的行为与我将文本框放在屏幕上并使用 .SelectedValue = -1
【问题讨论】: