vb.net 如何使用单行代码将多个项目添加到组合框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28007780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:40:58  来源:igfitidea点击:

How to add multiple items to a combobox using single line of code?

c#vb.net

提问by captain_999

How to add multiple items to a combobox using single line of code?

如何使用单行代码将多个项目添加到组合框?

public Form1()
    {
        InitializeComponent();
        comboBox1.Items.Add("simple");
        comboBox1.Items.Add("continuous");
        comboBox1.Items.Add("perfect");
        comboBox1.Items.Add("perfect continuous");
    }

回答by Bj?rn-Roger Kringsj?

"How to add multiple items to a combobox using single line of code?"

“如何使用单行代码将多个项目添加到组合框?”

If you look at the documentation for ComboBox.ObjectCollectionyou'll see that there's a method named AddRange. MSDN describes this method as:

如果您查看ComboBox.ObjectCollection的文档,您会看到有一个名为AddRange的方法。MSDN 将这种方法描述为:

Adds an array of items to the list of items for a ComboBox.

将一组项目添加到 ComboBox 的项目列表中。

So the one-liner you're looking for is:

所以你要找的单线是:

c#:

C#:

comboBox1.Items.AddRange(new string[] { "simple", "continuous", "perfect", "perfect continuous" });

vb.net:

vb.net:

ComboBox1.Items.AddRange(New String() {"simple", "continuous", "perfect", "perfect continuous"})

回答by Brandon Buck

There is no reason to do what you're asking. Reducing lines of code (LOC) unnecessarily is poor programming practice and should be avoided. Only do this where it makes sense and improves readability/understanding of the code.

没有理由去做你所要求的。不必要地减少代码行数 (LOC) 是糟糕的编程实践,应该避免。仅在有意义的情况下执行此操作并提高代码的可读性/理解能力。

When you're using a language that is compiled (and C# is compiled into an intermediate language) there really is no reason to care about how small you can make your source files, unless of course it's an improvement. When you're dealing with a scripting language where size may matter (like JavaScript for client-side code) minifiers or uglifiers take advantage of shortcuts to reduce as much whitespace as possible to reduce file sizes to speed downloads - but that's only necessary for released code - in development you keep it clean and readable.

当您使用一种经过编译的语言(并且 C# 被编译成一种中间语言)时,真的没有理由关心源文件的大小,除非它当然是一种改进。当您处理大小可能很重要的脚本语言(例如客户端代码的 JavaScript)时,minifier 或 uglifier 会利用快捷方式来减少尽可能多的空白以减少文件大小以加快下载速度 - 但这仅在已发布的情况下是必需的代码 - 在开发过程中,您要保持它的整洁和可读。

Now, all that being said you can "shorten" your code by using an array of strings and a foreachloop.

现在,说了这么多,您可以通过使用strings数组和foreach循环来“缩短”您的代码。

public Form1 {
    InitializeComponent();
    string[] items = new string[] { "simple", "continuous", "perfect", "perfect continuous" };
    foreach (string item in items) {
        comboBox1.Items.Add(item);
    }
}

Not quite as "one line" as you may want, but @OrelEraki proposed a one line solution that uses Listobject created in a similar fashion to the string array seen here. What you see may not be "shorter" than your example but it doesn't grow significantly with more items like what you have will do.

不像您想要的那样“一行”,但@OrelEraki 提出了一个一行解决方案,该解决方案使用List以类似于此处看到的字符串数组的方式创建的对象。您看到的内容可能不会比您的示例“短”,但随着更多项目(例如您将要做的事情)的增加,它不会显着增长。

回答by Pedro Henrique Bonifácio

Another way to solve this problem is in the [Design] tab. You just have to select you comboBox and open the proprierties, go to "Data" then to "Items" and just put this "(Collection)". Now you access the new part and put all the items you desire.

解决此问题的另一种方法是在 [设计] 选项卡中。你只需要选择你的组合框并打开属性,转到“数据”然后到“项目”,然后把这个“(集合)”。现在您可以访问新部分并放置您想要的所有项目。