Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf200 Project 1 DATABASE PROGRAMMING USING VC++.NET
5.Click on Finish to create the class. The class is added to the project in the Solution Explorer. Two files, LoginDlg.h and LoginDlg.cpp, and a dialog resource are added to the project.
To design the Login form, perform the following steps:
1. |
Switch to the Resource View. |
|
|
2. |
Select the dialog resource with the ID IDD_LOGIDLG. This is the resource |
||
|
|
|
Y |
|
added by the wizard for the class that you just added. |
||
3. |
|
L |
|
Refer to Figure 8-27 to design the ogin form. |
|||
|
|
F |
|
|
|
M |
|
|
A |
|
|
|
E |
|
|
|
T |
|
|
FIGURE 8-27 The design of the Login form |
|
Next, you need to set the properties of the form and the controls. Table 8-3 lists the properties to be set for the Login form, IDD_LOGINDLG:
Table 8-3 Properties to be Set for the Login Form
Property |
Category |
Value |
Caption |
Appearance |
Login Form |
Font(Size) |
Font |
MS Sans Serif(10) |
Use System Font |
Font |
False |
|
|
|
As in the previous form, you need not change the IDs of the static controls. For the remaining edit and button controls, you need to edit the IDs. Table 8-4 lists the controls and the value to be assigned to the ID property.
Team-Fly®
IMPLEMENTING DATA ACCESS TECHNOLOGIES |
Chapter 8 |
201 |
|||
Table 8-4 |
IDs of Controls of the Login Form |
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
Control |
Type |
ID Property Value |
|
|
|
|
|
|
|
|
|
Login ID |
Edit Box |
IDC_LOGIN |
|
|
|
Password |
Edit Box |
IDC_PASSWORD |
|
|
|
Submit |
Command Button |
IDOK |
|
|
|
Cancel |
Command Button |
IDCANCEL |
|
|
|
|
|
|
|
|
|
Besides this, the Password control’s Password property under the Behavior category should be set to True. This enables you to hide the password and display * instead.
You have successfully designed the Login form of your application. The next form to be designed is the Deposit/Withdrawal form.
Designing the Deposit/Withdrawal Form
In the same way as you handled the Login form, you also need to add a class to handle the Deposit/Withdrawal form. Follow the same steps that you performed to add the Login class. Specify the name of the class as CDeposit and the base class as CDialog. Correspondingly, two files, Deposit.h and Deposit.cpp, and a dialog resource are added to the project.
After adding the class, the next step is to design the form. To design the Deposit/Withdrawal form, perform the following steps:
1.Switch to the Resource View.
2.Select the dialog resource with the ID IDD_DEPOSIT. Change the ID to IDD_DEPWDRL to indicate that this dialog box handles both deposits and withdrawals. This is the resource added by the wizard for the class that you just added.
3.Refer to Figure 8-28 to design the Deposit/Withdrawal form.
Next, you need to set the properties of the form and the controls. Table 8-5 lists the properties to be set for the Deposit/Withdrawal form, IDD_DEPWDRL.
202 Project 1 DATABASE PROGRAMMING USING VC++.NET
FIGURE 8-28 The design of the Deposit/Withdrawal form
Table 8-5 Properties to be Set for the Deposit/Withdrawal Form
Property |
Category |
Value |
Caption |
Appearance |
Deposit /Withdrawal |
Font(Size) |
Font |
MS Sans Serif(10) |
Use System Font |
Font |
False |
|
|
|
As in the previous form, you need not change the IDs of the static controls. However, you need to edit the IDs of the remaining edit, buttons, and combo box controls. Table 8-6 lists the controls and the value to be assigned to the ID property.
Table 8-6 IDs of Controls of the Deposit/Withdrawal Form
Control |
Type |
ID Property Value |
Account Number |
Combo Box |
IDC_ACCNUM |
Control next to the Combo Box |
Edit Box |
IDC_TRANDT |
Deposit |
Radio Button |
IDC_RDEP |
Withdrawal |
Radio Button |
IDC_RWITHDRAW |
IMPLEMENTING DATA ACCESS TECHNOLOGIES |
Chapter 8 |
203 |
|||
|
|
|
|
|
|
|
|
|
|
|
|
Control |
Type |
ID Property Value |
|||
|
|
|
|
|
|
Cash |
Radio Button |
IDC_CASH |
|
|
|
Check |
Radio Button |
IDC_CHECK |
|
|
|
Save |
Command Button |
IDSAVE |
|
|
|
Cancel |
Command Button |
IDCANCEL |
|
|
|
Amount |
Edit Box |
IDC_AMOUNT |
|
|
|
|
|
|
|
|
|
The next form to be designed is the Transfer Operations form.
Designing the Transfer Operations Form
In the same way as you handled the Login and the Deposit/Withdrawal forms, you need to add a class to handle the Transfer Operations form. Follow the same steps that you performed to add classes in the previous two sections. Specify the name of the class as CTransfer and the base class as CDialog. Correspondingly, two files, Transfer.h and Transfer.cpp, and a dialog resource are added to the project.
Next, to design the Transfer Operations form, perform the following steps:
1.Switch to the Resource View.
2.Select the dialog resource with the ID IDD_TRANSFER. This is the resource added by the wizard for the class that you just added.
3.Refer to Figure 8-29 to design the Transfer form.
Next, you need to set the properties of the form and the controls. Table 8-7 lists the properties to be set for the Transfer Operations form, IDD_DEPWDRL:
Table 8-7 Properties to be Set for the Transfer Operations Form
Property |
Category |
Value |
Caption |
Appearance |
Transfer Operations |
Font(Size) |
Font |
MS Sans Serif(10) |
Use System Font |
Font |
False |
|
|
|
204 Project 1 DATABASE PROGRAMMING USING VC++.NET
FIGURE 8-29 The design of the Transfer Operations form
As in the previous form, you need not change the IDs of the static controls. For the remaining edit controls and buttons, you need to edit the IDs. Table 8-8 lists the controls and the value to be assigned to the ID property.
Table 8-8 IDs of Controls of the Transfer Operations Form
Control |
Type |
ID Property Value |
Control at the top of the form |
Edit Box |
IDC_CCDT |
From Account Number |
Edit Box |
IDC_FRMACCNO |
Internal |
Radio Button |
IDC_INTERNAL |
External |
Radio Button |
IDC_EXTERNAL |
To Account Number |
Edit Box |
IDC_TACCNO |
Creditor Name |
Edit Box |
IDC_CNAME |
Bank Name |
Edit Box |
IDC_CBANKNAME |
Bank Address |
Edit Box |
IDC_CBANKADDR |
Amount To Transfer |
Edit Box |
IDC_CAMT |
Save |
Command Button |
IDC_CSAVE |
Cancel |
Command Button |
IDC_CCANCEL |
|
|
|
IMPLEMENTING DATA ACCESS TECHNOLOGIES |
Chapter 8 |
205 |
|
|
|
Editing the Application’s Menu
You are now ready with all form designs required for your application. Before you start to code the application, you need to edit the application’s main menu. By default, the wizard creates the menu that is shown in Figure 8-30.
FIGURE 8-30 The application’s original main menu
You need to edit the menu to reflect only the options required in your application. Following is the list of changes that you need to make to arrive at the design shown in Figure 8-5:
Remove the Edit menu option.
Remove the View menu option.
Remove the Print, Print Preview, and Print Setup options.
Insert an option Reports in the menu and the suboptions shown in Figure 8-31.
FIGURE 8-31 The Reports menu option
Coding the Application
With the designing in place, your next action is to code the application. I will explain the coding to be done to handle the following:
Logging process
Adding and editing records
Deactivate and reactivate records
Deposit/Withdrawal process
Transfer process
Issue check book process
206 Project 1 DATABASE PROGRAMMING USING VC++.NET
Based on the aforementioned functionality, I will explain the coding to be done in the following forms and in the sequence in which they are listed:
Login form
Application’s main form
Deposit/Withdrawal form
Transfer Operations form
Coding the Login Form
Recall that you added a class named LoginDlg to handle the functionality of the Login form. To begin with, I will provide an outline of the logic to be used in implementing this functionality. Then, I will proceed to explain the code.
The logic behind:
When the user executes the application, the very first form that appears is the Login form. In this form, the user has to enter his or her username and the appropriate password.
Then, when the user clicks on the Submit button, the validity of the entered information should be checked. To do this, you will have to trap the data entered by the user and check it with the data in the BankLogin table that stores the usernames and the corresponding passwords.
If valid, then display the main form; otherwise, allow the user to retry logging in. The users will have only three attempts to log in. If all three attempts fail, then access to the application is denied and the application closes.
The code:
To display the Login form as the startup form, you need to add relevant code in
the InitInstance function of the CBankOperationApp class, which is a CWinApp-
derived class. To do this, perform the following steps:
1.Open the BankOperation.cpp file.
2.Include the following code:
#include “LoginDlg.h” // For the Application class to identify
the
CLoginDlg class
3. Edit the InitInstance function to add the code formatted in bold:
IMPLEMENTING DATA ACCESS TECHNOLOGIES |
Chapter 8 |
207 |
BOOL CBankOperationApp::InitInstance() |
|
|
|
|
|
{ |
|
|
CLoginDlg oLoginDlg; |
|
|
//Make 3 attempts before exiting the function |
|
|
for (int i=0; i<3; i++) |
|
|
{ |
|
|
if (oLoginDlg.DoModal()) |
|
|
{ |
|
|
// Remember to declare the variable, bCancelPressed, in the BankOpeartion.h //file. Declare it as a BOOL variable.
if (bCancelPressed)
//the bCancelPressed variable gets its value from the CloginDlg class. This value for this variable is set when the user presses either Cancel or Submit.
return FALSE;
// Remember to declare the variable, bLoginCorrect, in the BankOpeartion.h //file. Declare it as a BOOL variable.
if (!bLoginCorrect)
//bLoginCorrect also gets its value in the CloginDlg class where the login name //and password are accepted.
{
// Incorrect Login or Password AfxMessageBox(“Incorrect Login or Password.”);
if (i==2)
{
AfxMessageBox(“You have made 3 attempts.
Please contact admin for help. Can’t proceed.”); return FALSE;
}
}
else
{
break;
}
208 Project 1 DATABASE PROGRAMMING USING VC++.NET
}
else
{
return FALSE;
}
}
AfxEnableControlContainer();
//Standard initialization
//If you are not using these features and wish to reduce the size
//of your final executable, you should remove from the following
//the specific initialization routines you do not need.
//Change the registry key under which our settings are stored.
//TODO: You should modify this string to be something appropriate
//such as the name of your company or organization. SetRegistryKey(_T(“Local AppWizard-Generated Applications”));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
//Register the application’s document templates. Document templates
//serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(IDR_MAINFRAME,
RUNTIME_CLASS(CBankOperationDoc), |
|
RUNTIME_CLASS(CMainFrame), |
// main SDI frame window |
RUNTIME_CLASS(CBankOperationView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
IMPLEMENTING DATA ACCESS TECHNOLOGIES |
Chapter 8 |
209 |
|
|
|
//Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo))
return FALSE;
//The one and only window has been initialized, so show and update it.
m_pMainWnd->SetWindowPos(&CWnd::wndTop, 0, 0, 660, 440, SWP_DRAWFRAME);
m_pMainWnd->CenterWindow(); m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow();
return TRUE;
}
The explanation for the preceding code snippet follows:
When you execute your application, the control passes onto the InitInstance function. Therefore, in this function, you need to add the code to display the Login form and, hence, declare a pointer to the CLoginDlg class.
Only three attempts to log in are allowed; hence, you need to add a loop that will run exactly three times.
Within the loop, the control passes on to the dialog box class that will handle the login validation part. (You will next learn to code the validation. The dialog box class will validate and return a success or failure status back to this function.)
If the status as returned by the dialog class is successful, then you need to exit from the loop. Then the control passes to the line following the end of the while loop. Then the document template is created and the main view of the application, CBankOperationView, that is associated with the Account_Detail form is loaded.
If the status returned indicates failure, then check whether three attempts are over. If all three attempts are not successful, then break out of the loop and end the application.
The next task is to retrieve the value that the user enters and validate it against the values stored in the BankLogin table. To validate the user input with the values