无法获得组合框的选择值,返回空

时间:2014-02-09 22:04:39

标签: vb.net combobox

我确信这在我的代码中确实是愚蠢的,但是我无法从我的组合框中获取所选的值。这是我的代码。

        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)开始的第二行,这是我想要使用所选值来执行操作的地方,但它会在消息框中弹出空白。我很累,不确定为什么它不起作用。

1 个答案:

答案 0 :(得分:2)

您没有设置ComboBox的DataSource属性,而是在items集合中逐个插入每个项目。尝试设置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
相关问题