Objective Toolkit : Chapter 2 Objective Toolkit Quick Start : Basic Tutorial—MaskEdit Control
Basic Tutorial—MaskEdit Control
The CEdit control that MFC provides is often insufficient for data entry purposes. The application can only verify the value the user entered after it is fully typed. In addition, CEdit provides no ability to break the edit field into sub-fields that indicate what text is expected. SECMaskEdit addresses these problems. SECMaskEdit is derived from CEdit and it adds member functions for specifying a mask.
This tutorial uses the built-in functionality of Objective Toolkit’s SECMaskEdit class to create a masked edit field for a telephone number.
Follow the steps below to create a simple dialog-based application using the AppWizard. An SECMaskEdit object, added as a member of the dialog, displays an entry field that is formatted for a telephone number. Along the way, the application is given access to all the Objective Toolkit classes.
1. From the File menu, choose New... (Name your project application MaskPhone.)
2. From the main wizard dialog, select MFC AppWizard (exe).
3. Click the Dialog based radio button.
4. Click Finish. The MFC AppWizard finishes creating the project for you.
NOTE >> You must follow the two additional steps below if you are going to generate your own sample programs.
5. The next thing we need to do is make sure all of the resources in Objective Toolkit are available to your project. Find the stdafx.h file for the project and add the following line:
 
#include <toolkit\secall.h>
Make sure the stdafx.h file reads:
 
// stdafx.h : include file for standard system include files,
// or project specific include files that are used
// frequently, but
// are changed infrequently
 
#if !defined(AFX_STDAFX_H__DC17B08B_39B3_11D1_8889_006097BFD99B__INCLUDED_)
#define AFX_STDAFX_H__DC17B08B_39B3_11D1_8889_006097BFD99B__INCLUDED_
 
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
 
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
 
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC OLE automation classes
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
 
// if linking to a custom library configuration
// include the configuration headers
#include "foundation\config\sfl_MyConfig.h"
#include "toolkit\config\ot_MyConfig.h"
 
#include <toolkit\secall.h> // Objective Toolkit headers
 
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual Studio will insert additional
// declarations immediately before the previous line.
 
#endif //!defined(AFX_STDAFX_H__DC17B08B_39B3_11D1_8889_006097BFD99B__INCLUDED_)
6. Click View | Resource Includes… and add the following line to the Read-Only symbol directives list box:
 
#include “toolkit\secres.h”
In the same dialog, add the following line to the compile-time directives list box:
 
#include “toolkit\secres.rc”
The list boxes should look like the following:
7. Click OK. A message box with a warning appears on the screen. The wording may vary depending on the version of Visual Studio, but the appearance of the warning is standard.
Create the dialog box for the control. In the Resources view, open the Dialog folder. It holds IDD_MASKPHONE_DIALOG. Open it and remove the static text that is already there.
Replace the text with an Edit box from the Controls palette. Right-click the edit box and open the properties dialog. Name the edit box IDC_PHONE_NUMBER.
8. Add a member variable of SECMaskEdit to the dialog class. Open up the header file for MaskPhoneDlg and look up OnInitDialog(). Add the following line:
 
SECMaskEdit m_editPhone;
9. Open MaskPhoneDlg.cpp, look up OnInitDialog(), and the add the following code to it under the TODO comment.
 
// TODO: Add extra initialization here:
m_editPhone.AttachEdit(IDC_PHONE_NUMBER, this);
// attach the mask to the edit box
m_editPhone.SetMask(_T(“(###)###-#### ext. ####”));
10. Build the project and see the final dialog.