Display Listlt;Stringgt; in textBox (Winforms)(显示列表lt;字符串gt;在文本框中(Winforms))
问题描述
菜鸟问题...我正在尝试在文本框中显示一个列表...不幸的是,我的代码仅显示列表中的第一个元素...
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:显示列表<字符串>在文本框中(Winforms)
				
        
 
            
        - C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
 - 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
 - CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
 - 带问号的 nvarchar 列结果 2022-01-01
 - Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
 - 使用 rss + c# 2022-01-01
 - 在 LINQ to SQL 中使用 contains() 2022-01-01
 - 在 C# 中异步处理项目队列 2022-01-01
 - Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
 - 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
 
						
						
						
						
						