unit NumberEdit;
interface
uses
System.SysUtils, System.Classes,System.Character,
FGX.Edit,FGX.Text, FGX.Types,FGX.Consts;
type
[ComponentPlatformsAttribute(fgMobilePlatforms)]
TfgEditNumber = class(TfgEdit)
private
FDecimalSeparator: Char;
FThousandSeparator: Char;
FCurrencySymbol: string;
FAllowDecimal: Boolean;
procedure FormatNumber;
procedure DoTextChanged(Sender: TObject);
function CleanNumber(const S: string): string;
public
constructor Create(AOwner: TComponent); override;
property ThousandSeparator: Char read FThousandSeparator write FThousandSeparator;
property DecimalSeparator: Char read FDecimalSeparator write FDecimalSeparator;
property CurrencySymbol: string read FCurrencySymbol write FCurrencySymbol;
property AllowDecimal: Boolean read FAllowDecimal write FAllowDecimal;
end;
implementation
uses FGX.ClassStorage;
{ TfgEditNumber }
constructor TfgEditNumber.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Self.OnChanged := DoTextChanged;
Self.KeyboardType := TfgKeyboardType.NumbersAndPunctuation;
// Default Settings
FThousandSeparator := ','; // Default: Comma separator
FDecimalSeparator := '.'; // Default: Dot as decimal
FCurrencySymbol := ''; // No currency by default
FAllowDecimal := True; // Allow decimal numbers
end;
// Remove Non-Numeric Characters Except Decimal Point
function TfgEditNumber.CleanNumber(const S: string): string;
var
C: Char;
DecimalCount: Integer;
begin
Result := '';
DecimalCount := 0;
for C in S do
begin
if C.IsDigit then
Result := Result + C
else if (C = FDecimalSeparator) and (FAllowDecimal) and (DecimalCount = 0) then
begin
Result := Result + C;
Inc(DecimalCount);
end;
end;
end;
// Format Number with Custom Separators and Currency
procedure TfgEditNumber.FormatNumber;
var
NumStr, FormattedStr: string;
Value: Double;
DecimalPart: string;
begin
NumStr := CleanNumber(Self.Text);
if NumStr.IsEmpty then Exit;
if FAllowDecimal and (Pos(FDecimalSeparator, NumStr) > 0) then
begin
DecimalPart := Copy(NumStr, Pos(FDecimalSeparator, NumStr), MaxInt);
NumStr := Copy(NumStr, 1, Pos(FDecimalSeparator, NumStr) - 1);
end
else
DecimalPart := '';
if TryStrToFloat(NumStr, Value) then
FormattedStr := FormatFloat('#' + FThousandSeparator + '##0', Value)
else
FormattedStr := NumStr;
FormattedStr := FCurrencySymbol + FormattedStr + DecimalPart;
if Self.Text <> FormattedStr then
begin
Self.OnChanged := nil; // Prevent recursion
Self.Text := FormattedStr;
Self.OnChanged := DoTextChanged;
end;
end;
// Handle Text Change Event
procedure TfgEditNumber.DoTextChanged(Sender: TObject);
begin
FormatNumber;
end;
initialization
TfgClassStorage.RegisterClasses([TfgEditNumber]);
end.
I got this error on this line : LEdit:=TfgEditNumber.Create(Self);