Question

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. I have tried the same thing with the use of the dll but it's not quite the same. 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.

Any help will be appreciated.

Was it helpful?

Solution

The ListBox.Items.AddRange method only accepts an array of Object. The ScintillaNet.Scintilla.Lines property is a ScintillaNet.LinesCollection object, not an array. As such, you cannot pass it to the ListBox.Items.AddRange method.

The RichTextBox.Lines property, on the other hand, is an array of String, so that can be passed to the ListBox.Items.AddRange method.

Unfortunately, there is no easy way to convert from a ScintillaNet.LinesCollection object to an array. 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:

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. As Steve astutely pointed out in the comments below, the LineCollection contains a list of Line objects. The ToString method on the Line class simply outputs the line index rather than the text from that line.

OTHER TIPS

Building on Steven Doggart's answer, using AddRange() instead of Range() would look like this:

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top