?
Solved

Mask/Format text during typing

Posted on 2005-04-22
12
Medium Priority
?
470 Views
Last Modified: 2011-10-03
I have an edit box. While the user is entering numbers, I want dashes inserted at certain points, and a limit on how many numbers can be entered.

IE, I want it to end up like this:

9822-00-234-2345


So the dashes are added while the user is typing the number, and only 13 digits can be entered. They also need to be able to delete, and the dashes must remain in the correct places.


I'm totally lost on this. Any tips?
0
Comment
Question by:jlinke
  • 6
  • 2
  • 2
  • +2
12 Comments
 
LVL 11

Accepted Solution

by:
pcsentinel earned 1000 total points
ID: 13841937
Why not just use a masked edit control?

or you can use this

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
      if Length(Edit1.Text) in [4,8] then
  begin
        Edit1.Text:=Edit1.Text+'-';
    Edit1.SelStart:=Length(Edit1.Text);
  end;
end;

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key=VK_BACK) and (Copy(Edit1.Text,Length(Edit1.Text),1)='-') then
  begin
        Edit1.Text:=Copy(Edit1.Text,1,pred(Length(Edit1.Text)));
    Edit1.SelStart:=Length(Edit1.Text);
  end

end;


regards
0
 
LVL 11

Expert Comment

by:pcsentinel
ID: 13841943
Sorry you need to add 12 to your set i.e.

if Length(Edit1.Text) in [4,8,12] then

regards
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13842025
He will also need to make sure the length does not go over 16 ... 13 digits plus 3 '-' signs

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Length(Edit1.Text) > 16 then
    Key := 0;
end;

or something like that
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
LVL 11

Expert Comment

by:pcsentinel
ID: 13842065
Thanks mike forgot about the last bit

0
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 13842110
Surely you would also need to limit which keys may be pressed?

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
   if not (Key in [#8, '0'..'9',#13]) then // you may also want to add the arrow keys
   begin
    beep;
    Result := #0;
   end;
end;
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13842155
Another good point David, forgot he only wanted numbers as well.
0
 
LVL 17

Expert Comment

by:geobul
ID: 13843306
Hi,

Try TMaskEdit component with the following EditMask property value '!0000-00-000-0000;1; '.

Regards, Geo
0
 
LVL 11

Expert Comment

by:pcsentinel
ID: 13844211
Geo, the only thing with maskedit is that the dashes are in there permanantly not integrated during typing

0
 
LVL 5

Expert Comment

by:Voodooman
ID: 13844952

Watching

How bad are masked edit controls............hang your head in shame Bill Gates.......

Masked Edit was introuduced in VB3/Win95, I never saw a good one......


Voodooman
0
 
LVL 17

Expert Comment

by:geobul
ID: 13845000
pcsentinel, have you tried yours at all? Just try to delete using Backspace before '-' char.

I think I understood the requrements. Here is my version (not perfect though):

TForm1 = class(TForm)
    Edit1: TEdit;
    procedure Edit1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Edit1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    bkp: boolean;
    del: boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_BACK then bkp := true
  else bkp := false;
  if Key = VK_DELETE then del := true
  else del := false;
end;

procedure TForm1.Edit1Change(Sender: TObject);
var
  s: string;
  i,k: integer;
begin
  k := Edit1.SelStart;
  s := StringReplace(Edit1.Text, '-', '', [rfReplaceAll]);
  i := Length(s);
  if i > 13 then begin
    i := 13;
    s := Copy(s,1,13);
  end;
  if i > 4 then begin
    s := Copy(s,1,4)+'-'+Copy(s,5,i);
    i := i + 1;
  end;
  if i > 7 then begin
    s := Copy(s,1,7)+'-'+Copy(s,8,i);
    i := i + 1;
  end;
  if i > 11 then begin
    s := Copy(s,1,11)+'-'+Copy(s,12,i);
  end;
  Edit1.Text := s;
  if (not bkp) then
    case k of
      4,7,11: k := k + 1;
      5,8,12: if not del then k := k + 1;
    end;
  Edit1.SelStart := k;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  bkp := false;
  del := false;
  SetWindowLong(Edit1.Handle, GWL_STYLE, GetWindowLong(Edit1.Handle, GWL_STYLE) or ES_NUMBER);
end;

Regards, Geo
0
 
LVL 11

Expert Comment

by:pcsentinel
ID: 13977559
Modified version of mine that should work

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
       if (not (Key in [VK_BACK, 0,9,VK_RETURN])) or (Length(Edit1.Text) > 15) then // you may also want to add the arrow keys
  begin
    beep;
    Key := 0;
  end;
  if (Key in [0,9]) and (Length(Edit1.Text) in [4,8,12]) then
  begin
    Edit1.Text:=Edit1.Text+'-';
    Edit1.SelStart:=Length(Edit1.Text);
  end;
end;

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key=VK_BACK) and (Copy(Edit1.Text,Length(Edit1.Text),1)='-') then
  begin
    Key:=0;
    Edit1.Text:=Copy(Edit1.Text,1,pred(Length(Edit1.Text)));
    Edit1.SelStart:=Length(Edit1.Text);
  end
end;
0
 
LVL 11

Expert Comment

by:pcsentinel
ID: 13978127
Sorry I have to admit got it wrong again, problem with allowing chars input

The following seems to work for eveything except for the delete key

procedure TForm1.Edit3KeyPress(Sender: TObject; var Key: Char);
begin
  if ((not(Pos('-',Edit1.Text)<>0)) or (Edit3.SelStart>0)) and (ord(Key)=45) then
        key:=Chr(0);
{  if (Pos('.',Text)>0) and not(Pos('.',Edit3.SelText)<>0) and (ord(Key)=46) then
        key:=Chr(0);
}  if not (ord(key) in [8,13,48..57]) then
    key:=Chr(0);
  if (Length(Edit3.Text) in [4,8,12]) and (not (Ord(Key)=VK_BACK)) then
  begin
    Edit3.Text:=Edit3.Text+'-';
    Edit3.SelStart:=Length(Edit3.Text);
  end;
end;

procedure TForm1.Edit3KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 if (Key=VK_BACK) and (Copy(Edit3.Text,Length(Edit3.Text),1)='-') then
  begin
    Key:=0;
    Edit3.Text:=Copy(Edit3.Text,1,pred(Length(Edit3.Text)));
    Edit3.SelStart:=Length(Edit3.Text);
  end
end;
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…
Suggested Courses

589 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