event Change (Item as HITEM, ColIndex as Long, NewValue as Variant)
Occurs when the user changes the cell's content.

 TypeDescription 
   Item as HITEM A long expression that determines the item's handle. If the Item parameter is 0, and the ColIndex property is different than zero, the ColIndex indicates the handle of the cell where the state is changed.  
   ColIndex as Long A long expression that indicates the column's index, if the Item parameter is not zero, a long expression that indicates the handle of the cell if the Item parameter is 0.  
   NewValue as Variant A Variant value that indicates the newly cell's value. You can use the EditingText property to retrieve the caption of the editor while the control is in edit mode.  

The Change event notifies your application that the cell's value is about to be changed. The NewValue parameter of the Change event indicates the newly value to be assigned to the cell's value. The EditingText property returns the caption being shown on the editor while the control runs in edit mode. Changing the CellValue property invokes Change event too. During the Change event it is possible to have recursive calls, if you are changing the CellValue property ( only when you assign a value to a cell, not when you are retrieving the cell's value ). If you are changing the other cell's value, during the Change event you have to add a C++ code like follows in order to avoid recursive calls:

static sg_ChangeCounter = 0;
void OnChangeGrid1(long Item, long ColIndex, VARIANT FAR* NewValue) 
{
	if ( sg_ChangeCounter == 0)
	{
		sg_ChangeCounter++;
		m_Items.SetCellValue( COleVariant( Item ), COleVariant( (long)othercolumn ), *NewValue );
		sg_ChangeCounter--;
	}
}

or in VB you could have like this:

Private sg_ChangeCounter As Long
Private Sub Grid1_Change(ByVal Item As EXGRIDLibCtl.HITEM, ByVal ColIndex As Long, NewValue As Variant)
    If (sg_ChangeCounter = 0) Then
        sg_ChangeCounter = sg_ChangeCounter + 1
        Grid1.Items.CellValue(Item, othercolumn) = NewValue
        sg_ChangeCounter = sg_ChangeCounter - 1
    End If
End Sub

The edit events are fired in the following order:

  1. Edit event. Prevents editing cells, before showing the cell's editor.

  2. EditOpen event. The edit operation started, the cell's editor is shown. The Editing property gives the window's handle of the built-in editor being started.

  3. Change event. The Change event is fired only if the user types ENTER key, or the user selects a new value from a predefined data list.

  4. EditClose event. The cell's editor is hidden and closed.

Use the CellEditor or Editor property to assign an editor to a cell or to a column. Use the Edit event  to notify your application that the editing operation begins. The Change event notifies that the editing focused cell ended. If the control is bounded to an ADO recordset the Change event is automatically called when the user changes the focused cell, and it updates the recordset too. The control fires the ValidateValue event before calling the Change event, if the CauseValidateValue property is True. Please note that the Change event is called also when loading, or adding new items , so you need to use an internal counter ( like explained bellow ) to avoid calling the Change event during adding or loading the items, if it is not case ( increases the iChanging variable before loading items, and decreases the iChanging member when adding items is done ). Call the Refresh method, when changing the value for a cell that has the CellSingleLine property on False.

Syntax for Change event, /NET version, on:

private void Change(object sender,int Item,int ColIndex,ref object NewValue)
{
}

Private Sub Change(ByVal sender As System.Object,ByVal Item As Integer,ByVal ColIndex As Integer,ByRef NewValue As Object) Handles Change
End Sub

Syntax for Change event, /COM version, on:

private void Change(object sender, AxEXGRIDLib._IGridEvents_ChangeEvent e)
{
}

void OnChange(long Item,long ColIndex,VARIANT FAR* NewValue)
{
}

void __fastcall Change(TObject *Sender,Exgridlib_tlb::HITEM Item,long ColIndex,Variant * NewValue)
{
}

procedure Change(ASender: TObject; Item : HITEM;ColIndex : Integer;var NewValue : OleVariant);
begin
end;

procedure Change(sender: System.Object; e: AxEXGRIDLib._IGridEvents_ChangeEvent);
begin
end;

begin event Change(long Item,long ColIndex,any NewValue)
end event Change

Private Sub Change(ByVal sender As System.Object, ByVal e As AxEXGRIDLib._IGridEvents_ChangeEvent) Handles Change
End Sub

Private Sub Change(ByVal Item As EXGRIDLibCtl.HITEM,ByVal ColIndex As Long,NewValue As Variant)
End Sub

Private Sub Change(ByVal Item As Long,ByVal ColIndex As Long,NewValue As Variant)
End Sub

LPARAMETERS Item,ColIndex,NewValue

PROCEDURE OnChange(oGrid,Item,ColIndex,NewValue)
RETURN

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

<SCRIPT EVENT="Change(Item,ColIndex,NewValue)" LANGUAGE="JScript">
</SCRIPT>

<SCRIPT LANGUAGE="VBScript">
Function Change(Item,ColIndex,NewValue)
End Function
</SCRIPT>

Procedure OnComChange HITEM llItem Integer llColIndex Variant llNewValue
	Forward Send OnComChange llItem llColIndex llNewValue
End_Procedure

METHOD OCX_Change(Item,ColIndex,NewValue) CLASS MainDialog
RETURN NIL

void onEvent_Change(int _Item,int _ColIndex,COMVariant /*variant*/ _NewValue)
{
}

function Change as v (Item as OLE::Exontrol.Grid.1::HITEM,ColIndex as N,NewValue as A)
end function

function nativeObject_Change(Item,ColIndex,NewValue)
return

The following VB sample displays the newly value of the focused cell:

Private Sub Grid1_Change(ByVal Item As EXGRIDLibCtl.HITEM, ByVal ColIndex As Long, NewValue As Variant)
    ' Displays the old/new cell's value
    Debug.Print "The current cell's value is '" & Grid1.Items.CellValue(Item, ColIndex) & "'."
    Debug.Print "The newly cell's value is '" & NewValue & "'."
End Sub

You can change the newly cell's value by changing the NewValue parameter of the Change event. If you are changing the CellValue property during the Change event a recursive calls occurs, so you need to protect recursive calls using an internal counter that's increased when Change event starts, and decreased when the Change event ends like in the following VB sample:

Private iChanging As Long

Private Sub Grid1_Change(ByVal Item As EXGRIDLibCtl.HITEM, ByVal ColIndex As Long, NewValue As Variant)
    If (iChanging = 0) Then
        iChanging = iChanging + 1
            ' here's safe to change the Items.CellValue property
        iChanging = iChanging - 1
    End If
End Sub

The following sample is the C++ equivalent:

long iChanging = 0;
void OnChangeGrid1(long Item, long ColIndex, VARIANT FAR* NewValue) 
{
	if ( iChanging == 0 )
	{
		iChanging++;
		// here's safe to call Items.CellValue property, to avoid recursive calls.
		iChanging--;
	}
}

The following C++ sample displays the newly value of the focused cell:

#include "Items.h"
void OnChangeGrid1(long Item, long ColIndex, VARIANT FAR* NewValue) 
{
	if ( ::IsWindow( m_grid.m_hWnd ) )
	{
		CItems items = m_grid.GetItems();
		COleVariant vtItem( Item ), vtColumn( ColIndex );
		CString strFormat;
		strFormat.Format( "'%s' = %s", V2S( &items.GetCellValue( vtItem, vtColumn ) ), V2S( NewValue ) );
		OutputDebugString( strFormat );
	}
}

where the V2S function converts a VARIANT to a string value, and may look like follows:

static CString V2S( VARIANT* pv, LPCTSTR szDefault = _T("") )
{
	if ( pv )
	{
		if ( pv->vt == VT_ERROR )
			return szDefault;

		COleVariant vt;
		vt.ChangeType( VT_BSTR, pv );
		return V_BSTR( &vt );
	}
	return szDefault;
}

The following VB.NET sample displays the newly value of the focused cell:

Private Sub AxGrid1_Change(ByVal sender As Object, ByVal e As AxEXGRIDLib._IGridEvents_ChangeEvent) Handles AxGrid1.Change
    With AxGrid1.Items
        Debug.Print("Old Value: " & .CellValue(e.item, e.colIndex) & " New Value " & e.newValue.ToString())
    End With
End Sub

The following C# sample displays the newly value of the focused cell:

private void axGrid1_Change(object sender, AxEXGRIDLib._IGridEvents_ChangeEvent e)
{
	System.Diagnostics.Debug.WriteLine("Old Value " + axGrid1.Items.get_CellValue(e.item, e.colIndex).ToString() + " New Value " + e.newValue.ToString());
}

The following VFP sample displays the newly value of the focused cell:

*** ActiveX Control Event ***
LPARAMETERS item, colindex, newvalue

with thisform.Grid1.Items
	.DefaultItem = item
	local oldvalue
	oldvalue = .CellValue(0,colindex)
	wait window nowait "Old Value " + str(oldvalue)
	wait window nowait "New Value " + str(newvalue)
endwith

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