繁体   English   中英

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

[英]value from listbox to list variable in 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 = 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM