Solved

MaskEdit control input and display control

Posted on 2004-09-09
7
4,165 Views
Last Modified: 2012-06-27
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
Comment
Question by:johan777
7 Comments
 
LVL 10

Expert Comment

by:Jacco
Comment Utility
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
 
LVL 7

Accepted Solution

by:
sftweng earned 300 total points
Comment Utility
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
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 200 total points
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 22

Expert Comment

by:Ferruccio Accalai
Comment Utility
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
 
LVL 7

Expert Comment

by:sftweng
Comment Utility
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
 
LVL 7

Expert Comment

by:sftweng
Comment Utility
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
 

Author Comment

by:johan777
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now