显示列表<字符串>在文本框中(Winforms)

3

本文介绍了显示列表<字符串>在文本框中(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);

这篇关于显示列表&lt;字符串&gt;在文本框中(Winforms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14