gpt4 book ai didi

winforms - WinForm : Obtain values from selected items in a List Box

转载 作者:行者123 更新时间:2023-12-01 05:43:52 24 4
gpt4 key购买 nike

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

我的目的是使用这些值(代表我的数据库中的主键和外键)来组合一个 sql 查询。

规范:在 .Net Framework v.4 中使用 WinForm

最佳答案

您还可以在列表框中使用您喜欢的任何对象。下面的小例子,但要进行测试,您必须创建一个带有 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

关于winforms - WinForm : Obtain values from selected items in a List Box,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3576872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com