菜鸟问题...我正在尝试在文本框中显示一个列表...不幸的是,我的代码仅显示列表中的第一个元素...
Noob question... I'm trying to display a List in a textbox... unfortunately, my code only displays the first element in the list...
private void Form1_Load(object sender, EventArgs e)
{
List<String> vegetables = new List<String>();
vegetables.Add("tomato");
vegetables.Add("carrot");
vegetables.Add("celery");
vegetables.Add("potato");
textBox1.Text = displayMembers(vegetables);
}
public string displayMembers(List<String> vegetables)
{
foreach (String s in vegetables)
{
return s.ToString();
}
return null;
}
如何让文本框显示所有成员?我的错在哪里?
How do I get the textBox to display all of the members? Where is my mistake?
一旦你 return s.ToString()
,该方法的其余部分将停止运行.
一个方法不能返回多个东西.
Once you return s.ToString()
, the rest of that method stops running.
A method cannot return multiple things.
你可能想写
someTextBox.Text = String.Join(Environment.NewLine, vegetables);
这篇关于显示列表<字符串>在文本框中(Winforms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!