C# 我想显示列表框中的项目
我想显示列表框中的所有项目,并将其打印为输出。 我怎么做 我确实尝试了listbox1.text,但没有成功。试试这个C# 我想显示列表框中的项目,c#,visual-studio-2012,C#,Visual Studio 2012,我想显示列表框中的所有项目,并将其打印为输出。 我怎么做 我确实尝试了listbox1.text,但没有成功。试试这个 string s = string.Empty; while (listBox1.Items.Count > 0) { s = s + listBox1.Items[0] as string; // do something with s } “s”将保存列表框的所有值成员。您可以使用以下代码: string finalString=""; foreach (
string s = string.Empty;
while (listBox1.Items.Count > 0)
{
s = s + listBox1.Items[0] as string;
// do something with s
}
“s”将保存列表框的所有值成员。您可以使用以下代码:
string finalString="";
foreach (string s in listBox1.Items)
{
finalString = finalString + s;
}
您可以在变量“finalString”中获取listbox的所有成员。尝试将所有项目合并为一个字符串:
string result = string.Join(Environment.NewLine,
listBox1.Items.OfType<Object>());
string result=string.Join(Environment.NewLine,
listBox1.Items.OfType());
唯一的技巧是listBox1.Items
是一个不实现IEnumerable
的集合;因此,为了将集合(转换为IEnumerable
)转换为OfType()
)您可以使用以下方法:
var list = new ListItem[] {
new ListItem("number 01","1"),
new ListItem("number 02","2"),
new ListItem("number 03","3"),
new ListItem("number 04","4"),
new ListItem("number 05","5")
};
lst.Items.AddRange(list);
lst.DataBind();
var strPrint = new StringBuilder(string.Empty);
foreach (ListItem item in lst.Items)
{
strPrint.AppendLine(item.Text);
}
Response.Write(strPrint);
你能把你的代码放在这里吗?你每次都在初始化变量“s”,这样它就不会保存旧的值,只给出最后一个值,而且“s”在循环外是不可访问的。请调查一下是的,我已经做了改变,谢谢!非常感谢,您的代码工作谢谢:)@Jezreel,如果您觉得它有帮助,请将它标记为答案,这样它也会帮助其他人。