2013-10-17 74 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