2017-01-05 48 views
0

值是否有任何其他的方式來總結在組合框中的所有項目如何添加從組合框

我試圖總結組合框

這是我的代碼的所有值:

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

打開選項嚴格。 'b = ComboBox1.Items(a)'將Object轉換爲整數。它們不是一回事 – Plutonix

+0

ComboBox項目集合中沒有Count(x)方法。此代碼不可編譯。 – Steve

+0

你使用WinForms組合框或其他東西嗎? – Steve

回答

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