2014-02-09 25 views
1

我确定这在我的代码中确实很愚蠢,但我无法从我的组合框中为我的生活获取选定的值。这是我的代码。无法获取组合框的selectedvalue,返回空

 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)第二,这是我想要使用所选的值做的东西,但它一直在MessageBox弹出空。我很累,不确定它为什么不起作用。

+0

得到DIMSCALE场,你可以发布GetImperialScales()和GetMetricScales()的代码? – bdn02

回答

2

您没有设置ComboBox的DataSource属性,而是在items集合中逐个插入每个项目。尝试设置数据源

_ScaleForm.cmbScale.DataSource = objScales 

并且您将获得SelectedValue集。
在可替代的,你可以读取SelectedItem属性,如果事情已经选定,将返回一个尺度对象,然后从这种情况下

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 
+0

谢谢你的工作。 – joeb