4
如何獲取列表框中所有選定項目的值(不顯示文本)?WinForm:從列表框中的選定項目獲取值
我的意圖是使用值(它代表我的數據庫中的主鍵和外鍵)組裝sql查詢。
規格:使用的WinForm有一個.NET Framework 4節
如何獲取列表框中所有選定項目的值(不顯示文本)?WinForm:從列表框中的選定項目獲取值
我的意圖是使用值(它代表我的數據庫中的主鍵和外鍵)組裝sql查詢。
規格:使用的WinForm有一個.NET Framework 4節
您也可以使用您在列表框中喜歡的任何對象。下面的小例子,但要測試你必須創建一個帶有ListBox和按鈕的表單。 與字典相同的想法,但這將適用於更復雜的對象。
Public Class Form1
Dim tests As New List(Of Test)
Class Test
Private _Key As Integer
Public Property Key() As Integer
Get
Return _Key
End Get
Set(ByVal value As Integer)
_Key = value
End Set
End Property
Private _value As String
Public Property Value() As String
Get
Return _value
End Get
Set(ByVal value As String)
_value = value
End Set
End Property
End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With tests
.Add(New Test With {.Key = 1, .Value = "Val1"})
.Add(New Test With {.Key = 2, .Value = "Val2"})
.Add(New Test With {.Key = 3, .Value = "Val3"})
End With
ListBox1.SelectionMode = SelectionMode.MultiSimple
ListBox1.DisplayMember = "Value"
For Each t In tests
ListBox1.Items.Add(t)
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each t As Test In ListBox1.SelectedItems
Debug.WriteLine(t.Key)
Next
End Sub
End Class