Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
60 |
Part I |
INTRODUCING VC++.NET |
|
|
|
|
|
|
|
Y |
|
|
|
|
|
|
|
|
L |
|
|
|
|
M |
|
|
|
|
|
|
Dialog Editor |
A |
|
||
|
FResource View |
|||
FIGURE 3-6 The Dialog |
E |
|
||
ditor displaying the IDD_MYFIRSTAPPLICATION_FORM form |
||||
|
T |
|
|
|
7. Select and delete the default static text from the form.
8. Add controls to your form as shown in Figure 3-7. Table 3-1 consists of the IDs, control types, and properties to be changed for the controls.
Table 3-1 IDs for the Form Controls1
|
|
|
Properties to Be |
Control |
Control ID |
Control Type |
Changed |
|
|
|
|
Employee Code |
IDC_ECODE |
Edit Control |
None |
First Name |
IDC_FNAME |
Edit Control |
None |
Last Name |
IDC_LNAME |
Edit Control |
None |
Address |
IDC_ADDRESS |
Edit Control |
Multiline = True |
Date of Joining |
IDC_DOJ |
Edit Control |
None |
Date of Birth |
IDC_DOB |
Edit Control |
None |
Submit |
IDC_SUBMIT |
Button |
None |
Reset |
IDC_RESET |
Button |
None |
1Change the captions of all static controls and buttons, as shown in Figure 3-7.
Team-Fly®
CREATING A USER INTERFACE |
Chapter 3 |
61 |
|
|
|
FIGURE 3-7 The Application Form displaying the controls for the application
9.Select Format, Tab Order to check the tab index for the controls. Each control on the form displays its respective tab index. By default, the tab index is set according to the order in which you place the controls in the form. You can change the tab index for the controls by clicking on the controls in the order you need to set the tab index.
10.Select Format, Tab Order to hide the tab order screen.
11.Select File, Save All to save the project.
Just adding the controls to the form is not sufficient for your application to validate and manipulate the application data. To do so, you need to transfer data between the controls and the corresponding variables of the application. To transfer data between the controls and variables, you need to do the following:
1.Create variables for the controls.
2.Transfer data between controls and variables.
Each of these processes is discussed next in detail.
Creating Variables for the Form Controls
Adding variables for the form controls is yet another simple task in VC++.NET. VC++.NET provides you with the Add Member Variable Wizard for adding the
62 |
Part I |
INTRODUCING VC++.NET |
|
|
|
member variables to the application. To add variables for the form controls, follow these steps:
1.Right-click on the control for Employee Code.
2.Select Add Variable from the shortcut menu. The Add Member Variable Wizard starts, as shown in Figure 3-8.
FIGURE 3-8 The Add Member Variable Wizard displaying the default settings for a variable
The Add Member Variable Wizard enables you to create variables in an application. The Access list box enables you to determine the access specifier for the variable. The values of the Variable type list box are dynamically controlled by the status of the Control variable check box. By default, this check box is selected, and the Variable type list box contains the data type of the corresponding control object. When you clear the Control variables, the Variable type list displays the fundamental data types. In addition to the Variable type, other fields, such as Control ID, Category, and Control type, are disabled when you clear the check box.
3.Select Value from the Category list. The data type in the Variable type list changes to CString.
4.Type m_ECode in the Variable name text box.
CREATING A USER INTERFACE |
Chapter 3 |
63 |
|
|
|
5.Click on Finish to close the wizard.
6.Add member variables for other controls. Use the variable names as shown in Table 3-2.
Table 3-2 Variable Names for the Form Controls
Control ID |
Variable Name |
Maximum Characters |
IDC_ECODE |
m_ECode |
10 |
IDC_FNAME |
m_Fname |
30 |
IDC_LNAME |
m_Lname |
30 |
IDC_ADDRESS |
m_Address |
150 |
IDC_DOJ |
m_DOJ |
10 |
IDC_DOB |
m_DOB |
10 |
IDC_SUBMIT |
m_Submit |
None |
IDC_RESET |
m_Reset |
None |
|
|
|
Figure 3-9 displays the member variables that are created by the Add Member Variable Wizard.
FIGURE 3-9 The program code consisting of the member variables created by the Add Member
Variable Wizard
64 |
Part I |
INTRODUCING VC++.NET |
|
|
|
Transferring Data between Controls and Variables
After you have created the member variables for the form controls, to handle data in VC++, you use the following mechanisms:
Dialog Data Exchange (DDX)
Dialog Data Validation (DDV)
Adding events for validating data
Each of these mechanisms is described next.
Dialog Data Exchange (DDX)
The DDX mechanism, as the name suggests, transfers data between the controls and the member variables. The data that a user enters in the controls is transferred to the corresponding member variables. You implement this mechanism by using the following code in the DoDataExchange function of the view class:
DDX_Control(pDX, Control_ID, Var);
DDX_Text(pDX, Control_ID, Var);
The following list describes the elements of the preceding code:
pDX is the pointer to the CDataExchange class. The CDataExchange class consists of the m_bSaveAndValidate flag. The True or False status of the flag determines direction of data exchange between the controls and member variables. If the flag is True, the data transfers from controls to member variables. If the flag is False, the data transfers from member variables to controls.
Control_ID is the ID of the form control, whose data you need to transfer.
Var is the member variable in which you need to transfer data from the form control.
You use the DDX_Control function to associate the Control_ID control to the Var member variable or control objects, such as buttons, and you use the
function to transfer data between Control_ID and Var for fundamental data types.
CREATING A USER INTERFACE |
Chapter 3 |
65 |
|
|
|
Dialog Data Validation (DDV)
The DDV mechanism checks for the validation that you need to perform for the controls. The DDV mechanism should immediately follow the DDX mechanism. You implement the DDV mechanism by using the following code in the DoDataExchange function of the view class:
DDV_ValType(pDX, Var, Validation);
Here, ValType is the type of validation that you need to perform. The type of validation depends on the data type of the member variable. For example, to validate the string length, you replace ValType with MaxChars, and to validate the minimum and maximum values for an integer, you replace ValType with MinMaxInt.
pDX is the pointer to the CDataExchange class, Var is the member variable, and Validation is the parameter that you specify for the validation type. The number of validation parameters depends on the ValType. For example, DDV_MaxChars needs only one parameter, whereas DDV_MinMaxInt needs two parameters.
The Add Member Variable Wizard adds the following code in the MyFirstApplicationView.cpp file:
DDX_Control(pDX, IDC_SUBMIT, m_Submit);
DDX_Control(pDX, IDC_Reset, m_Reset);
DDX_Text(pDX, IDC_ECODE, m_Ecode);
DDV_MaxChars(pDX, m_Ecode, 10);
DDX_Text(pDX, IDC_FNAME, m_Fname);
DDV_MaxChars(pDX, m_Fname, 30);
DDX_Text(pDX, IDC_LNAME, m_Lname);
DDV_MaxChars(pDX, m_Lname, 30);
DDX_Text(pDX, IDC_ADDRESS, m_Address);
DDV_MaxChars(pDX, m_Address, 150);
DDX_Text(pDX, IDC_DOB, m_DOB);
DDV_MaxChars(pDX, m_DOB, 10);
DDX_Text(pDX, IDC_DOJ, m_DOJ);
DDV_MaxChars(pDX, m_DOJ, 10);
Notice that the DDV mechanism for all member variables is implemented immediately after the DDX mechanism.
66 |
Part I |
INTRODUCING VC++.NET |
|
|
|
Adding Events for Validating Data
The DDV mechanism validates data that is transferred to the member variables. You might need to perform validations on the control level, such as specify that a control should not be left blank or specify the maximum number of characters that users can enter in a control. You can add such validations to your application by adding event handlers for the required controls.
When a user interacts with the form controls, the controls generate several events. For example, when a user clicks on an edit control, the EN_SETFOCUS event is generated. Similarly, when a user moves out of the edit control, the EN_KILLFOCUS event is generated. To validate data when an event is generated, you need to add an event handler that defines the task to be performed. For example, to check the maximum number of characters in a control, you can trap the EN_KILLFOCUS event of the control.
To add an event handler, you need to follow these steps:
1.Right-click on the edit control for Employee Code.
2.Select Add Event Handler from the shortcut menu. The Event Handler Wizard, shown in Figure 3-10, starts. The Event Handler Wizard enables you to add a menu or dialog event handler to a class.
FIGURE 3-10 The Event Handler Wizard displaying the default settings
CREATING A USER INTERFACE |
Chapter 3 |
67 |
|
|
|
3.Select EN_KILLFOCUS from the Message type list. To check the description of an event, click on the event. The Handler description text box displays a short description of the event.
4.Click on Add and Edit to add an event handler to the control and edit its code. The wizard passes the control to the MyFirstApplicationView.cpp file. You need to edit the following function to validate the control:
void CMyFirstApplicationView::OnEnKillfocusEcode()
{
char ecode[15]; GetDlgItemText(IDC_ECODE,ecode,15); if (strlen(ecode)==0)
{
MessageBox(“The Employee Code can not be blank. Please enter an
Employee Code.”);
CEdit *code; code=(CEdit*)GetDlgItem(IDC_ECODE); code->SetFocus();
}
}
Here, OnEnKillfocusEcode() is the event handler function for the IDC_ECODE control. This function will execute immediately after the user moves out of the control. ecode is a character array of 15 elements and is used to store the data from the IDC_ECODE control. You use the GetDlgItemText function to retrieve the value from the IDC_ECODE control into the ecode array. The third parameter of the GetDlgItemText function specifies the number of characters to be transferred. If the user leaves the control without entering data, a message box with comments on validation is displayed. To set focus back to the control, you need to create a pointer of the class type of the control, which in this case is CEdit. Next, you make the pointer to point to the control by using the GetDlgItem function. The SetFocus function sets the focus to the control that the code pointer points to.
68 |
Part I |
INTRODUCING VC++.NET |
|
|
|
Handling Button Events
When you add a button control to the form, by default, an event handler for the BN_CLICKED event is also added. This event is generated immediately when the user clicks on the button. To perform a task on the button click, you need to write a code for this event handler. You can edit the code for the event handler by doing either of the following:
Using the Event Handler Wizard
Double-clicking on the button control
When you use the Event Handler Wizard to edit the event handler function, you will notice that the Add and Edit buttons are disabled for the BN_CLICKED event. This occurs because an event handler is already present for the event. To edit the code for the event handler, you need to click on the Edit Code button.
You will now learn the code that is required for resetting values when the user clicks on the Reset button. To code the Reset button, you need to perform these steps:
1.Double-click on the Reset button on the form.
2.Edit the OnBnClickedReset function and add the following code:
void CMyFirstApplicationView::OnBnClickedReset()
{
SetDlgItemText(IDC_ECODE,””);
SetDlgItemText(IDC_FNAME,””);
SetDlgItemText(IDC_LNAME,””);
SetDlgItemText(IDC_ADDRESS,””);
SetDlgItemText(IDC_DOB,””);
SetDlgItemText(IDC_DOJ,””);
}
Here, OnBnClickedReset is the event handler function for the BN_CLICKED event of the Reset button. This function executes immediately when the user clicks on Reset. The SetDlgItemText function sets the value of the controls.
To code the Submit button, you need to do the following:
1.Double-click on the Submit button on the form.
2.Edit the code for the OnBnClickedSubmit function as follows:
CREATING A USER INTERFACE |
Chapter 3 |
69 |
|
|
|
void CMyFirstApplicationView::OnBnClickedSubmit()
{
//Transferring data from controls to member variables UpdateData(TRUE);
//Checking data in name and address fields
if ((strlen(m_Fname)==0)||(strlen(m_Lname)==0) || (strlen(m_Address)==0))
{
MessageBox (“Incomplete form. Please enter data in all fields.”); return;
}
MessageBox (“Data for Employee code: “+ m_Ecode+” Employee name: “+m_Fname+” “+m_Lname+” is accepted.”);
}
Here, OnBnClickedSubmit is the event handler function for the BN_CLICKED event of the Submit button. This function executes immediately when the user clicks on the Submit button. The UpdateData(TRUE) function ensures that data is transferred from the form controls to member variables. Next, you ensure that the user enters data for First Name, Last Name, and Address. If the user does not enter data for these fields, a message box indicating the incomplete data is displayed. If the user does enter the data for these fields, a message box with a data acceptance message is displayed.
When you build and execute that application, the interface will be displayed as shown in Figure 3-11.
