Tworzę własną kontrolkę. I mam problem z zdarzenie KeyPress.
Kod kontrolki:
unit EditValidateControls;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TeditValidate = class(TFrame)
PanelColorError: TPanel;
EditText: TEdit;
LabelError: TLabel;
procedure EditTextKeyPress(Sender: TObject; var Key: Char);
private
FColorError: TColor;
FColorNormal: TColor;
FtextError: string;
Ferror: Boolean;
public
property ColorError: TColor read FColorError write FColorError;
property ColorNormal: TColor read FColorNormal write FColorNormal;
property textError: string read FtextError write FtextError;
property error: Boolean read Ferror write Ferror;
constructor Create(AOwner:TComponent); overload;
{ Public declarations }
end;
implementation
{$R *.dfm}
constructor TeditValidate.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
EditText.OnKeyPress := EditTextKeyPress;
end;
procedure TeditValidate.EditTextKeyPress(Sender: TObject; var Key: Char);
begin
inherited;
if (error) then
begin
PanelColorError.Color := FColorError;
LabelError.Caption := FtextError;
width := LabelError.Canvas.TextWidth(FtextError) + LabelError.Left + 100;
end
else
begin
PanelColorError.Color := FColorNormal;
LabelError.Caption := '';
width := LabelError.Canvas.TextWidth('') + LabelError.Left + 100;
end;
end;
end.
object EditValidate: TEditValidate
Left = 0
Top = 0
Width = 320
Height = 42
Anchors = [akLeft, akTop, akBottom]
TabOrder = 0
DesignSize = (
320
42)
object LabelError: TLabel
Left = 185
Top = 6
Width = 3
Height = 13
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = [fsBold]
ParentFont = False
end
object PanelColorError: TPanel
Left = 0
Top = 0
Width = 177
Height = 41
Margins.Left = 5
Margins.Top = 5
Margins.Right = 5
Margins.Bottom = 5
Anchors = [akLeft, akTop, akBottom]
BevelOuter = bvNone
Color = clBackground
ParentBackground = False
TabOrder = 0
object EditText: TEdit
AlignWithMargins = True
Left = 3
Top = 3
Width = 171
Height = 35
Align = alClient
TabOrder = 0
OnKeyPress = EditTextKeyPress
ExplicitHeight = 21
end
end
end
Procedura rejestrująca
unit Registers;
interface
uses
EditValidateControls;
procedure Register;
implementation
uses
Classes;
procedure Register;
begin
RegisterComponents('EditValide', [TeditValidate]);
end;
end.
I teraz np. na formie w zdarzeniu KeyPress mam taki kod
procedure TForm73.editValidate2KeyPress(Sender: TObject; var Key: Char);
begin
editValidate1.error := true;
editValidate1.textError := 'Jakiś tekst';
editValidate1.ColorError := clRed;
end;
Niestety zdarzenie editValidate2KeyPress w ogóle się nie wywołuje.