在VB.Net中将列表框中的数据链接到标签
请帮帮我。我试图做的是将所选项目从列表框转移到标签中。但每次我从列表框中单击一个项目时,它都不起作用。 这是代码在VB.Net中将列表框中的数据链接到标签,vb.net,winforms,listbox,label,Vb.net,Winforms,Listbox,Label,请帮帮我。我试图做的是将所选项目从列表框转移到标签中。但每次我从列表框中单击一个项目时,它都不起作用。 这是代码 Private Sub lbClients_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbClients.SelectedIndexChanged For i = 0 To lbClients.SelectedItems.Count -1
Private Sub lbClients_SelectedIndexChanged(sender As Object, e As EventArgs)
Handles lbClients.SelectedIndexChanged
For i = 0 To lbClients.SelectedItems.Count -1
Label1.Text &= lbClients.SelectedItems.Item(i).ToString() & " "
Next
End Sub
我已经包括了一张错误的照片
请帮帮我。我知道这并不难。但我一直在寻找答案,结果还是这样。对不起,我刚接触vb.net。谢谢你
这是我用来填充列表框的代码
Private Sub ListBoxClients()
Dim connection As New SqlConnection("Data Source=EURIZZE-PC;Initial Catalog=INTERTRANS;Integrated Security=True")
Dim SQLDA As New SqlDataAdapter("Select * FROM CLIENTS", connection)
Dim dt As New DataTable
connection.Open()
SQLDA.Fill(dt)
lbClients.DataSource = dt
lbClients.ValueMember = "User_"
lbClients.DisplayMember = "Clientname"
connection.Close()
End Sub
ListBox.SelectedItems返回当前选定项的集合 循环浏览列表框的SelectedItems集合
Private Sub ListBoxClients()
Dim connection As New SqlConnection("Data Source=EURIZZE-PC;Initial Catalog=INTERTRANS;Integrated Security=True")
Dim SQLDA As New SqlDataAdapter("Select * FROM CLIENTS", connection)
Dim dt As New DataTable
connection.Open()
SQLDA.Fill(dt)
lbClients.DataSource = dt
lbClients.ValueMember = "User_"
lbClients.DisplayMember = "Clientname"
connection.Close()
End Sub
例如,代码:
Dim text As String = ""
For Each item As System.Data.DataRowView In lbClients.SelectedItems
text &= item.Row.Field(Of String)(0) & ", "
Next
Label1.Text = text
ListBox.SelectedItems返回当前选定项的集合 循环浏览列表框的SelectedItems集合
Private Sub ListBoxClients()
Dim connection As New SqlConnection("Data Source=EURIZZE-PC;Initial Catalog=INTERTRANS;Integrated Security=True")
Dim SQLDA As New SqlDataAdapter("Select * FROM CLIENTS", connection)
Dim dt As New DataTable
connection.Open()
SQLDA.Fill(dt)
lbClients.DataSource = dt
lbClients.ValueMember = "User_"
lbClients.DisplayMember = "Clientname"
connection.Close()
End Sub
例如,代码:
Dim text As String = ""
For Each item As System.Data.DataRowView In lbClients.SelectedItems
text &= item.Row.Field(Of String)(0) & ", "
Next
Label1.Text = text
显然,您的列表框被绑定到DataTable,并使用
DisplayMember
显示项目。在这种情况下,项目的类型为
DataRowView
-这就是为什么您在标签中获得类型名称,您需要指定将使用哪列作为标签中所选项目的文本。DataRowView具有属性
.Row
,该属性返回底层DataRow
对象。使用
lbClients.DisplayMember的值必须显示与列表框中的项目相同的文本
Dim文本作为新的StringBuilder()
对于lbClients.SelectedItems中作为DataRowView的每个项目
text.Append(item.Row.Field(由字符串组成)(lbClients.DisplayMember))
text.Append(“”)
结束
Label1.Text=Text.ToString()
使用StringBuilder
在循环中创建字符串将更加有效。显然,您的列表框被绑定到数据表,并使用DisplayMember
显示项目。
在这种情况下,项目的类型为DataRowView
-这就是为什么您在标签中获得类型名称,您需要指定将使用哪列作为标签中所选项目的文本。
DataRowView具有属性.Row
,该属性返回底层DataRow
对象。
使用lbClients.DisplayMember的值必须显示与列表框中的项目相同的文本
Dim文本作为新的StringBuilder()
对于lbClients.SelectedItems中作为DataRowView的每个项目
text.Append(item.Row.Field(由字符串组成)(lbClients.DisplayMember))
text.Append(“”)
结束
Label1.Text=Text.ToString()
使用StringBuilder
在循环中创建字符串将更加有效,您的列表框
包含类型为DataRowView
的项。你能告诉我列表框是如何填写的吗lbClients.SelectedItems.Item(i).ToString()
将执行DataRowView.ToString()
@Fabio我已编辑了我的帖子,并根据您的要求放置了代码:(您可以使用方法获取指定项的文本表示形式。这样,您就不需要知道任何有关列表框的数据源或显示成员的信息,列表框会回答您的请求。您的ListBox
包含DataRowView
类型的项。您可以显示一个代码,说明ListBox
是如何填充的吗?lbCl.)ients.SelectedItems.Item(i).ToString()
将执行DataRowView.ToString()
@Fabio我已编辑了我的帖子,并根据您的要求放置了代码:(您可以使用方法获取指定项的文本表示形式。这样,您就不需要知道有关列表框的数据源或显示成员的任何信息,列表框会回答您的请求。您好。感谢您的回答。但我遇到了一个错误。它显示:“其他信息:无法将类型为'System.Int32'的对象强制转换为类型为'System.String'。”这是什么意思?:(这意味着您试图强制转换的列包含类型Integer
。请检查DisplayMember
的值,它必须包含列的名称Clientname
。或者使用item.Row(lbClient.DisplayMember)。ToString。”()
-然后您将看到右边的列用作textHi。感谢您的回答。但我遇到了一个错误。它说:“其他信息:无法将'System.Int32'类型的对象强制转换为'System.String'。”这是什么意思(这意味着您试图强制转换的列包含类型Integer
。检查DisplayMember
的值,它必须包含列的名称Clientname
。或者使用item.Row(lbClients.DisplayMember).ToString()
-然后您将看到右列用作文本