method Items.Add ([Caption as Variant])
Adds a new item, and returns the index of the newly created item.

 TypeDescription 
   Caption as Variant A string expression that indicates the caption for the cell in the first column, or a safe array that holds the captions for each column.  
 ReturnDescription 
  LongA long value that indicate the index for the newly created item  

The Add method adds a new item to the end of the list. If the control has more than a Column object, use the Caption property to set the caption for the rest of the cells.  Use the CaptionFormat property to specify HTML tags in the caption, or a computed field. Use ItemPosition property to change the item's position. The AddItem event is fired once that a new item has been added.  Use the PutItems method to load the control from an array. Use the DataSource property to bind the control to an ADO record set. The Add method is not available if the control is running in the virtual mode. Use the CellMerge property to combine two or more cells in a single cell. Use the BeginUpdate and EndUpdate methods to maintain performance while adding new columns and items. Use the ConditionalFormats method to apply formats to a cell or range of cells, and have that formatting change depending on the value of the cell or the value of a formula.

The following VB sample loads each item from a safe array:

With List1
    .BeginUpdate
    
    .Columns.Add "Column 1"
    .Columns.Add "Column 2"
    .Columns.Add "Column 3"
    
    With .Items
        .Add Array("Item 1", "Item 2", "Item 3")
        .Add Array("Item 4", "Item 5", "Item 6")
        .Add Array("Item 7", "Item 8", "Item 9")
    End With
    
    .EndUpdate
End With

The following VB sample add three columns and three items:

With List1
    .BeginUpdate
    
    .Columns.Add "Column 1"
    .Columns.Add "Column 2"
    .Columns.Add "Column 3"
    
    With .Items
        Dim i As Long
        i = .Add("Item 1")
        .Caption(i, 1) = "Item 2"
        .Caption(i, 2) = "Item 3"
        i = .Add("Item 4")
        .Caption(i, 1) = "Item 5"
        .Caption(i, 2) = "Item 6"
        i = .Add("Item 7")
        .Caption(i, 1) = "Item 8"
        .Caption(i, 2) = "Item 9"
    End With
    
    .EndUpdate
End With

The following C++ adds three columns and three items:

m_list.BeginUpdate();
CColumns columns = m_list.GetColumns();
CColumn column( V_DISPATCH( &columns.Add( "Column 1" ) ) );
columns.Add( "Column 2" );
columns.Add( "Column 3" );
CItems items = m_list.GetItems();
int i = items.Add( COleVariant("Item 1" ) );
items.SetCaption( i, COleVariant( long(1) ), COleVariant("SubItem 1.1" ) );
items.SetCaption( i, COleVariant( long(2) ), COleVariant("SubItem 1.2" ) );
i = items.Add( COleVariant("Item 2" ) );
items.SetCaption( i, COleVariant( long(1) ), COleVariant("SubItem 2.1" ) );
items.SetCaption( i, COleVariant( long(2) ), COleVariant("SubItem 2.2" ) );
i = items.Add( COleVariant("Item 3" ) );
items.SetCaption( i, COleVariant( long(1) ), COleVariant("SubItem 3.1" ) );
items.SetCaption( i, COleVariant( long(2) ), COleVariant("SubItem 3.2" ) );
m_list.EndUpdate();

The following VB.NET adds three columns and three items:

With AxList1
    .BeginUpdate()
    .Columns.Add("Column 1")
    .Columns.Add("Column 2")
    .Columns.Add("Column 3")
    With .Items
        Dim i As Integer = .Add("Item 1")
        .Caption(i, 1) = "SubItem 1.1"
        .Caption(i, 2) = "SubItem 1.2"
        i = .Add("Item 2")
        .Caption(i, 1) = "SubItem 2.1"
        .Caption(i, 2) = "SubItem 2.2"
        i = .Add("Item 3")
        .Caption(i, 1) = "SubItem 3.1"
        .Caption(i, 2) = "SubItem 3.2"
    End With
    .EndUpdate()
End With

The following C# adds three columns and three items:

axList1.BeginUpdate();
axList1.Columns.Add("Column 1");
axList1.Columns.Add("Column 2");
axList1.Columns.Add("Column 3");
EXLISTLib.Items items = axList1.Items;
int i = items.Add("Item 1");
items.set_Caption(i, 1,"SubItem 1.1");
items.set_Caption(i, 2,"SubItem 1.2");
i = items.Add("Item 2");
items.set_Caption(i, 1,"SubItem 2.1");
items.set_Caption(i, 2,"SubItem 2.2");
i = items.Add("Item 3");
items.set_Caption(i, 1,"SubItem 3.1");
items.set_Caption(i, 2,"SubItem 3.2");
axList1.EndUpdate();

The following VFP adds three columns and three items:

with thisform.List1
    .BeginUpdate()
    .Columns.Add("Column 1")
    .Columns.Add("Column 2")
    .Columns.Add("Column 3")
    With .Items
        Local i
        i = .Add("Item 1")
        .Caption(i, 1) = "SubItem 1.1"
        .Caption(i, 2) = "SubItem 1.2"
        i = .Add("Item 2")
        .Caption(i, 1) = "SubItem 2.1"
        .Caption(i, 2) = "SubItem 2.2"
        i = .Add("Item 3")
        .Caption(i, 1) = "SubItem 3.1"
        .Caption(i, 2) = "SubItem 3.2"
    EndWith
    .EndUpdate()
 endwith
 


Send comments on this topic.
1999-2017 Exontrol.COM, Software. All rights reserved.