vb.net - 无法获取组合框的选定值,返回空

标签 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),这是我想使用所选值来做事情的地方,但它在消息框中一直弹出空。我很累并且不确定为什么它不起作用。

最佳答案

您不是设置 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/

相关文章:

vb.net - 为什么 Visual Studio 不调试我的 VB.NET 应用程序?

java - SWT 组合中的空条目

swift - 当选择一行时,tableView didSelectRowAt 未在 SearchTextField 中调用

mysql - vb .NET ComboBox 从保存数据集中的值的字典填充

wpf - 如何将 "list"字符串绑定(bind)到 WPF 中的 ComboBox?

vb.net - 高速环形缓冲器

vb.net - .NET 中的 IsNumeric() 占用了这么长时间的原因是什么?

mysql - VB - MySql 从数据库中选择

vb.net - For Each 循环在 List(Of KeyValuePair(Of String, Integer)) 数组上

c# - 如何从 PropertyChanged 事件中正确重置与 ComboBox 关联的值?