2015-09-16 69 views
0

變量我有這樣的代碼:循環組合框和內部

Dim val1,val2,val3,val4,val5, ... ,val20 As String 

If Combobox1.SelectedIndex = 0 Then 
    val1=">" 
else 
    val1="<" 
end if 

If Combobox2.SelectedIndex = 0 Then 
    val2=">" 
else 
    val2="<" 
end if 

我怎麼能循環呢?有20個組合框。請幫忙!謝謝

+0

您可以縮短你的代碼'VAL1 = IF(Combobox1.SelectedIndex = 0, 「>」, 「<」)' –

+1

是VAL1 ,val2,val3等在方法中聲明爲** locals ** ...或者它們是類級變量嗎?使用組合框**名稱**作爲關鍵字來實現這一點可能會更容易。 –

+0

@Idle_Mind這就是我剛纔所說的(下) –

回答

0

要循環顯示窗體上的控件,請使用Me.Controls。並檢查控制的類型,使用Control.GetType.Name

問題是像val1,val2,val3...etc這樣的單獨變量在循環中不能輕鬆使用,所以我建議將它們更改爲列表。

Dim val As New List(Of String) 

For Each MyControl In Me.Controls 
    If MyControl.GetType.Name = "ComboBox" Then 
     Dim MyComboBox As ComboBox = CType(MyControl, ComboBox) 
     If MyComboBox.SelectedIndex = 0 Then 
      val.Add(">") 
     Else 
      val.Add("<") 
     end if 
    End If 
Next 
+0

他如何將值匹配到組合框?必須使用字典或映射對象列表 –

+0

不一定。這完全取決於我們不知道的他的設計。他原本有'val1,val2 ... etc',映射到'ComboBox1,ComboBox2 ... etc',所以我認爲索引映射就足夠了。如果他的表單上按順序具有組合框,則列表的索引足以進行映射,並且不需要字典。這樣'val(0)'將被映射到'ComboBox1'並且'val(1)'將被映射到'ComboBox2' ....等等。 –

+0

是...並且...不是 –

0

所以,這裏有一些循環序列給你。免責聲明 - 代碼沒有進行測試,可能是錯誤,但邏輯佈局應該是正確的

Private _numOfControls As Integer = 20 
Private _variables As New Dictionary(Of String, String)(_numOfControls) 

Private Sub Setup() 

    For i as integer = 1 To _numOfControls 

     Dim cboName As String = "cbo" & i 
     Dim cbo As New ComboBox() 
     cbo.Name = cboName 
     cbo.SelectedIndex = 0 
     ' Add cbo location according to your logic here 
     container.Controls.Add(cbo) ' container - any control or form that hosts cbo 

     _variables.Add(cboName, ">") 
     AddHandler cbo.SelectedIndexChanged, AddressOf OnCboSelectedIndexChanged 
    Next 
End Sub 

' If you do this way, your control-related variable will have appropriate equality sign without need for iteration 
Private Sub OnCboSelectedIndexChanged (sender As Object, e As EventArgs) 

    Dim cbo As ComboBox = CType(sender, ComboBox) 
    myDict(cbo.Name) = If(cbo.SelectedIndex = 0, ">", "<") 

End Sub 

' But if you do not want to do in the above^^ way, you can iterate dictionary 
Private Sub FromDictionaryToCbo() 
    ' Here you can't use for-loop on dictionary because your dictionary will be mutating. So lets iterate keys 
    For Each k As String In _variables.Keys.ToArray()) 

     _variables(k) = If(CType(container.Find(k, True), ComboBox).SelectedIndex = 0, ">", "<") 

    Next 
End Sub 

' And iterating combo boxes would be something similar as another answer here 
' But we use our naming convention to bypass other controls 
Private Sub FromCboToDictionary() 

    Dim c As Control 
    For i As Integer = 1 To < _numOfControls 
     Dim key As String = "cbo" & i.ToString() 

     'We don't know, may be there some other combo boxes are there, so we use our naming convention instead of control type 
     c = container.Find(key, True) 

     If c IsNot Nothing Then 

      _variables(key) = If(CType(c, ComboBox).SelectedIndex = 0, ">", "<")  
     end If  
    Next 

End Sub