vb.netscintillalistbox-control

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. 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.


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.