在 Vb.net 中将所选项目列表框到文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25087348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Listbox selected item to textbox in Vb.net
提问by user3900603
Sometimes simple code will make us big confusion. I researched this questions in internet from past few hours.
有时简单的代码会让我们大惑不解。过去几个小时,我在互联网上研究了这个问题。
I have a listbox, data will load from Access table. When i selected an item in listbox, i wanted to show that item in a textbox.
我有一个列表框,数据将从 Access 表加载。当我在列表框中选择一个项目时,我想在文本框中显示该项目。
i have tried some code as below, nothing worked for me:
我已经尝试了一些代码如下,对我没有任何作用:
Private Sub listBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
For i As Integer = 0 To listBox1.SelectedItems.Count - 1
textBox1.Text &= DirectCast(listBox1.SelectedItems(i), DataRowView)(1).ToString & vbCrLf
Next
End Sub
Private Sub listBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
textBox1.Text = listBox1.SelectedItem.Value
End Sub
Here is the code that will give values to listbox from access table
这是将从访问表中为列表框提供值的代码
Dim dataAdapter As New OleDbDataAdapter("select * from eval", connection)
Dim ds As DataSet = New DataSet
dataAdapter.Fill(ds, "eval")
listBox1.DataSource = ds
listBox1.DisplayMember = "eval"
listBox1.ValueMember = "eval.eval"
回答by Eminem
Here is a simple example of what you can do. Lets say you have a list box with 4 items: VB.Net,C#,Java and Python. First, make the listbox SelectionMode = MultiSimple
. If I understood you correctly, you want all the items that have been selected in the listbox to be transfered to a text box. You can do that by wrtining this code:
这是您可以做什么的一个简单示例。假设您有一个包含 4 个项目的列表框:VB.Net、C#、Java 和 Python。首先,制作列表框SelectionMode = MultiSimple
。如果我理解正确,您希望将列表框中已选择的所有项目转移到文本框中。您可以通过编写以下代码来做到这一点:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
TextBox1.Clear()
For Each Item As Object In ListBox1.SelectedItems
TextBox1.AppendText(Item.ToString + Environment.NewLine)
Next
End Sub
Here are the results :
(Sorry for the bad quality)
结果如下:(质量不好请见谅)
回答by Steve
The listBox1.SelectedItem will give you back a System.Data.Datarow, because that is what you put in there. If you want to display the same text in the textbox that is displayed for the listbox, you can simply use this:
listBox1.SelectedItem 会给你一个 System.Data.Datarow,因为那是你放在那里的。如果要在为列表框显示的文本框中显示相同的文本,只需使用以下命令:
Private Sub listBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
textBox1.Text = listBox1.text
End Sub
If you want a value from another field (you do not show your fields so lets pretend there is one called "FirstName") you could do this:
如果您想要来自另一个字段的值(您不显示您的字段,因此假设有一个名为“FirstName”的字段),您可以这样做:
Private Sub listBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
Dim DR as datarow = listBox1.SelectedItem
textBox1.Text = DR("FirstName")
End Sub
回答by Sherif Hamdy
textbox1.Text = listBox1.GetItemText(listBox1.SelectedItem);