ListBox.AddRow

From Xojo Documentation

Method

ListBox.AddRow(ParamArray Item as String)

Supported for all project types and targets.

Appends Item in a new row to the end of the list. Because it is called ParamArray, you can pass several items for a multicolumn ListBox. The items are separated by commas and will be the values for the various columns.


Method

ListBox.AddRow(items() as String)

Supported for all project types and targets.

Appends a new row to the end of the list with each element of Item as a separate column.


Method

ListBox.AddRow()

Supported for all project types and targets.

Appends a new blank row to the end of the list.

Notes

Use LastAddedRowIndex to determine the last row added. In the case of hierarchical ListBoxes, AddRow appends Item to the subitems of the expanded row when called in the ExpandRow event. The theoretical maximum number of rows is over 2 billion but the actual number will be lower due to memory constraints.

Variant Usage

Note that you get a compile error if you pass a Variant to AddRow methods. When using a Variant, convert it first to string or array so the compiler knows which method you want to call:

Var v As Variant = "Hello"
ListBox1.AddRow(v.StringValue)

Scrolling

If you are adding many rows to the ListBox, eventually the results will scroll off the bottom of the ListBox and no longer be visible. To make a newly added row visible, you have to scroll the listbox to the location of the newly added row. You can do this by setting the SelectedRowIndex property to the value of the newly added row like this:

ListBox1.SelectedRowIndex = ListBox1.LastAddedRowIndex

Or you can manually scroll the ListBox to the right position:

ListBox1.ScrollPosition = ListBox1.LastAddedRowIndex

Performance

Populating ListBoxes with data is a common task. If you are filling a ListBox with lots of data (which itself may not be a great idea), you can first make the ListBox invisible before you add the rows and then make it visible after the rows have been added. This can prevent some unnecessary possible screen refreshes and resulting event calls, providing an overall speed improvement.

ListBox1.Visible = False
// Do long running AddRow code here
ListBox1.Visible = True

Overriding

If you need to override AddRow, override the second version with items array. This is the main method which is called by the others.

Sample Code

Adding the string "October" to a new row in a ListBox named Listbox1:

ListBox1.AddRow("October")

The following line adds a row of months to a ListBox that has four columns:

Listbox1.AddRow("Sept", "Oct", "Nov", "Dec")

The following line adds a row of months to a ListBox that has four columns using an array:

Var months() As String = Array("Sept", "Oct", "Nov", "Dec")
ListBox1.AddRow(months)

The following adds a blank row then sets values for column 3:

ListBox1.AddRow
ListBox1.CellValueAt(ListBox1.LastAddedRowIndex, 2) = "Col 3"

See Also

ListBox.AddFolder and ListBox.AddRowAt methods