Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

I need an OnExitCell event in TStringGrid

256 views
Skip to first unread message

Rolf Fankhauser

unread,
Dec 16, 1999, 3:00:00 AM12/16/99
to
I need to update the cell content of a string grid after exiting the
cell. Which event can I use? I tried OnSetEditText but this event is
fired at each change during editing and not only at exit (Is this a
bug?). I used OnSelectCell by saving column and row of the current
position and OnSelectCell and OnExit to update the cell of the old
position. This works but is not very elegant. I guess there is an easier
way to achieve the same result. How can I extend TStringGrid with an
OnExitCell event?

Thanks in advance for your help.

Rolf
--
----------------------------------------------------------------
Rolf Fankhauser, PhD
ETHZ - Federal Institute of Technology Zurich
Institute of Hydromechanics and Water Resources Management
Urban Water Management Division
G 31.3
CH-8093 Zuerich-Hoenggerberg
Switzerland
Tel. +41-1-633 25 07
Fax. +41-1-633 10 61

Peter Below (TeamB)

unread,
Dec 16, 1999, 3:00:00 AM12/16/99
to
In article <3858FD95...@eawag.ch>, Rolf Fankhauser wrote:
> I need to update the cell content of a string grid after exiting the
> cell. Which event can I use? I tried OnSetEditText but this event is
> fired at each change during editing and not only at exit (Is this a
> bug?). I used OnSelectCell by saving column and row of the current
> position and OnSelectCell and OnExit to update the cell of the old
> position. This works but is not very elegant. I guess there is an easier
> way to achieve the same result. How can I extend TStringGrid with an
> OnExitCell event?
>

This way:

unit PBExStringgrid;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
Grids;

const
GM_ACTIVATECELL = WM_USER + 123;

type
TGMActivateCell = record
msg: Cardinal;
aCol, aRow: Integer;
result: Integer;
end;

TPBExStringgrid = class;
TExitCellEvent = Procedure( Sender: TPBExStringgrid; aCol, aRow:
Integer;
Const edittext: String ) of Object;

TPBExStringgrid = class(Tstringgrid)
private
FExitCell: TExitCellEvent;

procedure GMActivateCell( var msg: TGMActivateCell ); message
GM_ACTIVATECELL;
protected
function CreateEditor: TInplaceEdit; override;
procedure ExitCell( const edittext: String; aCol, aRow: Integer );
virtual;
public
procedure ActivateCell( aCol, aRow: Integer );
published
property OnExitCell: TExitCellEvent read FExitCell write FExitCell;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('PBGoodies', [TPBExStringgrid]);
end;

Type
TExInplaceEdit = Class( TInplaceEdit )
private
FLastCol, FLastRow: Integer;

Procedure WMKillFocus( Var msg: TMessage ); message WM_KILLFOCUS;
Procedure WMSetFocus( Var msg: TMessage ); message WM_SETFOCUS;
end;
{ TPBExStringgrid }

procedure TPBExStringgrid.ActivateCell(aCol, aRow: Integer);
begin
PostMessage( handle, GM_ACTIVATECELL, aCol, aRow );
end;

function TPBExStringgrid.CreateEditor: TInplaceEdit;
begin
result := TExInplaceEdit.Create( self );
end;

procedure TPBExStringgrid.ExitCell(const edittext: String; aCol, aRow:
Integer );
begin
If Assigned( FExitCell ) Then
FExitCell( self, aCol, aRow, edittext );
end;

procedure TPBExStringgrid.GMActivateCell(var msg: TGMActivateCell);
begin
Col := msg.aCol;
Row := msg.aRow;
EditorMode := true;
InplaceEditor.SelectAll;
end;

{ TExInplaceEdit }

procedure TExInplaceEdit.WMKillFocus(var msg: TMessage);
begin
TPBExStringgrid(Grid).ExitCell( Text, FLastCol, FLastRow );
inherited;
end;

procedure TExInplaceEdit.WMSetFocus(var msg: TMessage);
begin
FLastCol := TPBExStringgrid(Grid).Col;
FLastRow := TPBExStringgrid(Grid).Row;
inherited;
end;

end.

CAVEAT: The OnExitCell event is triggered by a focus change so it has the
same problems as OnExit/OnEnter: you must not do anything in it that
causes another focus change (e.g. showing a message dialog). You can use
the grids ActivateCell method to reactivate the cell. When OnExitCell
fires the grids Col/Row properties already refer to the cell that will
become active.

Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!


0 new messages