Hi I am building a custom label that will accept variants as input rather than using StrToInt and floatToStrf all the time. The code below works fine if the label input is direct ie
Numlabel1.input=234.56;
but when the value is assigned to a variable
var
v : double;
...
v := 234.56;
numLabel.input := v;
It does not work
Here is part of my code. Can anyone point me in the right direction please?
procedure TNumLabel.SetInput(Value : Variant);
var
s:string;
begin
FInput := Value;
if VarIsType(FInput,256) = True then s:=FInput; //string
if VarIsType(FInput,17) = True then s:=IntToStr(FInput); //integer
if VarIsType(FInput,18) = True then s:=IntToStr(FInput); //word
if VarIsType(FInput,6) = True then //double
begin
GetDecimals; //get the number of becimal places user has selected
if FCurrency = True then s := FloatToStrF(FInput,ffCurrency,7,FDecimals) else
s:= FloatToStrF(FInput,ffNumber,7,FDecimals);
end;
if FPrefix<>'' then Caption:=FPrefix; //header
if s<>Null then Caption:=Caption+s+' ';
if FSuffix<>'' then if FInput<>Null then Caption:=Caption+FSuffix;
end;
As requested where is the whole code
unit NumLabel;
interface
uses WinTypes, WinProcs, Messages, SysUtils, Classes, Controls,
Forms, Graphics, Stdctrls, Variants, Dialogs, StrUtils, ESBRtns;
type
TNumLabel = class(TLabel)
private
FCurrency : Boolean;
FInput : Variant;
FDecimals : Integer;
FPrefix : string;
FSuffix : string;
FLayout : TTextLayout;
procedure AutoInitialize;
procedure AutoDestroy;
function GetCurrency : Boolean;
procedure SetCurrency(Value : Boolean);
function GetInput : Variant;
procedure SetInput(Value : Variant);
function GetPrefix : string;
procedure SetPrefix(Value : string);
function GetSuffix : string;
procedure SetSuffix(Value : string);
function GetDecimals : Integer;
function GetLayout : TTextLayout;
procedure SetLayout(Value : TTextLayout);
procedure SetDecimals(Value : Integer);
procedure WMSize(var Message: TWMSize); message WM_SIZE;
protected
{ Protected fields of TNumLabel }
{ Protected methods of TNumLabel }
procedure Click; override;
procedure Loaded; override;
procedure Paint; override;
public
procedure ChkPrefix(Astr:string);
{ Public fields and properties of TNumLabel }
{ Public methods of TNumLabel }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published properties of TNumLabel }
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property Currency : Boolean read GetCurrency write SetCurrency;
property Prefix : string read GetPrefix write SetPrefix;
property Suffix : string read GetSuffix write SetSuffix;
property Input : Variant read GetInput write SetInput;
property Decimals : Integer
read GetDecimals write SetDecimals
default 2;
property Layout : TTextLayout read FLayout write FLayout;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [TNumLabel]);
end;
procedure TNumLabel.AutoInitialize;
begin
FDecimals := 2;
end;
procedure TNumLabel.AutoDestroy;
begin
end;
function TNumLabel.GetLayout : TTextLayout;
begin
Result := GetLayout;
end;
procedure TNumLabel.SetLayout(Value : TTextLayout);
begin
Layout := Value;
end;
function TNumLabel.GetDecimals : Integer;
begin
Result := FDecimals;
end;
procedure TNumLabel.SetDecimals(Value : Integer);
begin
FDecimals := Value;
end;
function TNumLabel.GetCurrency : Boolean;
begin
Result := FCurrency;
end;
procedure TNumLabel.SetCurrency(Value : Boolean);
begin
FCurrency := Value;
end;
function TNumLabel.GetPrefix : string;
begin
ChkPrefix(FPrefix);
Result := FPrefix;
end;
procedure TNumLabel.SetPrefix(Value : string);
begin
FPrefix := Value;
GetInput;
GetSuffix;
ChkPrefix(FPrefix);
if FInput<>Null then Caption:=Caption+FInput+' ';
if FSuffix<>'' then if FInput<>Null then Caption:=Caption+FSuffix;
Invalidate;
end;
procedure TNumLabel.ChkPrefix(Astr:string);
begin
if Astr<>'' then
begin
if Layout=tlTop then
begin
if Pos(#$D#$A,FPrefix) = 0 then FPrefix:=FPrefix +#$D#$A ;
end
else if ((RightStr(FPrefix,1)=' ') and (Layout=tlCenter)) then FPrefix:=FPrefix+' ';
end;
end;
function TNumLabel.GetSuffix : string;
begin
Result := FSuffix;
end;
procedure TNumLabel.SetSuffix(Value : string);
begin
FSuffix :=Value;
GetPrefix;
GetInput;
if FPrefix<>'' then Caption:=FPrefix;
if FInput<>Null then Caption:=Caption+FInput+' ';
if FSuffix<>'' then if FInput<>Null then Caption:=Caption+FSuffix;
Invalidate;
end;
function TNumLabel.GetInput : Variant;
begin
Result := FInput;
end;
procedure TNumLabel.SetInput(Value : Variant);
var
s:string;
begin
FInput := Value;
if VarIsType(FInput,256) = True then s:=FInput;
if VarIsType(FInput,17) = True then s:=IntToStr(FInput);
if VarIsType(FInput,18) = True then s:=IntToStr(FInput);
if VarIsType(FInput,6) = True then
begin
GetDecimals;
if FCurrency = True then s := FloatToStrF(FInput,ffCurrency,7,FDecimals) else
s := FloatToStrF(FInput,ffNumber,7,FDecimals);
end;
if FPrefix<>'' then Caption:=FPrefix;
if s<>Null then Caption:=Caption+s+' ';
if FSuffix<>'' then if FInput<>Null then Caption:=Caption+FSuffix;
end;
procedure TNumLabel.Click;
begin
inherited Click;
end;
constructor TNumLabel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
AutoInitialize;
end;
destructor TNumLabel.Destroy;
begin
AutoDestroy;
inherited Destroy;
end;
procedure TNumLabel.Loaded;
begin
inherited Loaded;
end;
procedure TNumLabel.Paint;
begin
inherited Paint;
end;
procedure TNumLabel.WMSize(var Message: TWMSize);
var
W, H: Integer;
begin
inherited;
W := Width;
H := Height;
if (W <> Width) or (H <> Height) then
inherited SetBounds(Left, Top, W, H);
Message.Result := 0;
end;
end.