我有很多组合框,我想在其中添加所有选定的值并显示在标签中。我还创建了一个事件处理程序,监视是否选择了一个组合框,如果检测到选择,它将不断更改标签。然后,我如何根据事件处理程序添加所有值?
Dim cbCtrl = Panel1.Controls.OfType(Of ComboBox)()
For Each ctrl In cbCtrl
AddHandler ctrl.SelectedIndexChanged, AddressOf ctrlSelectedIndexChanged
Next
Private Sub ctrlSelectedIndexChanged(sender As Object, e As EventArgs)
'MsgBox(CType(sender, ComboBox).Text)
For Each ctrl In Panel1.Controls
If (ctrl.GetType() Is GetType(ComboBox)) Then
For i = 0 To 'LastComboBox ???
i = i + ctrl.SelectedItem
label.Text = i
Next
End If
Next
End Sub
答案 0 :(得分:1)
从它的外观来看,你已经在使用'for-each'来获得所有的组合框控制,所以你可以;
Private Sub ctrlSelectedIndexChanged(sender As Object, e As EventArgs)
dim total as integer = 0
For Each ctrl In Panel1.Controls
If (ctrl.GetType() Is GetType(ComboBox)) Then
total += CInt(ctrl.SelectedItem)
End If
Next
label.text = total.tostring
End Sub
但如果您正在处理数字,为什么不使用nummericupdown?