Edit Events
Editing process is started after user click on cell, or start typing while cell is selected. In both cases public method EditCell is called.There are several events used in editing cell process. They are occur in next order:
OnBeforeEdit
Triggered before editing is started.
Accept parameter can be set to False in order to prevent editing.
Text can be adjusted to custom text (different than one in underlaying cell).
procedure TForm1.NextGrid61BeforeEdit(Sender: TObject; ACol, ARow: Integer; var Accept: Boolean;
var Text: WideString);
begin
// Set default text inside edit in case if cell is empty
if Text = '' then Text := 'Default';
// Prevent editing first 2 rows
if ARow < 2 then Accept := False;
end;
OnGetInplaceEdit
Editing is started, but in-place editor control is not yet set.
This event provide var InplaceEdit parameter which allows switching (or even creating) component to be used as editor.
procedure TForm1.NextGrid61GetInplaceEdit(Sender: TObject; ACol, ARow: Integer;
var InplaceEdit: INxInplaceEdit);
begin
if ACol = 1 then
begin
{ Set `InplaceEdit` var arg. }
InplaceEdit := TListBoxEdit.Create(Self);
end;
end;
If InplaceEdit is not nil, editing will start. EditingCell property is also set.
TListBoxEdit class used in example is TListBox descendant with INxInplaceEdit implementation.
OnInplaceEditInsert
InplaceEdit component is inserted (Parent property is set) into Grid.
This event provides opportunity to further adjust inplace-edit component with Component parameter.
It's called before Text is set, so this event is good place for tasks such as adding Items before ItemIndex is set.
procedure TForm1.NextGrid61InplaceEditInsert(Sender: TObject; ACol, ARow: Integer;
Component: TComponent);
begin
if ACol = 1 then
begin
with Component as TListBoxEdit do
begin
Items.Add('George');
Items.Add('Lucia');
Items.Add('Michael');
Items.Add('Ann');
Items.Add('Ben');
end;
end;
end;
OnInplaceEditBounds
This event is place where bounds of editor control can be adjusted. Event includes var parameter EditRect that can be adjusted.
procedure TForm1.NextGrid61InplaceEditBounds(Sender: TObject; ACol, ARow: Integer; Component: TComponent;
var EditRect: TRect);
begin
if (ACol = 1) and (NextGrid61.Columns[ACol].Width < 60) then
begin
EditRect.Right := EditRect.Right + 20;
end;
end;
Event is added in version 6.3.0 of NextGrid.
InplaceEdit is positioned, shown and focused.
OnEditEnter
Editing is started.
OnInplaceEditChange
Occurs after edit's value, or some other important property is changed. When this event is triggered depends on specific implementation but it is usually after user change value.
OnAcceptEdit
Occurs before Edit's value is transferred back to the cell. Var (in/out) parameters can be altered in order to prevent transfer of new value, or block user from exit editing (CanLeave parameter).
procedure TForm1.NextGrid61AcceptEdit(Sender: TObject; ACol, ARow: Integer;
var Text: WideString; var Accept, CanLeave: Boolean);
begin
// Don't allow empty text to be transfered.
if Trim(Text) = '' then
begin
Accept := False; // Value is not accepted
Text := 'default'; // Inject default value into edit
CanLeave := False; // Prevent leaving editing state
Beep; // Alert user with sound
end;
end;
OnAfterEdit
Occurs after In-place Edit's value is transferred back to cell. Now Cell property can be inspected for value.
OnEditExit
This event occurs even if edit wasn't successful (Accept parameter is set to False inside OnAcceptEdit event).