Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TMaskEdit Help 2

Status
Not open for further replies.

JaNeily

Programmer
Mar 30, 2011
6
US
My first question to the forum - thanks in advance for any assistance. I am new to programming, so please be patient with me. I am using XE, and am trying to use a TMaskEdit. I would like to have data entry limited to 4 digits, using only the numbers 3,5,7,and 9. How would I create the mask?
 
Hi welcome to the forum.

Try this:

1) Drop a Mask Edit on your form, right click and select Input Mask Editor.

2) Enter this for the input mask (or paste it):
!9999;1;-

3) Enter this code for the OnKeyPress event of the Mask Edit:

Code:
procedure TForm1.MaskEdit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in [#8, '3','5','7','9']) then
  begin
    Key:= #0;
  end;
end;

This will only allow backspaces to delete last characters, and 3,5,7,9. If you need to show a message or something you could do this:

Code:
procedure TForm1.MaskEdit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in [#8, '3','5','7','9']) then
  begin
    ShowMessage('This key is not allowed');
    Key:= #0;
  end;
end;

Hope this helps
 
Thank you! Now, can you explain to me why it works? I expect the
'!9999' is saying 4 numeric characters, ';' is a separator. What does the '1' following it mean? The '-' is the character to display for a blank or missing entry. What do the '#8' and 'Key := #0' in the code mean?
 
You can probably take out the 1 in the mask, i believe this is used for saving the literal characters.

#8 is the key code for backspace
#0 is nothing, it basically blocks further entry in the edit.
 
Good/Props for JaNeily. Always great to see someone that wants to learn when they don't understand and not just take code and run with it, not understanding any more than when they started.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top