procedure MonEditKeyPress(Sender: TObject; var Key: Char); var Edt : TEdit absolute Sender; begin Edt.Text := 'Modif générale'; Edt.Color := ClBlue; end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin MonEditKeyPress(Sender, Key); end;
unit UMdf_Edit; interface uses System.Classes, Vcl.StdCtrls, System.SysUtils; Type edTypeEdit = (edNormal, edEntier, edReel); TEdit = class(Vcl.StdCtrls.TEdit) private FTypeEdit : edTypeEdit; procedure KeyPress(Sender: TObject; var Key: Char); public constructor Create(AOwner : TComponent); override; published property TypeEdit : edTypeEdit read FTypeEdit write FTypeEdit; end; implementation const Entiers = ['1'..'9', #8, '0', #13]; Reels = ['1'..'9', #8, ',', '0', '.', #13]; { TEdit } constructor TEdit.Create(AOwner : TComponent); begin // Creation composant // * Initialisation du type d'edit // * Dérivation contrôle saisie inherited Create(AOwner); FTypeEdit := edNormal; OnKeyPress := KeyPress; end; procedure TEdit.KeyPress(Sender: TObject; var Key: Char); var Autorized : set of Char; begin if FTypeEdit = edNormal then Exit; // >>> Saisie normale : Sortie // Choix des caractères autorisés case FTypeEdit of edEntier: Autorized := Entiers; edReel: Autorized := Reels; end; // Contrôle frappe autorisée if not (key in Autorized) then key := #0; // Cas particulier saisie réel if FTypeEdit = edReel then begin if Key in ['.', ','] then key := DecimalSeparator; end; end; end.
TEdit = class(UMdf_Edit.TEdit);
MonEdit.TypeEdit := edEntier;
unit UMdf_Edit; interface uses Classes, StdCtrls, SysUtils, Messages; Type edTypeEdit = (edNormal, edEntier, edReel); TEdit = class(StdCtrls.TEdit) private FTypeEdit : edTypeEdit; procedure WMChar(var Message: TWMChar); message WM_CHAR; public constructor Create(AOwner : TComponent); override; published property TypeEdit : edTypeEdit read FTypeEdit write FTypeEdit; end; implementation const Entiers = ['1'..'9', #8, '0', #13]; Reels = ['1'..'9', #8, ',', '0', '.', #13]; { TEdit } constructor TEdit.Create(AOwner : TComponent); begin // Creation composant // * Initialisation du type d'edit // * Dérivation contrôle saisie inherited Create(AOwner); FTypeEdit := edNormal; // OnKeyPress := KeyPress; end; procedure TEdit.WMChar(var Message: TWMChar); var Autorized : set of Char; begin if FTypeEdit = edNormal then inherited; // >>> Saisie normale : Traitement par défaut // Choix des caractères autorisés case FTypeEdit of edEntier: Autorized := Entiers; edReel: Autorized := Reels; end; // Contrôle frappe autorisée if Chr(Message.CharCode) in Autorized then begin // Cas particulier saisie réel if FTypeEdit = edReel then begin if Chr(Message.CharCode) in ['.', ','] then Message.CharCode := Ord(DecimalSeparator); end; inherited; end; end; end.
@+Cirec
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionprocedure TEdit.WMChar(var Message: TWMChar); var Autorized : set of Char; begin if FTypeEdit = edNormal then inherited; // >>> Saisie normale : Traitement par défaut // Choix des caractères autorisés case FTypeEdit of edEntier: Autorized := Entiers; edReel: Autorized := Reels; end; // Contrôle frappe autorisée if (FTypeEdit <> edNormal) // Contrôle si on est en mode restreint and (Chr(Message.CharCode) in Autorized) then begin // Cas particulier saisie réel if FTypeEdit = edReel then begin if Chr(Message.CharCode) in ['.', ','] then Message.CharCode := Ord(DecimalSeparator); end; inherited; end; end;
Type edTypeEdit = (edNormal, edEntier, edReel); TWinControl = class(Vcl.Controls.TControl) private FTypeEdit : edTypeEdit; procedure WMChar(var Message: TWMChar); message WM_CHAR; public constructor Create(AOwner : TComponent); override; published property TypeEdit : edTypeEdit read FTypeEdit write FTypeEdit; end; TCustomEdit = class(TWinControl); TEdit = class(TCustomEdit); TCustomListControl = class(TWinControl); TCustomCombo = class(TCustomListControl); TCustomComboBox = class(TCustomCombo); TComboBox = class(TCustomComboBox);
TWinControl = class(UMdf_CtrlSaisie.TWinControl);
unit UMdf_Edit; interface uses Classes, StdCtrls, SysUtils, Messages; Type edTypeEdit = (edNormal, edEntier, edReel); TEdit = class(StdCtrls.TEdit) private FTypeEdit : edTypeEdit; protected procedure KeyPress(var Key: Char); override; public constructor Create(AOwner : TComponent); override; published property TypeEdit : edTypeEdit read FTypeEdit write FTypeEdit; end; implementation const Entiers = ['1'..'9', #8, '0', #13]; Reels = ['1'..'9', #8, ',', '0', '.', #13]; { TEdit } constructor TEdit.Create(AOwner : TComponent); begin // Creation composant // * Initialisation du type d'edit // * Dérivation contrôle saisie inherited Create(AOwner); FTypeEdit := edNormal; end; procedure TEdit.KeyPress(var Key: Char); var Autorized : set of AnsiChar; begin { au début ******} inherited; // si OnKeyPress est affecté il est déclanché if FTypeEdit = edNormal then Exit; // >>> Saisie normale : Sortie // Choix des caractères autorisés case FTypeEdit of edEntier: Autorized := Entiers; edReel: Autorized := Reels; end; // Contrôle frappe autorisée if not {$ifdef UNICODE}CharInSet(key, Autorized){$else} (key in Autorized){$endif} then key := #0; // Cas particulier saisie réel if FTypeEdit = edReel then begin if {$ifdef UNICODE}CharInSet(key, ['.', ',']){$else} (key in ['.', ',']){$endif} then key := DecimalSeparator; end; { ****** ou en fin de traitement au choix} //inherited; // si OnKeyPress est affecté il est déclanché end; end.