[英]How to print all items from ListBox to a TextBox?
我想顯示從列表框到文本框的所有元素。 我不確定如何執行此操作,我嘗試執行foreach語句,但由於ListBox不包含IEnumerator的原因而無法正常工作。
這個怎么做?
Winforms列表框的Items集合返回對象的Collection類型,因此您可以在每個項目上使用ToString()
來打印其文本值,如下所示:
string text = "";
foreach(var item in yourListBox.Items)
{
text += item.ToString() + "/n"; // /n to print each item on new line or you omit /n to print text on same line
}
yourTextBox.Text = text;
foreach (ListItem liItem in listBox1.Items)
textBox1.Text += liItem.Value + " "; // or .Text
編輯:
由於您使用的是WinForms,因此ListBox.Items
返回一個ObjectCollection
foreach (object liItem in listBox1.Items)
textBox1.Text += liItem.ToString() + " "; // or .Text
嘗試在具有可使用的枚舉器的Listbox.Items ..上運行foreach
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.