Subject Re: [IBO] Re: IB_Grid, pressing enter at last column
Author Helen Borrie
At 12:39 PM 3/03/2006, you wrote:

>I do not want to Cancel the edit state, just prevent posting so that
>the user gets the chance to correct mistakes (And no need to re-enter
>the data). This is the general outline of my beforepost event:
> if mycondition_is_true then begin
> MessageDlg('Row requires this data.',mtWarning,[mbok],0);
> query1.fieldbyname('REQUIREDFIELD').focuscontrol;
> Sysutils.Abort;
> end;
>while testing I trimmed it down to:
> Sysutils.Abort;
>Both code works as intended if I press up or down or click on a
>different row, but an exception is thrown if I press Enter at the end
>column. (You have already explained this to me). Now if I try to
>catch the keypress at the ib_grid's onkeypress event, it appears that
>ib_grid was already through with the #13 first hand before my own
>onkeypress gets the chance to process the keypress.
> > If the current row has any modified fields then pressing the up or
> > down key will cause the Post event to fire; but not otherwise. If
> > Abort gets called (due to changes in the row) then the BeforePost
> > will be visited and your Abort call will happen. However, the
> > execution path for an up or down key press is never going to end up
> > with closing a form since they are incapable of that. Enter will
> > it if there is no trap for it.
>So it is by design. I guess I have explained what I really wanted to
>do. Can you please give me an idea on how or where to trap the enter
>key so that I can prevent it from closing the window?

OK, TIB_Grid is a TCustomGrid descendant. Its OnKeyPress event is
too late to pre-empt the inherited behaviour of OnKeyDown, so
intervene at OnKeyDown instead. Test your conditions and, if the
dataset is in dssEdit and and grid's (Col = LastTabCol) then pass
your "safe" key back to the Key var.

The dataset won't go out of Edit state then, and the Post event won't
be called until the grid responds to the VK_UP or VK_DOWN.