1

私はvb.netを使用していますリストボックス名LSTlocationsを持っています..そのリストボックスから複数の場所を選択できます..特定の場所のIDを1つのリスト変数にフェッチしています..だから私は次のようなコードを与えました:

 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を取得して変数をリストする方法

4

1 に答える 1

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)
于 2013-10-17T15:49:45.760 に答える