还有其他方法可以对组合框中的所有项目进行求和
我试图在组合框上加总所有值
这是我的代码:
For a As Integer = 0 To ComboBox1.Items.Count - 1
Dim b As Integer
b = ComboBox1.Items(a)
MetroLabel12.Text = ComboBox1.Items.Count(0) + b
Next b
答案 0 :(得分:1)
以下代码将获取每个项目的字符串值,并尝试将其转换为整数。如果成功,则会将结果添加到result
。
Dim result as Integer = 0
Dim num as Integer = 0
For Each s As String In ComboBox1.Items
num = 0
If Integer.TryParse(s, num) Then
result = result + num;
End If
Next s