Solved

Formatting text in a TEdit

Posted on 2004-09-16
2
416 Views
Last Modified: 2010-04-05


Hi,
I have a TEdit and I want to format (every time the user type a number) the amount the user enter ( only numbers are allowed ).
Example : The user
enter 100, text in TEdit is 100
enter 1000, text in TEdit is 1,000
enter 10000, text in TEdit is 10,000
enter 100000, text in TEdit is 100,000
etc.

How can I do that ?
Thanks
0
Comment
Question by:frdep
2 Comments
 
LVL 3

Expert Comment

by:dlengel
ID: 12079851
Create a new application, and add a new TEdit component called Edit1 to Form1.  

Add a private boolean variable to the forms declaration:
    LockChanges: Boolean;
Create a method for the OnChange event, and replace it with the following code:

procedure TForm1.Edit1Change(Sender: TObject);
  var OldStart, OldEnd, Increment: Integer;
      TempString: string;
begin
  //We only want on instance of this method to be called.  Since
  //Edit1.Text is modified by this method, a recursive stack overflow
  //will occur if we do not lock changes until we are completely done
  if not LockChanges then begin
    LockChanges := True;
    //save selection endpoints so they can be restored later
    OldStart := Edit1.SelStart;
    OldEnd := OldStart + Edit1.SelLength;
    //strip existing commas from the text string
    //if the change the selection endpoints, update them also
    for Increment := Length(Edit1.Text) downto 1 do
      if Edit1.Text[Increment] = ',' then begin
        if Increment < OldStart then
          Dec(OldStart);
        if Increment < OldEnd then
          Dec(OldEnd);
      end;
    //now it's more efficient to delete the old commas all at once,
    //since the endpoint indexes have been adjusted correctly.
    Edit1.Text := AnsiReplaceStr(Edit1.Text, ',', '');
    //working backwards, add commas into the text string again,
    //this time in the correct locations.
    Increment := Length(Edit1.Text) - 3;
    while Increment > 0 do begin
      Edit1.Text := Copy(Edit1.Text, 1, Increment) + ',' + Copy(Edit1.Text, Increment + 1, MaxInt);
      if Increment < OldStart then
        Inc(OldStart);
      if Increment < OldEnd then
        Inc(OldEnd);
      Dec(Increment, 3);
    end;
    //restore the selection endpoints like they were before
    Edit1.SelStart := OldStart;
    Edit1.SelLength := OldEnd - OldStart;
    //since we won't change Edit.Text any more, it is safe to unlock
    LockChanges := False;
  end;
end;
0
 
LVL 9

Accepted Solution

by:
ginsonic earned 125 total points
ID: 12096256
type
  TCharS = Set of Ansichar;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    procedure Edit1Change(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure RemoveCharacters( Var S: AnsiString; Const characters: TCharS );
var
  i: Integer;
begin
  for i := Length(S) Downto 1 do
    if S[i] in characters then
      delete(S, i, 1 );
end;

procedure TForm1.Edit1Change(Sender: TObject);
var s:string;
begin
  s:=Edit1.Text;
  RemoveCharacters( s, [','] );
  Edit1.Text:= FormatFloat( '#,##0', StrToFloat(s));
  Edit1.SelStart:=Length(Edit1.Text);
  Edit1.SelLength:=0;
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
 if Length(Edit1.Text)>22 then key:=#0;
 if not (Key in [#8,'0'..'9'{,',','.'}]) then Key := #0;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ThousandSeparator:=',';
end;

end.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 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

19 Experts available now in Live!

Get 1:1 Help Now