2013-10-17 70 views
1

我使用vb.net 我有一個列表框名稱LSTlocations..i可以選擇從該列表box..i正在獲取特定位置的ID給一個列表多個位置varibale..so我給出這樣的代碼:值列表變量在vb.net

cnt = LSTlocations.SelectedItems.Count 

    Dim strname As String 
    If cnt > 0 Then 
     For i = 0 To cnt - 1 
      Dim locationanme As String = LSTlocations.SelectedItems(i).ToString 
      Dim locid As Integer = RecordID("Locid", "Location_tbl", "LocName", locationanme) 
      Dim list As New List(Of Integer) 
      list.Add(locid) 

     Next 
    End If 

,但我沒有得到我的所有seclected位置ID在我的名單varibale..how我可以從我的列表框中所有選定位置ID列出varable

回答

1

在選擇的項目上循環時,初始化應存儲ID的整數。
在每個循環的名單是新的,空的,那麼在添加新LOCID但在隨後的循環鬆動了。

所以你在列表中,只有最後一個整數最終

簡單,移動列表的聲明和初始化循環

Dim strname As String 
Dim list As New List(Of Integer) 

cnt = LSTlocations.SelectedItems.Count 
For i = 0 To cnt - 1 
    Dim locationanme As String = LSTlocations.SelectedItems(i).ToString 
    ' for debug 
    ' MessageBox.Show("Counter=" & i & " value=" & locationanme) 
    Dim locid As Integer = RecordID("Locid", "Location_tbl", "LocName", locationanme) 
    ' for debug 
    ' MessageBox.Show("ID=" & locid) 
    list.Add(locid) 
Next 
Console.WriteLine(list.Count) 

For Each num in list 
    MessageBox.Show("LocID:" & num) 
+0

。是的,先生正確 – user2878851

+0

很難說外面。啓動循環時,cnt的值是多少?你可以嘗試添加顯示的LSTlocation.SelectedItems(i)的值一個MessageBox,我想在清單上顯示 – Steve

+0

值,而不是數量的.. 。用逗號分隔,我怎麼能這樣做? – user2878851