vb.net 列表框和项目值 - Visual Basic 2010
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15117517/
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 and Item Values - Visual Basic 2010
提问by user2088174
I am writing a small application using VISUAL BASIC 2010 (It came packaged in Visual Studio 2010).
我正在使用 VISUAL BASIC 2010(它打包在 Visual Studio 2010 中)编写一个小应用程序。
I have a list box set up to be populated by an Access database. The database has two columns: 1) Variable Name 2) Description.
我有一个列表框设置为由 Access 数据库填充。数据库有两列:1) 变量名 2) 描述。
As an example to be used in my question, here is a sample of my data:
作为在我的问题中使用的示例,以下是我的数据示例:
Variable Name Description DOG1 Dog Species that came from Family 1
变量名称 描述 DOG1 来自家庭 1 的犬种
Now, I have a list box that is populated by the Description column. This is what I need. I want to click a button and then depending on which item is clicked, display its corresponding "Description" in a textbox. This pertains to I believe DisplayMember and ValueMember. I've succeeded in doing this for one item at a time. My question is, how do I use a loop to do the same task for several items that might be selected in the list Box?
现在,我有一个由“描述”列填充的列表框。这就是我需要的。我想单击一个按钮,然后根据单击的项目,在文本框中显示其相应的“说明”。这与我相信 DisplayMember 和 ValueMember 有关。我一次成功地为一个项目做到了这一点。我的问题是,如何使用循环为可能在列表框中选择的多个项目执行相同的任务?
Here is my Code that works 100% for displaying items clicked one at a time.
这是我的代码,它可以 100% 地显示一次单击一个的项目。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = ListBox1.SelectedValue
End Sub
I thought that the following would work, but it doesn't and I'm not too sure why.
我认为以下内容会起作用,但它不会,我不太确定为什么。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To ListBox1.SelectedValue - 1
TextBox1.Text = ListBox1.SelectedValue(i)
Next i
End sub
Also, if it is possible, I'd like to take the items selected in listbox1 and display their values in another listbox. I've done this before with simple listboxes I've populated myself, but now that I'm working with value and display members, I can't seem to adapt the code:
另外,如果可能的话,我想获取在 listbox1 中选择的项目并在另一个列表框中显示它们的值。我以前用自己填充过的简单列表框完成了这项工作,但是现在我正在处理值和显示成员,我似乎无法调整代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To Listbox1.SelectedItems.Count - 1
Listbox2.Items.Add(Listbox1.SelectedItems.Item(i))
Next i
Thanks so much for the help in advance!
非常感谢您的帮助!
回答by Jonny
I think you need -
我想你需要——
For i = 0 To ListBox1.SelectedValue - 1
TextBox1.Text &= ListBox1.SelectedValue(i)
Next i
See the "&".
见“&”。
Haven't had a chance to double check yet.
还没有机会仔细检查。
回答by chris_techno25
This should be what you need. Try it. Don't forget to change the Listbox's SelectionMode property to Multisimple.
这应该是你需要的。尝试一下。不要忘记将 Listbox 的 SelectionMode 属性更改为 Multisimple。
TextBox1.Clear()
Listbox2.Items.Clear()
For x As Integer = 0 To ListBox1.SelectedItems.Count - 1
TextBox1.Text += ListBox1.SelectedItems(x).ToString + " "
ListBox2.Items.Add(ListBox1.SelectedItems(x).ToString)
Next x
回答by dran1979
try this to View from Listbox1
to Listbox2
:
试试这个从Listbox1
到查看Listbox2
:
ListBox1.Items.Add("Dog, Cat, Fish, Bear, Bird, Goat")
Dim myArr() As String = ListBox1.Items.OfType(Of String)().ToArray()
ListBox2.DataSource = myArr