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.
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):
However, you can add this functionality yourself by extending the Class Listbox
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
l.AddRowAt(RowNumber, "")
Return
End If
l.AddRowAt(RowNumber, Items(0))
If (Items.LastIndex < 1) Then Return
For i As Integer = 1 To Items.LastIndex
l.CellValueAt(l.LastAddedRowIndex, i) = Items(i)
Next
End Sub
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.)