I have a one row, 3 column listbox and I want to insert a row into just the first row. I’d like to use the AddRowAt function but can’t get it work. I’m trying to add data into all 3 columns. Something like red, green, blue. It works as long as I only have one of the three. According to the docs:
AddRowAt(RowNumber as Integer, Item as String, Indent as Integer)
I have the RowNumber as 0 because I always want it to be the first row. I’m trying to concatenate the data into one string for the second parameter. I don’t use the third parameter.
Yes, I can just use AddRow and it works but this had got my interest. I’d like it to work on learn why it doesn’t work.
Maybe try something like the following:
ListBox1.CellValueAt(0, 1) = "green"
ListBox1.CellValueAt(0, 2) = "blue"
See CellValueAt for more info.
I hope that helps.
there should be a feature request that this method AddRowAt have the same arguments.
It doesn’t work because AddRow() and AddRowAt() have different method signatures. AddRow() allows you to supply a ParamArray with values for each column. AddRowAt() does not use a ParamArray so you can only add the first column. Add subsequent columns using CellValueAt like Scott showed (except I would also use LastAddedRowIndex):
ListBox1.CellValueAt(ListBox1.LastAddedRowIndex, 1) = "green"
ListBox1.CellValueAt(ListBox1.LastAddedRowIndex, 2) = "blue"
However, you can add this functionality yourself by extending the Class
Insert a Module (or use an existing one) to your project.
Then add this public extension Method to the Module:
Public Sub AddRowAt(Extends l As Listbox, RowNumber As Integer, ParamArray Items() As String)
If (Items.LastIndex < 0) Then
If (Items.LastIndex < 1) Then Return
For i As Integer = 1 To Items.LastIndex
l.CellValueAt(l.LastAddedRowIndex, i) = Items(i)
Now you can use your extension like this:
Listbox1.AddRowAt(0, "red", "green", "blue")
Thanks for the tips. It’s just as easy to delete the rows before adding the one row. That works but…once something gets my interest I like to see it through.
I’ll try a couple of these and get back to you. Especially @Jürg_Otter 's solution.
other options is to use a own cell paint with data from a array or a class object which is stored in a rowtag or celltag so you not have to convert it all into cell strings. (in case the list is only for display data and not for input.)
I’ll leave it up to you to add an extension allowing you to write:
Listbox1.ReplaceRowAt(0, "red", "green", "blue")
Hint: Don’t duplicate code - you can call an extension method from another one.
Did not realize this was an old thread!!! Please delete