列表框在winform中选择的项目
我在Windows应用程序中有列表框,按钮和文本框控件。 如何在文本框中显示多个选定值。
这是我的代码
textBox1.Text = listBox1.SelectedItems.ToString();
但它在文本框中显示如下:(我选择了多个项目)
System.Windows.Forms.ListBox+Selec.
请帮我
你可以这样做:
string text = ""; foreach (System.Data.DataRowView item in listBox1.SelectedItems) { text += item.Row.Field(0) + ", "; } textBox1.Text = text;
您需要迭代项目集合。 就像是:
textBox1.Text = ""; foreach (object o in listBox1.SelectedItems) textBox1.Text += (textBox1.Text == "" ? "" :", ") + o.ToString();
ListBox.SelectedItems :返回当前所选项的集合。
循环遍历列表框的SelectedItems集合。
foreach (ListItem liItem in ListBox1.SelectedItems) { // write your code. }