Solved

Formatting text in a TEdit

Posted on 2004-09-16
2
423 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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