Unit RealEdit;
interface
uses
Windows, Messages, SysUtils,
Variants, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls,ExtCtrls;
procedure Register;
type
TRealEdit = class(TCustomEdit)
private
fAlignment:TAlignment;
fOnlyReals:boolean;
fLabel:TLabel;
procedure SetAlignment(Value:TAlignment);
procedure SetOnlyReals(Value:boolean);
protected
procedure SetLabel(Value:TLabel);
procedure CreateParams(var Params:TCreateParams); override;
procedure KeyPress(var Key:Char); override;
public
constructor Create(AOwner: TComponent); override;
destructor destroy; override;
published
property Alignment: TAlignment read fAlignment write SetAlignment;
property OnlyReals: Boolean read fOnlyReals write SetOnlyReals;
property RealLabel: TLabel read fLabel write SetLabel;
end;
implementation
procedure TRealEdit.SetLabel (Value : TLabel);
begin
fLabel := value;
flabel.SetSubComponent(true);
end;
constructor TRealEdit.Create(AOwner: TComponent);
begin
inherited;
FAlignment := taRightJustify;
OnlyReals := true;
FLabel := TLabel.Create(AOwner);
// FLabel.Parent := AOwner;
FLabel.Name := 'RealLabel';
FLabel.SetSubComponent(true);
FLabel.Caption := 'CAPTION';
end;
destructor TRealEdit.Destroy;
begin
FLabel.Destroy;
inherited;
end;
procedure TRealEdit.CreateParams(var Params: TCreateParams);
const Alignments: array[TAlignment] of byte=(ES_Left, ES_Right, ES_Center);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or Alignments[fAlignment];
fAlignMent := taRightJustify;
end;
procedure TRealEdit.KeyPress(var Key:Char);
var ZulZeichen : SET OF char;
begin
inherited KeyPress(Key);
if OnlyReals then begin
ZulZeichen := ['0'..'9',#8];
if pos (DecimalSeparator,text) = 0 THEN // kein . im RealEdit.text . zulassen
ZulZeichen := ZulZeichen + [DecimalSeparator]
else
ZulZeichen := ZulZeichen - [DecimalSeparator]; // zweiten . verhindern
if not (Key in ZulZeichen) then
Key := #0;
end;
end;
procedure TRealEdit.SetAlignment (value:TAlignment);
begin
fAlignment:=value;
RecreateWnd;
end;
procedure TRealEdit.SetOnlyReals (value:boolean);
begin
fOnlyReals:=value;
RecreateWnd;
end;
procedure Register;
begin
RegisterComponents('Eigene', [TRealEdit]);
end;
end.