Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2009-11

Question about SetFocus in a DoExit method (TMaskEdit related)

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bart

Hi there,

I'm running into a problem with TMaskEdit (I'm trying to maintain maskedit..pp)

Desired behaviour (as in Delphi):
When user leaves the control and validation failes (and the according exception is raised), two things must happen:
1. the control must get focus again
2. while resetting the focus to the control the value of FTextOnEnter must _not_ be updated (so that Reset resets the control in a state wher no validation is performed upon lossing focus)

I tried to achieve this using a boolean FValidationFailed, set in DoExit, and examine this in DoEnter (where FTextOnEnter is set).

See the code below (my working copy)

procedure TCustomMaskEdit.DoEnter;
begin
  inherited DoEnter;
  if isMasked then
  begin
    debugln('TCustomMaskEdit.DoEnter: FValidationFailed =
',DbgS(FValidationFailed));
    FCursorPos := GetSelStart;
    //Only save FTextOnEnter if validation did not fail in last DoExit
that occurred
    if not FValidationFailed then
      FTextOnEnter := Inherited Text
    else
      FValidationFailed := False;
   <snipped irrelevant code>
end;
procedure TCustomMaskEdit.DoExit;
begin
  debugln('TCustomMaskEdit.DoExit: FValidationFailed =
',DbgS(FValidationFailed));
  //First give OnExit a change to prevent a EDBEditError
  inherited DoExit;
  {$IFNDEF MASKEDIT_NOVALIDATEONEXIT}
  //Do not validate if FValidationFailed, or risk raising an exception
while the previous exception was
  //not handled, resulting in an application crash
  if IsMasked and (FTextOnEnter  Inherited Text) and (not
FValidationFailed) then
  begin
    //assume failure
    try
      //debugln('TCustomMaskedit.DoExit: try ValidateEdit');
      FValidationFailed := True;
      ValidateEdit;
      FValidationFailed := False;
    finally
      if FValidationFailed then
      begin
        //debugln('TCustomMaskedit.DoExit: Validation failed');
        SetFocus;
        SelectAll;
      end;
    end;
  end;
  {$ENDIF}
end;

This functions just fine, as long as the user uses the mouse to leave the control, if the user presses Tab things go wrong:

Here's the sequence of events when using the mouse
1. DoExit, FValidationFailed = False
2. Default ExceptionHandler kicks in with a showmessage
3. DoEnter, FValidationFailed = True

Now with the user pressing Tab
1. DoExit, FValidationFailed = False
2. DoEnter, FValidationFailed = True
3. Default ExceptionHandler kicks in with a showmessage
4. DoEnter, FValidationFailed = False

In the latter case, the second DoEnter will set FTextOnEnter because FValidationFailed now is False and Reset does not work properly.

Has anyone of you an idea how to solve this?

Thanks,

Bart

Reply by email

Daniel Simoes de Ameida
to Bart

Hi Bart,

I did report a Bug like this on Lazarus BugTracker

[]s Daniel

Conhe?a o Projeto ACBr - Automa?ao Comercial Brasil DJSystem a Loja Patrocinadora do ACBr

Reply by email