vb.net中从列表框到列表变量的值

vb.net中从列表框到列表变量的值,vb.net,Vb.net,我正在使用vb.net 我有一个列表框名称LSTlocations..我可以从该列表框中选择多个位置..我正在将特定位置的id提取到一个列表变量..因此我给出了如下代码: cnt = LSTlocations.SelectedItems.Count Dim strname As String If cnt > 0 Then For i = 0 To cnt - 1 Dim locationanme As String = LSTl

我正在使用vb.net 我有一个列表框名称LSTlocations..我可以从该列表框中选择多个位置..我正在将特定位置的id提取到一个列表变量..因此我给出了如下代码:

 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

但是我没有在我的列表变量中获取我的所有选定位置id。如何在选定项目上循环时,从我的列表框获取所有选定位置id到列表变量

,初始化应存储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)

在所选项目上循环时,初始化应存储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)

很难说。启动循环时,cnt的值是多少?您可以尝试添加一个显示LSTlocation的值的消息框。SelectedItems(i)和locidfirst time它自己的值我想显示所有选定的Id…用逗号分隔,,我怎么做?很难说。启动循环时,cnt的值是多少?您可以尝试添加一个MessageBox,其中显示LSTlocation的值。SelectedItems(i)和LocadFirst time的值。我要显示所有选定的Id…以逗号分隔,,如何执行此操作?