Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms WinForm:从列表框中的选定项获取值_Winforms_Listbox - Fatal编程技术网

Winforms WinForm:从列表框中的选定项获取值

Winforms WinForm:从列表框中的选定项获取值,winforms,listbox,Winforms,Listbox,如何获取列表框中所有选定项的值(未显示文本) 我的意图是使用这些值(表示数据库中的主键和外键)来组装sql查询 规范:将WinForm与.Net Framework v.4一起使用,您还可以在列表框中使用您喜欢的任何对象。下面是一个小示例,但要进行测试,您必须创建一个带有列表框和按钮的表单。 与字典的想法相同,但这将适用于更复杂的对象 Public Class Form1 Dim tests As New List(Of Test) Class Test Pr

如何获取列表框中所有选定项的值(未显示文本)

我的意图是使用这些值(表示数据库中的主键和外键)来组装sql查询


规范:将WinForm与.Net Framework v.4一起使用,您还可以在列表框中使用您喜欢的任何对象。下面是一个小示例,但要进行测试,您必须创建一个带有列表框和按钮的表单。 与字典的想法相同,但这将适用于更复杂的对象

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