method Items.AddItem ([Caption as Variant])

Adds a new item, and returns a handle to the newly created item.

   Caption as Variant A string expression that indicates the cell's caption for the first column. or a safe array that contains the captions for each column. The Caption accepts HTML format, if the CellCaptionFormat property is exHTML.  
  HITEMA long expression that indicates the handle of the newly created item. 

Use the Add method to add new columns to the control. If the control contains no columns, the AddItem method fails. Use the AddItem property when your control acts like a list. Use InsertItem when your control acts like a tree. Use the InsertControlItem property when the item needs to host an ActiveX control. Use the LockedItemCount property to add or remove items locked to the top or bottom side of the control. Use the MergeCells method to combine two or multiple cells in a single cell. Use the SplitCell property to split a 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. Use the FormatColumn property to format the column.

The AddItem property adds a new item that has no parent. When a new item is added (inserted) to the Items collection, the control fires the AddItem event. If the control contains more than one column use the CellCaption property to set the cell's caption. If there are no columns AddItem method fails. 

The following VB sample uses the VB Array function to add two items: 

With Tree1
        .Columns.Add "Column 1"
        .Columns.Add "Column 2"
        .Columns.Add "Column 3"
        With .Items
            .AddItem Array("Item 1.1", "Item 1.2", "Item 1.3")
            .AddItem Array("Item 2.1", "Item 2.2", "Item 2.3")
        End With
End With  

In VB/NET using the /NET assembly, the Array equivalent is New Object such as follows:

With Tree1

    .Columns.Add("Column 1")
    .Columns.Add("Column 2")
    .Columns.Add("Column 3")

    With .Items
        .AddItem(New Object() {"Item 1.1", "Item 1.2", "Item 1.3"})
        .AddItem(New Object() {"Item 2.1", "Item 2.2", "Item 2.3"})
    End With

End With

In C# using the /NET assembly, the Array equivalent is new object such as follows:


extree1.Columns.Add("Column 1");
extree1.Columns.Add("Column 2");
extree1.Columns.Add("Column 3");

extree1.Items.AddItem(new object[] { "Item 1.1", "Item 1.2", "Item 1.3" });
extree1.Items.AddItem(new object[] { "Item 2.1", "Item 2.2", "Item 2.3" });


Use the PutItems method to load an array, like in the following VB sample:

Set rs = CreateObject("ADODB.Recordset")
rs.Open "Orders", "Provider=Microsoft.Jet.OLEDB.3.51;Data Source= D:\Program Files\Microsoft Visual Studio\VB98\NWIND.MDB", 3 ' Opens the table using static mode
' Add the columns
With Tree1.Columns
For Each f In rs.Fields
    .Add f.Name
End With
Tree1.PutItems rs.getRows()

The following C++ sample adds new items to the control:

#include "Items.h"
CItems items = m_tree.GetItems();
long iNewItem = items.AddItem( COleVariant( "Item 1" ) );
items.SetCellCaption( COleVariant( iNewItem ), COleVariant( (long)1 ), COleVariant( "SubItem 1" ) );
iNewItem = items.AddItem( COleVariant( "Item 2" ) );
items.SetCellCaption( COleVariant( iNewItem ), COleVariant( (long)1 ), COleVariant( "SubItem 2" ) );

The following VB.NET sample adds new items to the control:

With AxTree1.Items
    Dim iNewItem As Integer
    iNewItem = .AddItem("Item 1")
    .CellCaption(iNewItem, 1) = "SubItem 1"
    iNewItem = .AddItem("Item 2")
    .CellCaption(iNewItem, 1) = "SubItem 2"
End With

The following C# sample adds new items to the control:

EXTREELib.Items items = axTree1.Items;
int iNewItem = items.AddItem( "Item 1" );
items.set_CellCaption( iNewItem, 1, "SubItem 1" );
iNewItem = items.AddItem( "Item 2" );
items.set_CellCaption( iNewItem, 1, "SubItem 2" );

The following VFP sample adds new items to the control:

with thisform.Tree1.Items
	.DefaultItem = .AddItem("Item 1")
	.CellCaption(0, 1) = "SubItem 1"

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