event AddItem (Item as HITEM)

Occurs after a new Item has been inserted to Items collection.

TypeDescription
Item as HITEM A long expression that indicates the handle of the item that's inserted to the Items collection.

The AddItem event notifies your application that a new items is inserted. Use the AddItem and InsertItem methods to insert new items to Items collection. Use the InsertControlItem method to add a new item that hosts an ActiveX control. Use the Add method to add new columns to Columns Collection. Use the Def property to specify a common value for all cells in the same column. Use the the AddBar method to add new bars to the newly added item. 

If the control's DataSource property is set, the AddItem event occurs as soon as a new record is loaded from the giving recrodset. Also, the AddItem event occurs if the AddNew ( method of the ADO.RecordSet object ) is performed, if the control's DetectAddNew property is True. If using the CellValue properties during the AddItem event, you must be sure that they are available, or they have the proper values or expected values. 

Syntax for AddItem event, /NET version, on:

private void AddItem(object sender,int Item)
{
}

Private Sub AddItem(ByVal sender As System.Object,ByVal Item As Integer) Handles AddItem
End Sub

Syntax for AddItem event, /COM version, on:

private void AddItem(object sender, AxEXG2ANTTLib._IG2anttEvents_AddItemEvent e)
{
}

void OnAddItem(long Item)
{
}

void __fastcall AddItem(TObject *Sender,Exg2anttlib_tlb::HITEM Item)
{
}

procedure AddItem(ASender: TObject; Item : HITEM);
begin
end;

procedure AddItem(sender: System.Object; e: AxEXG2ANTTLib._IG2anttEvents_AddItemEvent);
begin
end;

begin event AddItem(long Item)
end event AddItem

Private Sub AddItem(ByVal sender As System.Object, ByVal e As AxEXG2ANTTLib._IG2anttEvents_AddItemEvent) Handles AddItem
End Sub

Private Sub AddItem(ByVal Item As EXG2ANTTLibCtl.HITEM)
End Sub

Private Sub AddItem(ByVal Item As Long)
End Sub

LPARAMETERS Item

PROCEDURE OnAddItem(oG2antt,Item)
RETURN

Syntax for AddItem event, /COM version (others), on:

<SCRIPT EVENT="AddItem(Item)" LANGUAGE="JScript">
</SCRIPT>

<SCRIPT LANGUAGE="VBScript">
Function AddItem(Item)
End Function
</SCRIPT>

Procedure OnComAddItem HITEM llItem
	Forward Send OnComAddItem llItem
End_Procedure

METHOD OCX_AddItem(Item) CLASS MainDialog
RETURN NIL

void onEvent_AddItem(int _Item)
{
}

function AddItem as v (Item as OLE::Exontrol.G2antt.1::HITEM)
end function

function nativeObject_AddItem(Item)
return

For instance, let's say that we defined the AddItem event such as:

Private Sub G2antt1_AddItem(ByVal Item As EXG2ANTTLibCtl.HITEM)
    With G2antt1.Items
        .AddBar Item, "Task", .CellValue(Item, 1), .CellValue(Item, 2)
    End With
End Sub

If using the r.AddNew method we MUST use the values to be added as parameters of the AddNew method as in the following sample:

r.AddNew Array(0, 1, 2), Array("Task", #1/3/2001#, #1/4/2001#)

instead using the following code:

r.AddNew
	r(0) = "Task"
	r(1) = #1/1/2001#
	r(2) = #1/2/2001#
r.Update

which is wrong as the AddItem event is called when the r.AddNew method is performed, and so during the AddItem event, the values for the cells are NOT yet available, as the r(0), r(1), r(2) are filled later then r.AddNew call. 

The following VB sample shows how to change the item's foreground color:

Private Sub G2antt1_AddItem(ByVal Item As EXG2ANTTLibCtl.HITEM)
    G2antt1.Items.ItemForeColor(Item) = vbBlue
End Sub

The following VB sample changes the background color for all cells in the first column:

G2antt1.Columns(0).Def(exCellBackColor) = RGB(240, 240, 240)

The following C++ sample changes the item's foreground color when a new items is inserted:

#include "Items.h"
void OnAddItemG2antt1(long Item) 
{
	if ( ::IsWindow( m_g2antt.m_hWnd ) )
	{
		CItems items = m_g2antt.GetItems();
		items.SetItemForeColor( Item, RGB(0,0,255) );
	}
}

The following C++ sample changes the background color for all cells in the first column:

COleVariant vtBackColor( (long)RGB(240, 240, 240) );
m_g2antt.GetColumns().GetItem( COleVariant( (long) 0 ) ).SetDef( /*exCellBackColor*/ 4, vtBackColor );

The following VB.NET sample changes the item's foreground color when a new items is inserted:

Shared Function ToUInt32(ByVal c As Color) As UInt32
    Dim i As Long
    i = c.R
    i = i + 256 * c.G
    i = i + 256 * 256 * c.B
    ToUInt32 = Convert.ToUInt32(i)
End Function

Private Sub AxG2antt1_AddItem(ByVal sender As System.Object, ByVal e As AxEXG2ANTTLib._IG2anttEvents_AddItemEvent) Handles AxG2antt1.AddItem
    AxG2antt1.Items.ItemForeColor(e.item) = ToUInt32(Color.Blue)
End Sub

The following VB.NET sample changes the background color for all cells in the first column:

With AxG2antt1.Columns(0)
    .Def(EXG2ANTTLib.DefColumnEnum.exCellBackColor) = ToUInt32(Color.WhiteSmoke)
End With

The following C# sample changes the item's foreground color when a new items is inserted:

private UInt32 ToUInt32(Color c)
{
	long i;
	i = c.R;
	i = i + 256 * c.G;
	i = i + 256 * 256 * c.B;
	return Convert.ToUInt32(i);
}

private void axG2antt1_AddItem(object sender, AxEXG2ANTTLib._IG2anttEvents_AddItemEvent e)
{
	axG2antt1.Items.set_ItemForeColor( e.item, ToUInt32(Color.Blue) );
}

The following C# sample changes the background color for all cells in the first column:

axG2antt1.Columns[0].set_Def(EXG2ANTTLib.DefColumnEnum.exCellBackColor, ToUInt32(Color.WhiteSmoke));

The following VFP sample changes the item's foreground color when a new items is inserted:

*** ActiveX Control Event ***
LPARAMETERS item

with thisform.G2antt1.Items
	.DefaultItem = item
	.ItemForeColor( 0 ) = RGB(0,0,255 )
endwith

The following VFP sample changes the background color for all cells in the first column:

with thisform.G2antt1.Columns(0)
	.Def( 4 ) = RGB(240,240,240)
endwith

For instance, the following VB sample loads an ADO recordset.

Dim rs As Object

Private Sub Form_Load()

    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

    G2antt1.BeginUpdate
    ' Add the columns
    With G2antt1.Columns
    For Each f In rs.Fields
        .Add f.Name
    Next
    End With

    ' Add the items
    With G2antt1.Items
    rs.MoveFirst
    While Not rs.EOF
        .InsertItem , rs.Bookmark
        rs.MoveNext
    Wend
End With

    G2antt1.EndUpdate
End Sub

Private Sub G2antt1_AddItem(ByVal Item As EXG2ANTTLibCtl.HITEM)
    Dim i As Integer
    Dim n As Integer
    n = G2antt1.Columns.Count
    With G2antt1.Items
    For i = 0 To n - 1
        .CellValue(Item, i) = rs(i).Value
    Next
    End With
End Sub

The following VB sample use the PutItems method to load items to the control:

Dim rs As Object

Private Sub Form_Load()

    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

    G2antt1.BeginUpdate
    ' Add the columns
    With G2antt1.Columns
    For Each f In rs.Fields
        .Add f.Name
    Next
    End With

    G2antt1.PutItems rs.getRows()

    G2antt1.EndUpdate
End Sub