Delen via


CMFCMaskedEdit Class

The CMFCMaskedEdit class supports a masked edit control, which validates user input against a mask and displays the validated results according to a template.

class CMFCMaskedEdit : public CEdit

Members

Public Constructors

Name

Description

CMFCMaskedEdit::CMFCMaskedEdit

Default constructor.

CMFCMaskedEdit::~CMFCMaskedEdit

Destructor.

Public Methods

Name

Description

CMFCMaskedEdit::DisableMask

Disables validating user input.

CMFCMaskedEdit::EnableGetMaskedCharsOnly

Specifies whether the GetWindowText method retrieves only masked characters.

CMFCMaskedEdit::EnableMask

Initializes the masked edit control.

CMFCMaskedEdit::EnableSelectByGroup

Specifies whether the masked edit control selects particular groups of user input, or all user input.

CMFCMaskedEdit::EnableSetMaskedCharsOnly

Specifies whether the text is validated against only masked characters, or against the whole mask.

CMFCMaskedEdit::GetThisClass

Used by the framework to obtain a pointer to the CRuntimeClass object that is associated with this class type.

CMFCMaskedEdit::GetWindowText

Retrieves validated text from the masked edit control.

CMFCMaskedEdit::SetValidChars

Specifies a string of valid characters that the user can enter.

CMFCMaskedEdit::SetWindowText

Displays a prompt in the masked edit control.

Protected Methods

Name

Description

CMFCMaskedEdit::IsMaskedChar

Called by the framework to validate the specified character against the corresponding mask character.

Remarks

Perform the following steps to use the CMFCMaskedEdit control in your application:

1. Embed a CMFCMaskedEdit object into your window class.

2. Call the CMFCMaskedEdit::EnableMask method to specify the mask.

3. Call the CMFCMaskedEdit::SetValidChars method to specify the list of valid characters.

4. Call the CMFCMaskedEdit::SetWindowText method to specify the default text for the masked edit control.

5. Call the CMFCMaskedEdit::GetWindowText method to retrieve the validated text.

If you do not call one or more methods to initialize the mask, valid characters, and default text, the masked edit control behaves just as the standard edit control behaves.

Example

The following example demonstrates how to set up a mask (for example a phone number) by using the EnableMask method to create the mask for the masked edit control, the SetValidChars method to specify a string of valid characters that the user can enter, and SetWindowText method to display a prompt in the masked edit control. This example is part of the New Controls sample.

  CMFCMaskedEdit m_wndMaskEdit1;
    CMFCMaskedEdit m_wndMaskEdit2;
    CMFCMaskedEdit m_wndMaskEdit3;
    CMFCMaskedEdit m_wndMaskEdit4;
    CMFCMaskedEdit m_wndMaskEdit5;

    CString m_strValue1;
    CString m_strValue2;
    CString m_strValue3;
    CString m_strValue4;
    CString m_strValue5;


...


BOOL CPage4::OnInitDialog()
{
    CMFCPropertyPage::OnInitDialog();

    // Mask 1: phone number
    m_wndMaskEdit1.EnableMask(_T(" ddd  ddd dddd"), // The mask string
        _T("(___) ___-____"), // Literal, "_" char = character entry
        _T(' ')); // Default char
    m_wndMaskEdit1.SetValidChars(NULL); // Valid string characters
    m_wndMaskEdit1.SetWindowText(_T("(123) 123-1212"));

    // Mask 2: State, Zip Code
    m_wndMaskEdit2.EnableMask(_T("       cc       ddddd-dddd"), // The mask string
        _T("State: __, Zip: _____-____"), // Literal, "_" char = character entry
        _T(' ')); // Backspace replace char
    m_wndMaskEdit2.SetValidChars(NULL); // Valid string characters
    m_wndMaskEdit2.SetWindowText(_T("State: NY, Zip: 12345-6789"));
    // Mask 3: serial code
    m_wndMaskEdit3.EnableMask(_T("     AAAA AAAA AAAA AAAA"), // The mask string
        _T("S/N: ____-____-____-____"), // Literal, "_" char = character entry
        _T('_')); // Backspace replace char
    m_wndMaskEdit3.SetValidChars(NULL); // Valid string characters
    m_wndMaskEdit3.SetWindowText(_T("S/N: FPR5-5678-1234-8765"));

    // Mask 4: 0xFFFF
    m_wndMaskEdit4.EnableMask(_T("  AAAA"), // The mask string
        _T("0x____"), // Literal, "_" char = character entry
        _T('_')); // Backspace replace char
    m_wndMaskEdit4.SetValidChars(_T("1234567890ABCDEFabcdef")); // Valid string characters
    m_wndMaskEdit4.SetWindowText(_T("0x01AF"));

    // Mask 5: digits only
    m_wndMaskEdit5.DisableMask(); // Don't use the mask
    m_wndMaskEdit5.SetValidChars(_T("1234567890")); // Valid string characters
    m_wndMaskEdit5.SetWindowText(_T("1234567890"));
    
    return TRUE;  // return TRUE unless you set the focus to a control
}

void CPage4::OnButtonGet()
{
    m_wndMaskEdit1.GetWindowText(m_strValue1);
    m_wndMaskEdit2.GetWindowText(m_strValue2);
    m_wndMaskEdit3.GetWindowText(m_strValue3);
    m_wndMaskEdit4.GetWindowText(m_strValue4);
    m_wndMaskEdit5.GetWindowText(m_strValue5);
    UpdateData(FALSE);
}

Inheritance Hierarchy

CObject

   CCmdTarget

      CWnd

         CEdit

            CMFCMaskedEdit

Requirements

Header: afxmaskededit.h

See Also

Reference

Hierarchy Chart

CEdit Class

Other Resources

MFC Classes