C# winform中的列表框选定项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1420514/
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 items in winform
提问by Surya sasidhar
I have listbox, button, and textbox controls in a Windows application. How can I display multiple selected values in a textbox.
我在 Windows 应用程序中有列表框、按钮和文本框控件。如何在文本框中显示多个选定的值。
this is my code
这是我的代码
textBox1.Text = listBox1.SelectedItems.ToString();
but it display in textbox like this: (I select more than one item)
但它显示在这样的文本框中:(我选择了多个项目)
System.Windows.Forms.ListBox+Selec.
please help me
请帮我
采纳答案by Robban
You could do something like:
你可以这样做:
string text = "";
foreach (System.Data.DataRowView item in listBox1.SelectedItems) {
text += item.Row.Field<String>(0) + ", ";
}
textBox1.Text = text;
回答by JDunkerley
You need to iterate over the collection of items. Something like:
您需要遍历项目集合。就像是:
textBox1.Text = "";
foreach (object o in listBox1.SelectedItems)
textBox1.Text += (textBox1.Text == "" ? "" :", ") + o.ToString();
回答by rahul
ListBox.SelectedItems: Returns a collection of the currently selected items.
ListBox.SelectedItems:返回当前所选项目的集合。
Loop through the SelectedItems collection of the listbox.
循环遍历列表框的 SelectedItems 集合。
foreach (ListItem liItem in ListBox1.SelectedItems)
{
// write your code.
}
回答by Xiaohuan ZHOU
Actrually, if you know the type of object you input into ListBox, the selected item is that type, here is a sample: Input List of FileInfo to ListBox:
实际上,如果您知道输入到 ListBox 中的对象类型,则所选项目就是该类型,这里是一个示例:将 FileInfo 的列表输入到 ListBox:
FileInfo[] lFInfo = new DirectoryInfo(textBox1.Text).GetFiles();
foreach (var i in lFInfo)
lstFile.Items.Add(i);
A copy function to copy the selected files to path of textBox2.Text:
将选定文件复制到 textBox2.Text 路径的复制功能:
private void btnCopy_Click(object sender, EventArgs e)
{
foreach (FileInfo i in lstFile.SelectedItems)
File.Copy(i.FullName, Path.Combine(textBox2.Text, i.Name));
}
回答by Reza Aghaei
The post is quite old but lacks a correct general answer which can work regardless of the data-bound item type for example for
List<T>
,DataTable
, or can work regardless of setting or not settingDisplayMember
.
该帖子很旧,但缺乏正确的通用答案,无论数据绑定项目类型如何,例如 for
List<T>
,DataTable
都可以工作,或者无论设置或不设置都可以工作DisplayMember
。
The correct way to get text of an item in a ListBox
or a ComboBox
is using GetItemText
method.
在 aListBox
或 a 中获取项目文本的正确方法ComboBox
是使用GetItemText
方法。
It doesn't matter what is the type of item, if you have used DataSource
and DisplayMember
it uses DisplayMember
to return text, otherwise it uses ToString
method of item.
项目的类型是什么并不重要,如果您使用过DataSource
并且DisplayMember
它用于DisplayMember
返回文本,否则它使用ToString
项目的方法。
For example, to get a comma-separated list of selected item texts:
例如,要获取以逗号分隔的所选项目文本列表:
var texts = this.listBox1.SelectedItems.Cast<object>()
.Select(x => this.listBox1.GetItemText(x));
MessageBox.Show(string.Join(",", texts));
Note:For those who are looking for selected item values rather that selected item texts regardless of the item type and the value member field, they use GetItemValue
extension method.
注意:对于那些正在寻找选定项目值而不是选定项目文本的人,无论项目类型和值成员字段如何,他们都使用GetItemValue
扩展方法。