Assign an array to a ListBox.Heading

I guess that ListBox.Heading is not an actual array, but a method?

I don’t seem to be able to do

// Can't remember if I can initialize an array in Xojo like this, but you get the gist...
Dim myHeadings() As String = ("Col1", "Col2", "Col3")
myListBox.Heading = myHeadings

what version youre trying this is always useful to know since the syntax for API 1 and 2 is different

I’m going to guess you mean 2019r1.1 or older ?
Heading doesnt take an array
You assign them one at a time

Beat me to it!

Or you could write your own extension that does this :slight_smile:

Something like

Module ListBoxExtensions
    Sub Heading(extends lb as listbox, assigns s() as string)

             for i as integer = 0 to min(s.ubound, lb.columnCount - 1)
                  lb.heading(i) = s(i)

    End Sub

    Function Heading(extends lb as listbox) as String()
             dim headings() as string
             for i as integer = 0 to lb.columnCount - 1
                  headings.append lb.heading(i)
             return headings           

    End Function

End Module

How about:

Dim myHeadings() As String = Array("Col1", "Col2", "Col3")
Listbox1.HasHeading = True //not needed if already set
Listbox1.ColumnCount = myHeadings.Ubound + 1 //not needed if already set
Listbox1.Heading(-1) = Join(myHeadings, Chr(9))

AH yeah I forget the magic -1 setting lots of times :slight_smile:

When I move away from the last good version (2019r1.1), I’ll let you know @npalardy!


-1 doesn’t work, AFAIK, but MAYBE if it’s a tab-delimited list instead of an array. Haven’t tried it…

there sre some things with listbox that do accept -1 and work on the whole row etc and others that dont
and its not well documented which is which

Why do you say it doesn’t work? I tested that code in Xojo2019r1.1 and Xojo2019r3.1 and it worked.

If you see the docs: it says:

You can set the headings in a multi-column listbox by assigning to Heading(-1) the text of the headings separated by the tab character, e.g.,

Me.Heading(-1) = "FirstName" + Chr(9) + "LastName"

That’s why Join is needed, to join the array with Chr(9) between each element.

If you tried:

myListbox.Headion(-1) = myHeadings

it won’t work because and Array doesn’t have Tab between elements.

make sure HasHeading is true or you wont see it

That’s great! Thank you for checking.

‘AFAIK’ means “As Far As I Know”, I also tagged it with “I haven’t tried it”. Both of these let me off the hook. :wink:

Ah, ok, well know you know that it works :slight_smile: