vb.net 如何在列表框或列表视图中添加组合框项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15777393/
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
how to add combobox items in listbox or listview
提问by Juan Filipe
I want to know what control to be use on my project is it Listview or listbox???. I have a comboBox control on my project what I want to do is when I selected 1 item on my combobox it will automatically add on listbox or listview and when I selected more than 1 item I want to add it on listbox or listview on newline...
我想知道在我的项目中使用什么控件是 Listview 还是列表框???。我的项目中有一个组合框控件,我想要做的是,当我在组合框中选择 1 个项目时,它会自动添加到列表框或列表视图中,当我选择超过 1 个项目时,我想将其添加到列表框或列表视图上的换行符。 ..
Is it simple, please help me to do that in listbox or listview..thanks!
是否简单,请帮我在列表框或列表视图中做到这一点..谢谢!
采纳答案by Kasnady
Listbox > Is for viewing too but user can select it
Listview > Is for viewing only, user cannot select also it viewing by five view directly cause it's for viewing only
If your project wanted the list to be viewing from what have been select by Combobox, then you just pick List View, but if you want for viewing also user can select it, better use listbox, so it's up to you. Also you can know how the Tools work by focus your mouse cursor to the tool, then it will pop up an tooltip that write what the tool for.
如果您的项目希望从组合框选择的内容中查看列表,那么您只需选择列表视图,但如果您想查看也用户可以选择它,最好使用列表框,所以这取决于您。您也可以通过将鼠标光标放在工具上来了解工具的工作原理,然后它会弹出一个工具提示,上面写着该工具的用途。
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
ListView1.Items.Add(ComboBox1.SelectedIndex)
End Sub
That is the code to viewing in listview for what you select in combobox
这是在列表视图中查看您在组合框中选择的内容的代码
For clearing all the item in the Listview or listbox, just write to your form_load
要清除列表视图或列表框中的所有项目,只需写入您的 form_load
Listview.items.clear
Why i said in form load, cause the list just for viewing, of course every time the form begin to run, it will need blank list, so the best is put in form load
为什么我在表单加载中说,导致列表只是为了查看,当然每次表单开始运行时,它都会需要空白列表,所以最好放在表单加载中
UPDATE
更新
To remove the selected index in listbox
删除列表框中选定的索引
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
回答by Cherokee215
ListView Items can be selected a couple of ways In the Properties window for the ListBox the activation property allows an item to be activated by one click or two clicks. Here is an example of how the selected items can be used
可以通过多种方式选择 ListView 项目 在 ListBox 的“属性”窗口中,激活属性允许通过单击或两次单击来激活项目。以下是如何使用所选项目的示例
If Me.ListView1.SelectedItems.Count = (1) Then
'Declare the selected item
Dim lviSelectedItem As ListViewItem = Me.listView1.SelectedItems(0)
'Now you can use it
lblLabel1.Text = lviSelecetedItem.Text
Else
lblLabel2.Text = "You can make Items selectable by switching CheckBoxes property to True in the Properties windows and using CheckBox event handlers"
End If

