?
Solved

Mask/Format text during typing

Posted on 2005-04-22
12
Medium Priority
?
467 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

809 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