Board index » delphi » Error handling on invalid input DBEdit, part II

Error handling on invalid input DBEdit, part II

Hello,

First, thanks for the help I already got on this subject.

I could use the BeforePost handler to check the fieldvalue of a certain
TField. However the BeforePost handler is called only when Post is called
(automatically or by your own code). This means that when skipping from
DBEdit1 to DBEdit2, Post and BeforePost are not called.However when skipping
from DBEdit1 to DBEdit2, the input for DBEdit1 is checked. Let's say that
the field behind DBEdit1 is of type TDate. If you enter a wrong date, an
errormessage will be displayed, when skipping from DBEdit1 to DBEdit2. This
error I would like to intercept.

How can I do this?

Roy

 

Re:Error handling on invalid input DBEdit, part II


Roy,

You can set a mask for the field.
Open the FieldsEditor by rightclicking on the table component.
Select the fields you need.
Set the Editmask and DisplayFormat in the object-inspector.

Success,
Alfred.

Roy Rutten <no.junkmail.roy.rut...@xarmac.nl> schreef in berichtnieuws
7umb9u$1a2a...@reader3.wxs.nl...

Quote
> Hello,

> First, thanks for the help I already got on this subject.

> I could use the BeforePost handler to check the fieldvalue of a certain
> TField. However the BeforePost handler is called only when Post is called
> (automatically or by your own code). This means that when skipping from
> DBEdit1 to DBEdit2, Post and BeforePost are not called.However when
skipping
> from DBEdit1 to DBEdit2, the input for DBEdit1 is checked. Let's say that
> the field behind DBEdit1 is of type TDate. If you enter a wrong date, an
> errormessage will be displayed, when skipping from DBEdit1 to DBEdit2.
This
> error I would like to intercept.

> How can I do this?

> Roy

Re:Error handling on invalid input DBEdit, part II


Quote
Roy Rutten <no.junkmail.roy.rut...@xarmac.nl> wrote in message

news:7umb9u$1a2a7$1@reader3.wxs.nl...

Quote
> Let's say that
> the field behind DBEdit1 is of type TDate. If you enter a wrong date, an
> errormessage will be displayed, when skipping from DBEdit1 to DBEdit2.
This
> error I would like to intercept.

I guess there's a number of ways of tackling this, but here's one
suggestion. Handle the OnSetText event of the TField object. This means you
have to create persistent fields, at least for the fields you want to check
(see help for TField).

procedure TForm1.Table1ArtworkreceivedSetText(Sender: TField;
  const Text: string);
begin
  try
    TDateField(Sender).Value := StrToDate(Text);
  except
    ShowMessage('Wrong');
    // set the focus back to the edit control
    Perform(WM_NEXTDLGCTL, -1, 0);
  end;
end;

--
Stephen Brown

Re:Error handling on invalid input DBEdit, part II


Quote
Stephen Brown <s...@stephenbrown.co.uk> wrote in message

news:7uo2hl$2us$5@gxsn.com...

Quote
> procedure TForm1.Table1ArtworkreceivedSetText(Sender: TField;
>   const Text: string);
> begin
>   try
>     TDateField(Sender).Value := StrToDate(Text);
>   except
>     ShowMessage('Wrong');
>     // set the focus back to the edit control
>     Perform(WM_NEXTDLGCTL, -1, 0);
>   end;
> end;

On reflection, this is not a good way to do it. If you don't move to the
next control in tab order, Perform puts the focus on the wrong control.
However, I think the priciple of handling validation at the TField level is
correct: the rules should be applied irrespective of how the value of the
field is modified - therefore should not be placed on the user interface.

The following would be better, I think. It has the advantage that the focus
returns to the DBEdit where the wrong value was entered, and it will also
trap invalid values regardless of how the field is modified.

procedure TForm1.FormCreate(Sender: TObject);
begin
 Application.OnException := AppException;
end;

procedure TForm1.AppException(Sender: TObject; E: Exception);
begin
 if E is EConvertError then
   ShowMessage('Error: ' + E.Message);  // or whatever message you want
end;

--
Stephen Brown

Other Threads