joeb
joeb

Reputation: 877

cannot get selectedvalue of combobox, returns empty

I'm sure this is really something stupid in my code, but I cannot get the selected value from my combobox for the life of me. Here is my code.

        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

The second from the last line MsgBox(_ScaleForm.cmbScale.SelectedValue), this is where I want to use the selected value to do stuff but it keeps popping up empty in the messagebox. I'm tired and unsure of why it's not working.

Upvotes: 2

Views: 3012

Answers (1)

Steve
Steve

Reputation: 216358

You are not setting the DataSource property of the ComboBox but inserting every item one by one in the items collection. Try to set the DataSource

 _ScaleForm.cmbScale.DataSource = objScales

and you will get the SelectedValue set.
In alternative you could read the SelectedItem property that will return a Scale object if something has been selected and then get the DimScale field from this instance

    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

Upvotes: 2

Related Questions