Click here to Skip to main content
15,880,608 members
Articles / Desktop Programming / MFC
Article

Masked Edit Control

Rate me:
Please Sign up or sign in to vote.
3.40/5 (10 votes)
30 Jan 2000 199.3K   7.3K   52   25
A masked edit control that provides restricted, formatted and cued data input and validation
  •  Download demo project - 33 Kb
  •  Download source files - 14 Kb
  •  Download self-extracting installer - 488 Kb

    Sample Image - maskededit.gif

    Introduction

    Image 2

    Ultimate Toolbox includes a wide array of MFC extensions that enhance various user interface controls. COXMaskedEdit is one of several Ultimate Toolbox edit controls that extend the MFC's CEdit class. We are making it freely available to developers with the same licencing terms that would apply had they purchased the toolkit.

    Overview

    The COXMaskedEdit control extends the MFC CEdit control to provide restricted data input with visual cues, formatted data output, overtype capability, and a validation framework.

    You can use COXMaskedEdit anywhere you would use a CEdit class. If no input mask is set, it will behave like a standard CEdit.

    If you define an input mask, each character position in the Masked Edit control maps to either a placeholder of a specified type or a literal character<.

    The input mask prevents you from entering invalid characters into the control. If you attempt to enter a character that conflicts with the input mask, the control generates a ValidationError beep.

    The insertion point automatically skips over literals as you enter data or move the insertion point.

    When you insert or delete a character, all nonliteral characters to the right of the insertion point are shifted, as necessary. If shifting these characters leads to a validation error, the insertion or deletion is prevented, and a ValidationError beep is triggered.

    For example, suppose the Mask property is defined as "?###", and the current value of the Text property is "A12". If you attempt to insert the letter "B" to the left of the letter "A", the "A" would shift to the right. Since the second value of the input mask requires a number, the letter "A" would cause the control to generate a ValidationError beep.

    The Masked Edit control also validates the parameter value of the SetInputText function the user passes at run time. If you use the SetInputText function so that it conflicts with the input mask, the function will return an errorcode.

    You may select text in the same way as for a standard text box control. When selected text is deleted, the control attempts to shift the remaining characters to the right of the selection. However, any remaining character that might cause a validation error during this shift is deleted, and no ValidationError beep is generated.

    Normally, when a selection in the Masked Edit control is copied onto the Clipboard, the entire selection, including literals, is transferred onto the Clipboard. You can use the SetClipMode function to define the behavior for transferring only user-entered data onto the Clipboard or not - literal characters that are part of the input mask are not copied.

    Using COXMaskedEdit

    You can attach a COXMaskedEdit to an existing edit control by subclassing the latter.

    This is remarkably simple to do in the DevStudio IDE when working with an MFC dialog.

    Place a standard edit control on the dialog using the dialog editor. Invoke the Class Wizard and select the Member Variables page. Add a member variable for the ID of the edit control, selecting a CEdit control as the type.

    Next, open the header file for the dialog. Include OXMaskedEdit.h.

    In the AFX_DATA section for the dialog you will see the declaration for the edit control as a CEdit. Change this to COXMaskedEdit (or a class derived from COXMaskedEdit) and viola!

    Typically you will call SetMask and SetPromptSymbol on the control in OnInitDialog to set up your particular mask etc.

    Note that you won't need to call Create in this scenario.

    Don't forget to include the OXMaskedEdit.cpp file in your project!

    Depending on the order of compilation you may also find if helpful to include OXMaskedEdit.h in the dialog and/or main application .cpp file of your project.

    Input Masks

    These are the characters you can use to set the mask:

      . (period)

    Decimal placeholder. The actual character used is the one specified as the decimal placeholder in your international settings. This character is treated as a literal for masking purposes.

     

      , (comma)

    Thousands separator. The actual character used is the one specified as the thousands separator in your international settings. This character is treated as a literal for masking purposes.

     

      : (colon)

    Time separator. The actual character used is the one specified as the time separator in your international settings. This character is treated as a literal for masking purposes.

     

      / (slash)

     Date separator. The actual character used is the one specified as the date separator in your international settings. This character is treated as a literal for masking purposes.

     

      #

    Digit placeholder. (0-9)

     

      A

    Alphanumeric character placeholder (0-9 and a-Z)

     

      ?

    Alphabetic placeholder (a-Z)

     

      >

    Alphabetic placeholder, but forces uppercase chars (A-Z)

     

      <

    Alphabetic placeholder, but forces them to lowercase (a-z)

     

      &

    Character placeholder. Valid values for this placeholder are ANSI characters in the following ranges: 32-126 and 128-255.

     

      \

    Literal escape. Use this to place your own literals in the mask - note that two backslashes must be used in string literals to accomodate for the fact that this is also treated as an escape character for ASNI/ISO string formatting.

    As an example, lets look at a string to mask an IP address:

    "IP \\Address: ###\\.###\\.###\\.###"

    This will appear as:

    IP Address: ___.___.___.___

    Assuming that the placeholder or 'prompt' symbol has been set to the underscore.

    Note that we needed to use the escape character to enable both the 'A' in Address and the periods to show as literals.

    To display the string 'http:// ' we would have to use the escape char for the colon and slashes:

    "http\\:\\/\\/ "

    To display a backslash as a literal, we need to escape the escape, as in "c:\\\\AAAAAAAA\\.AAA"

     

  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    United States United States
    Since 1992 Dundas Data Visualization has been helping companies all over the world visualize their data. Dundas products have a global reputation of being the highest quality, and are all designed, built and tested to meet the strictest requirements that developers and business managers demand.

    Our showcase product is Dundas Dashboard, an easy-to-integrate digital dashboard software solution. Dundas Dashboard allows for the rapid and collaborative development of performance dashboards, helping companies leverage their business intelligence (BI) solutions.

    Our web-based dashboard software comes with wizard interfaces, and a unique Dundas DashFlowTM process, allowing for the simultaneous development of an executive dashboard by business analysts, IT staff and database administrators. It also uses premier charts, maps, gauges and graph controls, letting end-users visualize their data as required.

    Dundas also offers superb, world class consulting services for those companies that do not have the in-house expertise to implement their data visualization projects.

    The quality of our products in conjunction with our unmatched technical support, numerous awards and years of experience reflect Dundas Data Visualization's commitment to being the best!
    This is a Organisation

    3 members

    Comments and Discussions

     
    QuestionGreat and useful for integration into other projects. Added mask for HEX input Pin
    Martin Patarinski11-Apr-18 23:07
    Martin Patarinski11-Apr-18 23:07 
    AnswerRe: Great and useful for integration into other projects. Added mask for HEX input Pin
    Martin Jimenez12-Mar-24 19:41
    Martin Jimenez12-Mar-24 19:41 
    GeneralTotally useless : Native in Win32 ! Pin
    kilt20-Feb-09 6:49
    kilt20-Feb-09 6:49 
    GeneralSweet! Pin
    Jörgen Sigvardsson22-Jan-07 2:54
    Jörgen Sigvardsson22-Jan-07 2:54 
    Questionhow to associate the mask edit with pop up content lists Pin
    future200x16-Dec-06 20:05
    future200x16-Dec-06 20:05 
    Questionhow to associate the mask edit with pop up content lists Pin
    future200x16-Dec-06 19:30
    future200x16-Dec-06 19:30 
    Question2 things... Pin
    Eduard Huguet17-Jan-06 22:54
    Eduard Huguet17-Jan-06 22:54 
    GeneralClipboard problem Pin
    Harmaxus20-Jan-05 0:05
    Harmaxus20-Jan-05 0:05 
    GeneralQuestion Pin
    one_eddie24-Apr-04 12:55
    one_eddie24-Apr-04 12:55 
    How can I get the control text (typed data with mask signs, but without prompt signs). Does this class support a method to do this.

    For example:

    Control mask: ##-####-##-#
    Control text: 12-34-_-__-_
    GetInputText returned: 1234____

    I want to get: 12-34- or 12-34

    I know i can do somthing like this:

    CString GetInputDataNoPromptSign()<br />
    {<br />
    	CString strText;<br />
    	GetWindowText(strText);<br />
    <br />
    	int nPos = strText.Find(GetPromptSymbol(), 0);<br />
    <br />
    	if (nPos > 0)<br />
    	{<br />
    		return strText.Left(nPos);<br />
    	}<br />
    <br />
    	return "";<br />
    }


    Is there some other (simplier) way do do this?
    Generallame Pin
    DaFonz18-Jan-04 5:50
    DaFonz18-Jan-04 5:50 
    GeneralAlternative Pin
    Ben Hanson30-Jul-04 1:49
    Ben Hanson30-Jul-04 1:49 
    GeneralDynomite! and a suggestion Pin
    LiptenSoup9-Dec-03 8:08
    LiptenSoup9-Dec-03 8:08 
    GeneralVery cool Pin
    alex.barylski1-Jan-03 17:43
    alex.barylski1-Jan-03 17:43 
    GeneralCommercial apps Pin
    Kannan Kalyanaraman17-Oct-02 0:37
    Kannan Kalyanaraman17-Oct-02 0:37 
    GeneralRe: Commercial apps Pin
    alex.barylski1-Jan-03 17:42
    alex.barylski1-Jan-03 17:42 
    GeneralSetClipMode and updated version Pin
    Member 9619-Apr-02 19:42
    Member 9619-Apr-02 19:42 
    GeneralPocketPC 2002 Pin
    Mel Stober4-Apr-02 13:12
    Mel Stober4-Apr-02 13:12 
    GeneralUpdated Pin
    John Fisher30-Nov-00 5:31
    John Fisher30-Nov-00 5:31 
    GeneralRe: Updated Pin
    21-Jan-01 23:51
    suss21-Jan-01 23:51 
    GeneralDel key problem Pin
    jedf125-Jul-00 22:31
    jedf125-Jul-00 22:31 
    GeneralRe: Del key problem Pin
    Phil Davis11-Sep-00 7:47
    Phil Davis11-Sep-00 7:47 
    GeneralGreat! - But one issue.... Pin
    Phil Davis28-Jun-00 12:03
    Phil Davis28-Jun-00 12:03 
    GeneralRe: Great! - But one issue.... Pin
    pgiustinoni25-Feb-05 12:00
    pgiustinoni25-Feb-05 12:00 
    GeneralRe: Great! - But one issue.... Pin
    David Crow13-Mar-12 7:05
    David Crow13-Mar-12 7:05 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.