home *** CD-ROM | disk | FTP | other *** search
- NUMBER : 2967
- PRODUCT : Delphi
- VERSION : All
- OS : Windows/Win32
- DATE : June 14, 1996
-
- TITLE : Validating input in TEdit components
-
- Q: How can I validate input in my TEdit components?
-
- A: Assuming you're using regular TEdit components (as opposed to TDBEdit
- components), the best place to validate input is the OnExit event of
- the TEdit. This event fires whenever focus leaves the component.
-
- Typically, when the user enters some invalid text into the control,
- you want to issue a warning to the user, and set the focus back
- to the control. A difficulty arises, however, when you attempt
- to set focus to a particular control from an OnExit event handler.
- Since Windows is "in the middle of" setting focus from one control
- to another during OnExit, you will see irregular behavior from
- controls if you attempt to change focus at that time.
-
- The solution is to post a message to your form in the TEdit's OnExit
- event handler. This user-defined posted message will indicate that
- the coast is clear to begin validating input. Since posted messages
- are placed at the end of the message queue, this gives Windows the
- opportunity to complete the focus change before you attempt to
- change the focus back to another control.
-
- Attatch is a unit and text representation of a DFM (form) file
- which demostrates this technique.
-
- { *** BEGIN CODE FOR UNIT1.PAS *** }
- unit Unit1;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, Mask;
-
- const
- { User-defined message }
- um_ValidateInput = wm_User + 100;
-
- type
- TForm1 = class(TForm)
- Edit1: TEdit;
- Edit2: TEdit;
- Edit3: TEdit;
- Edit4: TEdit;
- Button1: TButton;
- MaskEdit1: TMaskEdit;
- procedure Edit1Exit(Sender: TObject);
- private
- { User-defined message handler }
- procedure ValidateInput(var M: TMessage); message um_ValidateInput;
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- procedure TForm1.ValidateInput(var M: TMessage);
- begin
- { The following line is my validation. I want to make sure the }
- { first character is a lower case alpha character. Note the }
- { typecast of lParam to a TEdit. }
- if not (TEdit(M.lParam).Text[1] in ['a'..'z']) then begin
- ShowMessage('Bad input'); { Yell at the user }
- TEdit(M.lParam).SetFocus; { Set focus back }
- end;
- end;
-
- procedure TForm1.Edit1Exit(Sender: TObject);
- begin
- { Post a message to myself which indicates it's time to }
- { validate the input. Pass the TEdit instance (Self) as }
- { the message lParam. }
- PostMessage(Handle, um_ValidateInput, 0, longint(Sender));
- end;
-
- end.
- { *** END CODE FOR UNIT1.PAS *** }
-
- { *** BEGIN CODE FOR UNIT1.DFM *** }
- object Form1: TForm1
- Left = 200
- Top = 99
- Width = 318
- Height = 205
- Caption = 'Form1'
- Font.Color = clWindowText
- Font.Height = -13
- Font.Name = 'System'
- Font.Style = []
- PixelsPerInch = 96
- TextHeight = 16
- object Edit1: TEdit
- Left = 32
- Top = 32
- Width = 121
- Height = 24
- TabOrder = 0
- Text = 'Edit1'
- OnExit = Edit1Exit
- end
- object Edit2: TEdit
- Left = 160
- Top = 32
- Width = 121
- Height = 24
- TabOrder = 1
- Text = 'Edit2'
- OnExit = Edit1Exit
- end
- object Edit3: TEdit
- Left = 32
- Top = 64
- Width = 121
- Height = 24
- TabOrder = 2
- Text = 'Edit3'
- OnExit = Edit1Exit
- end
- object Edit4: TEdit
- Left = 160
- Top = 64
- Width = 121
- Height = 24
- TabOrder = 3
- Text = 'Edit4'
- OnExit = Edit1Exit
- end
- object Button1: TButton
- Left = 112
- Top = 136
- Width = 89
- Height = 33
- Caption = 'Button1'
- TabOrder = 4
- end
- end
- { *** END CODE FOR UNIT1.DFM *** }
-
-
- DISCLAIMER: You have the right to use this technical information
- subject to the terms of the No-Nonsense License Statement that
- you received with the Borland product to which this information
- pertains.
-