Add, AddItem Method (ListView, ListItems)

Purpose

Adds an ListItem to a ListItems collection in a ListView control and returns a reference to the newly created ListItem object.

Syntax

ListView.Add[Item]([index], [key], [text], [icon] [, smallicon])

ListItems.Add([index], [key], [text], [icon] [, smallicon])

object:ListView, ListItems
Index, Key, Text, Icon, smallicon: Variant exp

Description

The ListView Ocx has the AddItem and Add methods, which act exactly the same. The ListItems object supports the Add method only.

index Optional. An integer specifying the position where you want to insert the ListItem. If no index is specified, the ListItem is added to the end of the ListItems collection.
key Optional. A unique string expression that can be used to access a member of the collection.
text Optional. A string that is associated with the ListItem object control.
icon Optional. An integer that sets the icon to be displayed from an ImageList control, when the ListView control is set to Icon view.
smallicon Optional. An integer that sets the icon to be displayed from an ImageList control, when the ListView control is set to SmallIcon view.

Before setting either the Icons or SmallIcons properties, you must first initialize them. You can do this at design time by specifying an ImageList object, or at run time with the following code:

listview1.Icons = iml1       'Assuming the Imagelist is iml1.

listview1.SmallIcons = iml2

If the list is not currently sorted, a ListItem object can be inserted in any position by using the index argument. If the list is sorted, the index argument is ignored and the ListItem object is inserted in the appropriate position based upon the sort order.

If index is not supplied, the ListItem object is added with an index that is equal to the number of ListItem objects in the collection + 1.

Use the Key property to reference a member of the ListItems collection if you expect the value of an object's Index property to change, such as by dynamically adding objects to or removing objects from the collection.

Example

Dim li As ListItem

OpenW 1, 20, 20 , 300, 300

Ocx ListView lvw = , 10, 10, 100, 100

lvw.View = 2

lvw.AddItem 1, , "First"

lvw.Add 2, , "Second"

lvw.ListItems.Add 3, , "Third"

Set li = lvw.Add( , , "Fourth")

Do

Sleep

Until Me Is Nothing

Remarks

GFA-BASIC 32 specific

Instead of explicitly using the ListItems collection to access a ListItem element, you can use a shorter notation. First, the ListView supports an Item property:

lvw.Item(idx), lvw.ListItems.Item(idx)

Like the Item method of lvw.ListItems, Item is the default method of ListView. Therefore, a ListItem can be accessed as follows:

lvw(idx) , lvw.ListItems(idx)

lvw!idx, lvw.ListItems!idx

Each dot saves about 30 bytes of code.

To enumerate over the ListItems collection of a ListView Ocx, use For Each on the Ocx control directly, like:

Local li As ListItem

For Each li In lvw : DoSomething(li) : Next

See Also

ListView, ListItem, ListItems

{Created by Sjouke Hamstra; Last updated: 23/09/2014 by James Gaite}