我有一个For Each语句,我希望能够从我的组合框中选择某个选项开始。这可能吗?
For Each cbo As ComboBox In s_combos
For Each cov As String In s_coverage
If combo.Name = cbo.Name And combo.SelectedIndex = 0 Then
If txtNewMoney.Text <> "" And b_isSmoker = False Then
CalculateRunningTotal(False, cov, , CKeyValuePair.GetComboBoxSelectedKey(s_applicants(counter), True))
ElseIf txtNewMoney.Text <> "" And b_isSmoker = True Then
CalculateRunningTotal(True, cov, , CKeyValuePair.GetComboBoxSelectedKey(s_applicants(counter), True))
End If
counter += 1
Exit For
End If
Next
If combo.Name = cbo.Name And combo.SelectedIndex = 1 Then
'If this option is selected, start over in For Each
counter -= 1
Dim dec As frmDeclined = New frmDeclined(DirectCast(sender, Control).Name, Me)
dec.ShowDialog()
End If
Next
答案 0 :(得分:2)
您可以使用plain for循环,并将迭代器设置回它的初始值。 你可能最好重组它以提高效率,如果选择了这个选项,第一次有什么点循环吗?
答案 1 :(得分:1)
通常认为从循环内部修改For
循环的索引是不礼貌的。
一种社会可接受的方法是使用While
循环,在循环内增加或重置自变量。
如果您肯定想要使用For
... Each
循环,可以在其外部放置一个While
循环进行迭代,以有效地重置For
。 .. Each
循环。在我能想到的几乎所有情况下,这都会不必要地复杂化,我通常不会推荐它。
答案 2 :(得分:-1)
您可以创建一个标签并将其改为:
GoHere:
For Each cbo As ComboBox In s_combos
For Each cov As String In s_coverage
If combo.Name = cbo.Name And combo.SelectedIndex = 0 Then
If txtNewMoney.Text <> "" And b_isSmoker = False Then
CalculateRunningTotal(False, cov, , CKeyValuePair.GetComboBoxSelectedKey(s_applicants(counter), True))
ElseIf txtNewMoney.Text <> "" And b_isSmoker = True Then
CalculateRunningTotal(True, cov, , CKeyValuePair.GetComboBoxSelectedKey(s_applicants(counter), True))
End If
counter += 1
Exit For
End If
Next
If combo.Name = cbo.Name And combo.SelectedIndex = 1 Then
'If this option is selected, start over in For Each
counter -= 1
Dim dec As frmDeclined = New frmDeclined(DirectCast(sender, Control).Name, Me)
dec.ShowDialog()
Goto GoHere
End If
Next