[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4526
  • Last Modified:

MaskEdit control input and display control

Hi all,

I need to allow a user to enter a value (which may contain up to a maximum of 3 digits after the decimal), but I need to then display it as a fixed 3 digits after the decimal as well as indicate the thousands seperator.

I am actually using Devexpress TcxMaskEdit (which allow using of regular expressions in the mask), but if you have any answer, I would like to know.

Example:

Input Value:                     Display Value (after input):
23                                   23.000
2134.34                           2,134.340

Regards,
Js
0
johan777
Asked:
johan777
2 Solutions
 
JaccoCommented:
We use the TurboPower Orpheus TOvcNumericField for that with a picture mask of #.###.###,### for that.

(The Orpheus library is open source on sourceforge now)

Regards Jacco
0
 
sftwengCommented:
How about this, no special components required:

unit uQ21125564;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Label1: TLabel;
    procedure Edit1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Edit1Change(Sender: TObject);
var
  inputString, outputString : String;
  realNumber : Real;
begin
  inputString := Edit1.Text;
  try
    realNumber := StrToFloat(inputString);
    CurrencyString := '';
    ThousandSeparator := ',';
    outputString := Format('%.3m',[realNumber]);
  except
    outputString := '??';
  end {try};
  Label1.Caption := outputString;
end;

end.
0
 
kretzschmarCommented:
if you need it for a database-entry,
then you could use a tdbedit-field directly bound to you field

to get the output in your format, simple assign a displayformat then like

//in the afteropen-event of your dataset use
  TFloatField(Dataset.FieldByName('FieldName')).DisplayFormat := '#,##0.000';

well, just in case that you need this for a database-entry

meikl ;-)

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
This is not a component solution, btw maybe can help.
I've this my old function to format digits...

function FormatDigits(Value: Extended;
                     Digits: Cardinal): String;
var
  Fmt: String;
begin
   Fmt := '#,###';
   If Digits > 0 then begin
      Fmt := Fmt+'.';
      while Digits > 1 do
         begin
            Fmt := Fmt+'#';
            dec(Digits);
         end;
      Fmt := Fmt+'0';
      end;
   Result := FormatFloat(Fmt,Value);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Showmessage(FormatDigits(1100.1258796,3));
Showmessage(FormatDigits(1100.1258796,5));
Showmessage(FormatDigits(1100.1,6));
Showmessage(FormatDigits(1100.1258796,0));
end;
0
 
sftwengCommented:
BTW, just to make the code a bit "cleaner", you might like to add:
    DecimalSeparator := '.';
and save the prior settings and then restore them in the Edit1Change event handler.
0
 
sftwengCommented:
Doh! This is a bit easier:

procedure TForm1.Edit1Change(Sender: TObject);
var
  inputString, outputString : String;
  realNumber : Real;
begin
  inputString := Edit1.Text;
  try
    realNumber := StrToFloat(inputString);
    outputString := Format('%.3n',[realNumber]);
  except
    outputString := '??';
  end {try};
  Label1.Caption := outputString;
end;
0
 
johan777Author Commented:
Hi all,

In short: The Devexpress component TMaskEdit does not have the ability to accommodate the thousands seperator, but their TCurrencyEdit component does. So, I decided to use this component and delete the currency symbol which then gives me exactly what I was looking for.

Thanx for all the help! I'm going to allocate the points based on the excellent alternative solutions I received.

Regards,
Johan Swart

PS: Sorry for only getting around to resolve this now, but the project I'm on was keeping me a bit busy ;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now