Solved

Formatting text in a TEdit

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

810 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