Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Formatting text in a TEdit

Posted on 2004-09-16
2
Medium Priority
?
472 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 500 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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

783 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