简体   繁体   English

如何将项目添加到列表框?

[英]How to add Items to a Listbox?

The line of code below adds each line too each index of the list Box.下面的代码行也添加了列表框的每个索引的每一行。

ListBox1.Items.AddRange(CType(TabControl1.SelectedTab.Controls.Item(0), RichTextBox).Lines)

This works however, if I wish to perform the same function as the line below but with the ScintillaNet DLL .但是,如果我希望使用ScintillaNet DLL执行与下面一行相同的功能,这将起作用。 I have tried the same thing with the use of the dll but it's not quite the same.我已经使用 dll 尝试了同样的事情,但它并不完全相同。 Here was the code I tested:这是我测试的代码:

ListBox1.Items.AddRange(CType(TabControl1.SelectedTab.Controls.Item(0), ScintillaNet.Scintilla).Lines)

I am sorry I am asking such a silly question but I am a noob at the ScintillaNet DLL .很抱歉我问了这么一个愚蠢的问题,但我是ScintillaNet DLL菜鸟

Any help will be appreciated.任何帮助将不胜感激。

The ListBox.Items.AddRange method only accepts an array of Object . ListBox.Items.AddRange方法只接受一个Object数组。 The ScintillaNet.Scintilla.Lines property is a ScintillaNet.LinesCollection object, not an array. ScintillaNet.Scintilla.Lines属性是一个ScintillaNet.LinesCollection对象,而不是一个数组。 As such, you cannot pass it to the ListBox.Items.AddRange method.因此,您不能将其传递给ListBox.Items.AddRange方法。

The RichTextBox.Lines property, on the other hand, is an array of String , so that can be passed to the ListBox.Items.AddRange method.另一方面, RichTextBox.Lines属性一个String数组,因此可以将其传递给ListBox.Items.AddRange方法。

Unfortunately, there is no easy way to convert from a ScintillaNet.LinesCollection object to an array.不幸的是,没有简单的方法可以将ScintillaNet.LinesCollection对象转换为数组。 You could use it's CopyTo method to copy the collection to an array, but it's probably easier and more efficient to just loop through the collection and add each one individually, like this:您可以使用它的CopyTo方法将集合复制到数组,但循环遍历集合并单独添加每个集合可能更容易和更有效,如下所示:

For Each i As Line In CType(TabControl1.SelectedTab.Controls.Item(0), ScintillaNet.Scintilla).Lines
    ListBox1.Items.Add(i.Text)
Next

Notice that I am adding i.Text to the ListBox rather than just i .请注意,我将i.Text添加到ListBox而不仅仅是i As Steve astutely pointed out in the comments below, the LineCollection contains a list of Line objects.正如史蒂夫在下面的评论中LineCollection地指出的那样, LineCollection包含一个Line对象列表。 The ToString method on the Line class simply outputs the line index rather than the text from that line. Line类上的ToString方法只输出行索引而不是该行的文本。

Building on Steven Doggart's answer, using AddRange() instead of Range() would look like this:基于 Steven Doggart 的回答,使用 AddRange() 而不是 Range() 看起来像这样:

Dim lst As New List(Of String)

For Each i As Line In CType(TabControl1.SelectedTab.Controls.Item(0), ScintillaNet.Scintilla).Lines
    lst.Add(i.Text)
Next

ListBox1.Items.AddRange(lst.ToArray)
Dim ListA As New List(Of String)(New String() {"aaa", "bbb", "ccc", "ddd"})
ComboBox1.Items.AddRange(ListA.ToArray)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM