Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Formatting text in a TEdit

Posted on 2004-09-16
2
Medium Priority
?
457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

721 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