home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1996 December / CD_shareware_12-96.iso / DOS / Programa / TI2967.ZIP / TI2967.ASC < prev   
Encoding:
Text File  |  1996-06-17  |  4.1 KB  |  154 lines

  1.    NUMBER  :  2967
  2.   PRODUCT  :  Delphi
  3.   VERSION  :  All
  4.        OS  :  Windows/Win32
  5.      DATE  :  June 14, 1996                           
  6.  
  7.     TITLE  :  Validating input in TEdit components
  8.  
  9. Q: How can I validate input in my TEdit components?
  10.  
  11. A: Assuming you're using regular TEdit components (as opposed to TDBEdit
  12.    components), the best place to validate input is the OnExit event of
  13.    the TEdit.  This event fires whenever focus leaves the component.
  14.    
  15.    Typically, when the user enters some invalid text into the control,
  16.    you want to issue a warning to the user, and set the focus back
  17.    to the control.  A difficulty arises, however, when you attempt
  18.    to set focus to a particular control from an OnExit event handler.
  19.    Since Windows is "in the middle of" setting focus from one control
  20.    to another during OnExit, you will see irregular behavior from
  21.    controls if you attempt to change focus at that time.
  22.  
  23.    The solution is to post a message to your form in the TEdit's OnExit
  24.    event handler.  This user-defined posted message will indicate that
  25.    the coast is clear to begin validating input.  Since posted messages
  26.    are placed at the end of the message queue, this gives Windows the
  27.    opportunity to complete the focus change before you attempt to
  28.    change the focus back to another control.
  29.  
  30.    Attatch is a unit and text representation of a DFM (form) file
  31.    which demostrates this technique.
  32.  
  33. { *** BEGIN CODE FOR UNIT1.PAS *** }
  34. unit Unit1;
  35.  
  36. interface
  37.  
  38. uses
  39.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  40.   Forms, Dialogs, StdCtrls, Mask;
  41.  
  42. const
  43.   { User-defined message }
  44.   um_ValidateInput = wm_User + 100;
  45.  
  46. type
  47.   TForm1 = class(TForm)
  48.     Edit1: TEdit;
  49.     Edit2: TEdit;
  50.     Edit3: TEdit;
  51.     Edit4: TEdit;
  52.     Button1: TButton;
  53.     MaskEdit1: TMaskEdit;
  54.     procedure Edit1Exit(Sender: TObject);
  55.   private
  56.     { User-defined message handler }
  57.     procedure ValidateInput(var M: TMessage); message um_ValidateInput;
  58.   end;
  59.  
  60. var
  61.   Form1: TForm1;
  62.  
  63. implementation
  64.  
  65. {$R *.DFM}
  66.  
  67. procedure TForm1.ValidateInput(var M: TMessage);
  68. begin
  69.   { The following line is my validation.  I want to make sure the }
  70.   { first character is a lower case alpha character.  Note the    }
  71.   { typecast of lParam to a TEdit. }
  72.   if not (TEdit(M.lParam).Text[1] in ['a'..'z']) then begin
  73.     ShowMessage('Bad input');              { Yell at the user }
  74.     TEdit(M.lParam).SetFocus;              { Set focus back   }
  75.   end;
  76. end;
  77.  
  78. procedure TForm1.Edit1Exit(Sender: TObject);
  79. begin
  80.   { Post a message to myself which indicates it's time to  }
  81.   { validate the input.  Pass the TEdit instance (Self) as }
  82.   { the message lParam. }
  83.   PostMessage(Handle, um_ValidateInput, 0, longint(Sender));
  84. end;
  85.  
  86. end.
  87. { *** END CODE FOR UNIT1.PAS *** }
  88.  
  89. { *** BEGIN CODE FOR UNIT1.DFM *** }
  90. object Form1: TForm1
  91.   Left = 200
  92.   Top = 99
  93.   Width = 318
  94.   Height = 205
  95.   Caption = 'Form1'
  96.   Font.Color = clWindowText
  97.   Font.Height = -13
  98.   Font.Name = 'System'
  99.   Font.Style = []
  100.   PixelsPerInch = 96
  101.   TextHeight = 16
  102.   object Edit1: TEdit
  103.     Left = 32
  104.     Top = 32
  105.     Width = 121
  106.     Height = 24
  107.     TabOrder = 0
  108.     Text = 'Edit1'
  109.     OnExit = Edit1Exit
  110.   end
  111.   object Edit2: TEdit
  112.     Left = 160
  113.     Top = 32
  114.     Width = 121
  115.     Height = 24
  116.     TabOrder = 1
  117.     Text = 'Edit2'
  118.     OnExit = Edit1Exit
  119.   end
  120.   object Edit3: TEdit
  121.     Left = 32
  122.     Top = 64
  123.     Width = 121
  124.     Height = 24
  125.     TabOrder = 2
  126.     Text = 'Edit3'
  127.     OnExit = Edit1Exit
  128.   end
  129.   object Edit4: TEdit
  130.     Left = 160
  131.     Top = 64
  132.     Width = 121
  133.     Height = 24
  134.     TabOrder = 3
  135.     Text = 'Edit4'
  136.     OnExit = Edit1Exit
  137.   end
  138.   object Button1: TButton
  139.     Left = 112
  140.     Top = 136
  141.     Width = 89
  142.     Height = 33
  143.     Caption = 'Button1'
  144.     TabOrder = 4
  145.   end
  146. end
  147. { *** END CODE FOR UNIT1.DFM *** }
  148.  
  149.  
  150. DISCLAIMER: You have the right to use this technical information
  151. subject to the terms of the No-Nonsense License Statement that
  152. you received with the Borland product to which this information
  153. pertains.
  154.