480 Project 3 ADO.NET IN MANAGED C++
if(totallogincount==3)
{
//No row found - hence invalid login
//close the application after 3 tries are done.
|
MessageBox::Show(“You have exhausted your login attempts. The |
application will now close”); |
|
|
|
|
|
Close(); |
|
|
|
Y |
|
Application::Exit(); |
|
|
//Exit the application |
|
} |
|
|
L |
} |
|
|
|
F |
else |
//the record exists |
M |
|
{ |
|
|
A |
|
} |
|
|
dataReader->Close(); |
|
|
|
|
|
sqlconn->Close(); |
|
|
|
|
|
E |
//Close the Login form now that the login is |
|
LoginForm::Dispose(); |
|
T //validated
}
If the user tries to close the Login form by clicking on the Cancel button, close the application too:
void OnCancelButtonClick(Object *sender, EventArgs *e)
{
Close(); //Close the form
Application::Exit(); //Exit from the application
}
Creating the Account Details Form
Once the user has logged in successfully, the main banking form will be accessible, which will have all the GUI elements necessary for making the banking transactions. Figure 14-3 depicts the Account Details form, which is the main banking form.
Following is the declaration of the BankForm class and the variables declared in it. This class will handle the Account Details form and all other transactions in this form.
IMPLEMENTING ADO.NET IN A MANAGED C++ APP. Chapter 14 481
FIGURE 14-3 The Account Details form
__gc class BankForm: public Form
{
private:
//all variable declarations to initialize the various controls that are to be
//displayed on the form
StatusBar |
*statusBar; |
Button |
*prevbutton; |
Button |
*nextbutton; |
Button |
*firstbutton; |
Button |
*lastbutton; |
Button |
*insertrecord; |
Button |
*updaterecord; |
Button |
*clearfields; |
MainMenu |
*mainMenu; |
MenuItem |
*fileMenu; |
MenuItem |
*recordmenu; |
MenuItem |
*operatemenu; |
482 |
Project 3 |
ADO.NET IN MANAGED C++ |
|
|
MenuItem |
*helpmenu; |
|
|
|
// Pointer to the main form of the application that will display the login form |
|
|
LoginForm |
*lgform; |
|
|
TextBox |
*acno; |
|
|
TextBox |
*fname; |
|
|
TextBox |
*lname; |
|
|
TextBox |
*dob; |
|
|
TextBox |
*paddr; |
|
|
TextBox |
*maddr; |
|
|
TextBox |
*phno; |
|
|
TextBox |
*emailid; |
|
|
TextBox |
*refacno; |
|
|
TextBox |
*amount; |
|
|
TextBox |
*va; |
|
|
TextBox |
*typeac; |
|
|
Label |
*heading; |
|
|
ErrorProvider *error; |
|
|
bool flag; |
|
|
|
String |
*caption; |
|
|
int |
width; |
|
|
int |
height; |
//a global variable to store the reference to the DataSet. This data set will
//be used across the application - once it has been populated
DataSet *ds;
//Create a SqlDataAdapter.
SqlDataAdapter* myAdapter;
//variable to store the number of the total records in the dataset at any point int lastrow;
//a global variable to store which is the current record no.
IMPLEMENTING ADO.NET IN A MANAGED C++ APP. Chapter 14 483
Once the application starts running, it will create an instance of the BankForm class, and its constructor is invoked. The constructor of the BankForm class does the following:
Creates an instance of the LoginForm class and displays it as a dialog box.
Calls the CreateControls method that will create all the controls on the form.
Calls the FetchData method that will fetch records from the database and populate the controls with the first row of the returned dataset.
Following is the code for the constructor of the BankForm class:
BankForm()
{
//create the error provider control error=new ErrorProvider(); error->BlinkRate=250;
error->BlinkStyle=ErrorBlinkStyle::BlinkIfDifferentError;
flag=false;
//setting the properties of the form caption = “Bank Operations”;
width = 600; height = 400;
//the following code calls the login form that displays a dialog box. If the user
//successfully passes through the login form, then the control will pass to the
//CreateControls function that will create the main form. On the other hand, if
//the user clicks on the Cancel button or fails to enter the correct login
//details, then exit from the application.
lgform = new LoginForm(); if(lgform->ShowDialog(this) == 1)
{
}
lgform->Dispose();
484 Project 3 ADO.NET IN MANAGED C++
// call the function to create all the controls on the bank form
CreateControls();
currentrec = 0;
//Call the function to fetch the data from the database and put it into a Dataset
//and also populate the fields with the first row that has been fetched
FetchData();
}
The CreateControls method is used to create all the user interface controls that will be displayed on the BankForm. Different kinds of controls are created in this method.
The MainMenu and MenuItem classes allow you to create menus for the form. The main menu will have the option File, which in turn will have the option Exit. To create that menu structure, first you need to create the main menu:
mainMenu = new MainMenu();
Then, the File menu item is created and added to the MainMenu object:
fileMenu = new MenuItem(“&File”);
Then, the Exit menu item is created and added to the File menu item object. If you add it to the main menu object, then it will appear on the main menu bar.
mainMenu->MenuItems->Add(fileMenu);
fileMenu->MenuItems->Add(new MenuItem(“E&xit”, new EventHandler(this,
&BankForm::OnFileExit)));
When the entire menu is created, the mainMenu object is designated as the menu of the form by the following line of code:
this.Menu = mainMenu; //where this is a reference to the current Form object
Next, take a look at how the controls are created and added to the form. Consider the case of the button labeled First on the form. This button allows the user to navigate to the first record.
1.The Button object is instantiated.
2.The Text, Size, and TabIndex properties are set. The TabIndex property allows you to control the tab order on the form.
IMPLEMENTING ADO.NET IN A MANAGED C++ APP. Chapter 14 485
3.The Location property is set to control the location of the control on the form.
4.The event handler method for the Click event of the button is set and the button is added to the Controls collection of the form.
The following code snippet illustrates the preceding steps:
firstbutton = new Button(); firstbutton->Text = “&<< First”; firstbutton->Size = Drawing::Size(75, 30); firstbutton->TabIndex = 0;
firstbutton->Location = Drawing::Point(width/2-180, height - 160); firstbutton->Click += (new EventHandler(this, &BankForm::OnFirstRecord)); Controls->Add(firstbutton);
All other controls are added to the form similarly. Following is the complete listing of the CreateControls method:
void CreateControls()
{
//create controls here
//FOR EACH CONTROL - the size, location etc. are set
//also specified are the function handlers for buttons
//and menu item controls
// Basic WinForm Settings
Text = caption;
Size = Drawing::Size(width, height);
// Set up Menu
mainMenu = new MainMenu(); fileMenu = new MenuItem(“&File”); mainMenu->MenuItems->Add(fileMenu);
fileMenu->MenuItems->Add(new MenuItem(“E&xit”, new EventHandler(this, &BankForm::OnFileExit)));
// create the records menu
recordmenu = new MenuItem(“&Record”); mainMenu->MenuItems->Add(recordmenu);
486 Project 3 ADO.NET IN MANAGED C++
recordmenu->MenuItems->Add(new MenuItem(“&First Record”, new EventHandler(this, &BankForm::OnFirstRecord)));
recordmenu->MenuItems->Add(new MenuItem(“&Prev Record”, new EventHandler(this, &BankForm::OnPrevRecord)));
recordmenu->MenuItems->Add(new MenuItem(“&Next Record”, new EventHandler(this, &BankForm::OnNextRecord)));
recordmenu->MenuItems->Add(new MenuItem(“&Last Record”, new EventHandler(this, &BankForm::OnLastRecord)));
// create the Operation menu operatemenu = new MenuItem(“Operate”); mainMenu->MenuItems->Add(operatemenu);
operatemenu->MenuItems->Add(new MenuItem(“&Insert New Record”, new EventHandler(this, &BankForm::OnInsertRecord)));
operatemenu->MenuItems->Add(new MenuItem(“&Update Current Record”, new EventHandler(this, &BankForm::OnUpdateRecord)));
Menu = mainMenu;
// Set status bar
statusBar = new StatusBar(); statusBar->Text = “”; Controls->Add(statusBar);
// Set up first Record Button firstbutton = new Button();
firstbutton->Text = “&<< First”; firstbutton->Size = Drawing::Size(75, 30); firstbutton->TabIndex = 0;
firstbutton->Location = Drawing::Point(width/2-180, height - 160); firstbutton->Click += (new EventHandler(this, &BankForm::OnFirstRecord)); Controls->Add(firstbutton);
// Set up Prev Record Button prevbutton = new Button(); prevbutton->Text = “&< Prev”;
prevbutton->Size = Drawing::Size(75, 30); prevbutton->TabIndex = 0;
IMPLEMENTING ADO.NET IN A MANAGED C++ APP. Chapter 14 487
prevbutton->Location = Drawing::Point(width/2-80, height - 160); prevbutton->Click += (new EventHandler(this, &BankForm::OnPrevRecord)); Controls->Add(prevbutton);
// Set up Next Record Button nextbutton = new Button(); nextbutton->Text = “& Next >”;
nextbutton->Size = Drawing::Size(75, 30); nextbutton->TabIndex = 0;
nextbutton->Location = Drawing::Point(width/2+20, height - 160); nextbutton->Click += (new EventHandler(this, &BankForm::OnNextRecord)); Controls->Add(nextbutton);
// Set up last Record Button lastbutton = new Button(); lastbutton->Text = “&Last >>”;
lastbutton->Size = Drawing::Size(75, 30); lastbutton->TabIndex = 0;
lastbutton->Location = Drawing::Point(width/2+120, height - 160); lastbutton->Click += (new EventHandler(this, &BankForm::OnLastRecord)); Controls->Add(lastbutton);
//The following pieces of code create the labels
//and the corresponding edit box for the field’s data to
//be displayed on the form
heading = new Label(); heading->Text=”First Name”; heading->Size = Drawing::Size(100, 20);
heading->Location = Drawing::Point(width/2-240, height - 360); Controls->Add(heading);
fname = new TextBox(); fname->Name=”fname”;
fname->Size = Drawing::Size(150, 40); fname->TabIndex = 2;
fname->Location = Drawing::Point(width/2-120, height - 360); Controls->Add(fname);
488 Project 3 ADO.NET IN MANAGED C++
heading = new Label(); heading->Text=”Last Name”; heading->Size = Drawing::Size(100, 20);
heading->Location = Drawing::Point(width/2-240, height - 340); Controls->Add(heading);
lname = new TextBox(); lname->Name=”lname”;
lname->Size = Drawing::Size(150, 20); lname->TabIndex = 2;
lname->Location = Drawing::Point(width/2-120, height - 340); Controls->Add(lname);
heading = new Label(); heading->Text=”Date of Birth”; heading->Size = Drawing::Size(100, 20);
heading->Location = Drawing::Point(width/2-240, height - 320); Controls->Add(heading);
dob = new TextBox(); dob->Name=”dob”;
dob->Size = Drawing::Size(150, 40); dob->TabIndex = 2;
dob->Location = Drawing::Point(width/2-120, height - 320); Controls->Add(dob);
heading = new Label(); heading->Text=”Permanent Address”; heading->Size = Drawing::Size(120, 20);
heading->Location = Drawing::Point(width/2-240, height - 300); Controls->Add(heading);
paddr = new TextBox(); paddr->Name=”paddr”;
paddr->Size = Drawing::Size(150, 40); paddr->TabIndex = 2;
paddr->Location = Drawing::Point(width/2-120, height - 300);
IMPLEMENTING ADO.NET IN A MANAGED C++ APP. Chapter 14 489
Controls->Add(paddr);
heading = new Label(); heading->Text=”Mailing Address”; heading->Size = Drawing::Size(100, 20);
heading->Location = Drawing::Point(width/2-240, height - 280); Controls->Add(heading);
maddr = new TextBox(); maddr->Name=”maddr”;
maddr->Size = Drawing::Size(150, 40); maddr->TabIndex = 2;
maddr->Location = Drawing::Point(width/2-120, height - 280); Controls->Add(maddr);
heading = new Label(); heading->Text=”Phone No:”; heading->Size = Drawing::Size(100, 20);
heading->Location = Drawing::Point(width/2-240, height - 260); Controls->Add(heading);
phno = new TextBox(); phno->Name=”phno”;
phno->Size = Drawing::Size(150, 40); phno->TabIndex = 2;
phno->Location = Drawing::Point(width/2-120, height - 260); Controls->Add(phno);
heading = new Label(); heading->Text=”Email id:”; heading->Size = Drawing::Size(100, 20);
heading->Location = Drawing::Point(width/2-240, height - 240); Controls->Add(heading);
emailid = new TextBox(); emailid->Name=”emailid”;
emailid->Size = Drawing::Size(150, 40); emailid->TabIndex = 2;