2014-02-09 26 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