我确信这在我的代码中确实是愚蠢的,但我一生都无法从组合框中获取所选值。这是我的代码。
Dim objScales As List(Of My.Scale) = Nothing
Dim ExistingDimScale As Double = 0
Dim ExistingDimScaleIndex As Double = 0
_ScaleForm = New ScaleForm
Try
Me.LoadProperties()
If Me.ConfigUnits <> 0 Then
'Get the right scales per units
If Me.ConfigUnits = 1 Then 'imperial
objScales = Me.GetImperialScales()
Else
objScales = Me.GetMetricScales()
End If
'Load up the combobox values
If objScales IsNot Nothing Then
_ScaleForm.cmbScale.DisplayMember = "Name"
_ScaleForm.cmbScale.ValueMember = "DimScale"
For Each objScale In objScales
_ScaleForm.cmbScale.Items.Add(objScale)
'MsgBox(objScale.Name.ToString)
Next
'Set the selected Index to the current dim scale
Double.TryParse(Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("Dimscale").ToString, ExistingDimScale)
ExistingDimScaleIndex = objScales.FindIndex(Function(Val) Val.DimScale = ExistingDimScale)
If ExistingDimScaleIndex = -1 Then
_ScaleForm.cmbScale.SelectedIndex = 0
Else
Integer.TryParse(ExistingDimScaleIndex.ToString, _ScaleForm.cmbScale.SelectedIndex)
End If
Else
MsgBox("There were no scales set")
End If
Else
Throw New System.Exception("Error Reading Configuration Units")
End If
Catch ex As System.Exception
MsgBox(ex.Message)
'handle it here internally
End Try
_ScaleForm.ShowDialog()
If DialogResult.OK = 1 Then
MsgBox(_ScaleForm.cmbScale.SelectedValue)
End If
最后一行的第二个MsgBox(_ScaleForm.cmbScale.SelectedValue)
,这是我想使用所选值来做事情的地方,但它在消息框中一直弹出空。我很累并且不确定为什么它不起作用。
最佳答案
您不是设置 ComboBox 的 DataSource 属性,而是将每个项目逐一插入项目集合中。尝试设置数据源
_ScaleForm.cmbScale.DataSource = objScales
您将获得SelectedValue
集。
或者,您可以读取 SelectedItem 属性,如果已选择某些内容,该属性将返回 Scale 对象,然后从此实例获取 DimScale 字段
if DialogResult.OK = _ScaleForm.ShowDialog() Then
if _ScaleForm.cmbScale.SelectedItem IsNot Nothing Then
My.Scale obj = CType(_ScaleForm.cmbScale.SelectedItem, My.Scale)
....
End If
End If
关于vb.net - 无法获取组合框的选定值,返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21665671/