Vb.net 组合框的选定值不适用于DirectCast或CType
我试图清除表单上组合框的选择,但是当将Vb.net 组合框的选定值不适用于DirectCast或CType,vb.net,combobox,controls,Vb.net,Combobox,Controls,我试图清除表单上组合框的选择,但是当将.SelectedValue属性设置为-1时,我得到的是第一个值 为了解释headerControls和controlsList,我在运行时从一个配置文件创建了所有控件,加载到controlsList中,当我将控件添加到屏幕时,我也将其添加到headerControls Public headerControls As New Dictionary(Of String, Control) Try For each dynamicControl As
.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
也许您应该检查SelectedValue
和SelectedIndex
之间的差异。将SelectedValue
设置为值可能无法选择任何项目。如果您不想选择任何项目,那么您可以将SelectedIndex
设置为-1,SelectedItem
设置为Nothing
,或者,根据您的绑定情况,SelectedValue
设置为Nothing我尝试了SelectedItem
并收到了相同的结果,但如果我做了两次,就会清除选择。。。不太清楚为什么