Home > Articles > Data > Access

  • Print
  • + Share This
This chapter is from the book

Creating the Authors ADO.NET Application

In this lesson, you will be creating a managed C++ application that produces a Windows Form. The application will access data found within an Access database and display the results within the controls of the form. Furthermore, the application will allow you to add new records to the database.

The first step is to create the main project solution. You will be creating a managed C++ application, so click New, Project from the File menu. Select Visual C++ Projects from the list of project types and select Managed C++ Application from the list of application templates. Give your solution the name AuthorDB and click OK to create the project. Now that the project has been created, you will need to create the main form's code and the necessary event handlers for the various controls contained on the form. The user interface will consist of three edit controls, three label controls, and six buttons. The edit controls will be used to display the database contents, and the buttons will be used to navigate and change the data.

For this project, create a separate file containing the Windows Form code. Click Project, Add Class from the main menu. Select the Generic C++ Class option from the list of class templates. Click Open and give your class the name AuthorDBForm. Click Finish to create the new class. Because this just creates a non-managed class, you will have to add the __gc keyword to the class declaration. Open the AuthorDBForm.h file and add the __gc keyword before the class keyword. Also, you will need to import and declare the necessary namespaces required for Windows Forms. Listing 20.1 shows the various namespaces you will need.

Listing 20.1 Class Declaration for the AuthorDB User Interface

 1: #pragma once
 2: #include "stdafx.h"
 3: #include <tchar.h>
 4:
 5: #using <mscorlib.dll>
 6: #using <System.DLL>
 7: #using <System.Drawing.DLL>
 8: #using <System.Windows.Forms.DLL>
 9:
10: using namespace System;
11: using namespace System::Drawing;
12: using namespace System::Windows::Forms;
13:
14: __gc class AuthorDBForm : public Form
15: {
16: public:
17:   AuthorDBForm(void);
18:   ~AuthorDBForm(void);
19:
20: protected:
21:   void InitForm();
22:
23: private:
24:   Button* buttonPrevious;
25:   Button* buttonNext;
26:   Button* buttonFirst;
27:   Button* buttonLast;
28:   Button* buttonNew;
29:   Button* buttonDelete;
30:   Label*  labelID;
31:   Label*  labelName;
32:   Label*  labelYear;
33:   TextBox* textBoxID;
34:   TextBox* textBoxName;
35:   TextBox* textBoxYear;
36:
37:   void buttonNew_Click(Object* sender, EventArgs* e);
38:   void buttonDelete_Click(Object* sender, EventArgs* e);
39:   void buttonFirst_Click(Object* sender, EventArgs* e);
40:   void buttonPrevious_Click(Object* sender, EventArgs* e);
41:   void buttonNext_Click(Object* sender, EventArgs* e);
42:   void buttonLast_Click(Object* sender, EventArgs* e);
43: };

Because this is a Windows Form object, you will need to derive your class from the .NET Forms class. This can be seen on line 14 of Listing 20.1. The next step involves creating the controls for your form. As mentioned earlier, you will be creating six Button controls, three TextBox controls, and three Label controls. Create the necessary declarations for these controls in a private section of your class declaration. We will continue with the same design that has been used throughout this book for forms, so create a protected InitForm method that doesn't accept any parameters and returns void.

This form contains two groups of buttons. Four of these buttons will be used to navigate through the data. With these buttons, you will be able to move to the next or previous record and also be able to move to the first or last record within the DataTable. The other group of buttons will allow you to insert and delete records within the database. Therefore, create six event handler delegates for each of the buttons, as shown in Listing 20.2.

Listing 20.2 Setting Control Properties and Wiring Events Within InitForm

 1: #include "StdAfx.h"
 2: #include "authordbform.h"
 3:
 4: AuthorDBForm::AuthorDBForm()
 5: {
 6:   InitForm();
 7: }
 8:
 9: AuthorDBForm::~AuthorDBForm()
 10: {
 11:
 12: }
 13:
 14: void AuthorDBForm::InitForm()
 15: {
 16:  textBoxID = new TextBox();
 17:  textBoxName = new TextBox();
 18:  textBoxYear = new TextBox();
 19:  buttonPrevious = new Button();
 20:  buttonNext = new Button();
 21:  buttonFirst = new Button();
 22:  buttonLast = new Button();
 23:  buttonNew = new Button();
 24:  buttonDelete = new Button();
 25:  labelID = new Label();
 26:  labelName = new Label();
 27:  labelYear = new Label();
 28:
 29:  SuspendLayout();
 30:
 31:  // textBoxID
 32:  textBoxID->Location = Point(16, 24);
 33:  textBoxID->Name = S"textBoxID";
 34:  textBoxID->Size = System::Drawing::Size(40, 20);
 35:  textBoxID->TabIndex = 0;
 36:  textBoxID->Text = S"";
 37:  textBoxID->ReadOnly = true;
 38:
 39:  // textBoxName
 40:  textBoxName->Location = Point(64, 24);
 41:  textBoxName->Name = S"textBoxName";
 42:  textBoxName->Size = System::Drawing::Size(192, 20);
 43:  textBoxName->TabIndex = 1;
 44:  textBoxName->Text = S"";
 45:
 46:  // textBoxYear
 47:  textBoxYear->Location = Point(264, 24);
 48:  textBoxYear->Name = S"textBoxYear";
 49:  textBoxYear->Size = System::Drawing::Size(104, 20);
 50:  textBoxYear->TabIndex = 2;
 51:  textBoxYear->Text = S"";
 52:
 53:  // buttonPrevious
 54:  buttonPrevious->Location = Point(48, 56);
 55:  buttonPrevious->Name = S"buttonPrevious";
 56:  buttonPrevious->Size = System::Drawing::Size(32, 23);
 57:  buttonPrevious->TabIndex = 3;
 58:  buttonPrevious->Text = S"<";
 59:  buttonPrevious->add_Click(new EventHandler(this,buttonPrevious_Click));
 60:
 61:  // buttonNext
 62:  buttonNext->Location = Point(80, 56);
 63:  buttonNext->Name = S"buttonNext";
 64:  buttonNext->Size = System::Drawing::Size(32, 23);
 65:  buttonNext->TabIndex = 4;
 66:  buttonNext->Text = S">";
 67:  buttonNext->add_Click( new EventHandler( this, buttonNext_Click ));
 68:
 69:  // buttonFirst
 70:  buttonFirst->Location = Point(16, 56);
 71:  buttonFirst->Name = S"buttonFirst";
 72:  buttonFirst->Size = System::Drawing::Size(32, 23);
 73:  buttonFirst->TabIndex = 5;
 74:  buttonFirst->Text = S"<<";
 75:  buttonFirst->add_Click( new EventHandler( this, buttonFirst_Click ));
 76:
 77:  // buttonLast
 78:  buttonLast->Location = Point(112, 56);
 79:  buttonLast->Name = S"buttonLast";
 80:  buttonLast->Size = System::Drawing::Size(32, 23);
 81:  buttonLast->TabIndex = 6;
 82:  buttonLast->Text = S">>";
 83:  buttonLast->add_Click( new EventHandler( this, buttonLast_Click ));
 84:
 85:  // buttonNew
 86:  //
 87:  buttonNew->Location = Point(232, 56);
 88:  buttonNew->Name = S"buttonNew";
 89:  buttonNew->Size = System::Drawing::Size(64, 23);
 90:  buttonNew->TabIndex = 7;
 91:  buttonNew->Text = S"New";
 92:  buttonNew->add_Click( new EventHandler( this, buttonNew_Click ));
 93:
 94:  // buttonDelete
 95:  buttonDelete->Location = Point(304, 56);
 96:  buttonDelete->Name = S"buttonDelete";
 97:  buttonDelete->Size = System::Drawing::Size(64, 23);
 98:  buttonDelete->TabIndex = 8;
 99:  buttonDelete->Text = S"Delete";
100:  buttonDelete->add_Click( new EventHandler( this, buttonDelete_Click ));
101:  //
102:  // labelID
103:  //
104:  labelID->Location = Point(16, 8);
105:  labelID->Name = S"label1";
106:  labelID->Size = System::Drawing::Size(32, 16);
107:  labelID->TabIndex = 9;
108:  labelID->Text = S"ID";
109:
110:  // labelName
111:  labelName->Location = Point(64, 8);
112:  labelName->Name = S"label2";
113:  labelName->Size = System::Drawing::Size(100, 16);
114:  labelName->TabIndex = 10;
115:  labelName->Text = S"Name";
116:
117:  // labelYear
118:  labelYear->Location = Point(264, 8);
119:  labelYear->Name = S"label3";
120:  labelYear->Size = System::Drawing::Size(100, 16);
121:  labelYear->TabIndex = 11;
122:  labelYear->Text = S"Year Born";
123:
124:  // Form1
125:  AutoScaleBaseSize = System::Drawing::Size(5, 13);
126:  ClientSize = System::Drawing::Size(384, 102);
127:  Name = "AuthorDBForm";
128:  Text = "Authors";
129:
130:  Controls->Add( labelYear);
131:  Controls->Add( labelName );
132:  Controls->Add( labelID );
133:  Controls->Add( buttonDelete );
134:  Controls->Add( buttonNew );
135:  Controls->Add( buttonLast );
136:  Controls->Add( buttonFirst );
137:  Controls->Add( buttonNext );
138:  Controls->Add( buttonPrevious );
139:  Controls->Add( textBoxYear );
140:  Controls->Add( textBoxName );
141:  Controls->Add( textBoxID );
142:
143:  ResumeLayout(false);
144: }
145:
146: void AuthorDBForm::buttonNew_Click(Object* sender, EventArgs* e)
147: {
148: }
149:
150: void AuthorDBForm::buttonDelete_Click(Object* sender, EventArgs* e)
151: {
152: }
153:
154: void AuthorDBForm::buttonFirst_Click(Object* sender, EventArgs* e)
155: {
156: }
157:
158: void AuthorDBForm::buttonPrevious_Click(Object* sender, EventArgs* e)
159: {
160: }
161:
162: void AuthorDBForm::buttonNext_Click(Object* sender, EventArgs* e)
163: {
164: }
165:
166: void AuthorDBForm::buttonLast_Click(Object* sender, EventArgs* e)
167: {
168: }

Now that the user interface variables and member functions have been declared, you can set the control properties and wire the controls to their associated delegates. This is all done within the InitForm function, as shown in Listing 20.2. The last step is to make sure your applications entry-point function, _tmain, displays your new form. In your AuthorDB.cpp file, include the header file for your form class and add the code to display your form, as shown in Listing 20.3.

Listing 20.3 Displaying Your Main Windows Form

 1: #include "stdafx.h"
 2:
 3: #using <mscorlib.dll>
 4: #include <tchar.h>
 5:
 6: using namespace System;
 7:
 8: #include "authordbform.h"
 9:
10: // This is the entry point for this application
11: int _tmain(void)
12: {
13:   Application::Run( new AuthorDBForm() );
14:   return 0;
15: }

At this point, your user interface is complete, albeit without any data to work with. Compile your application to make sure everything is working correctly. When you run your application, you should see a window similar to the one in Figure 20.2.

Figure 20.2 The user interface for the AuthorDB application.

  • + Share This
  • 🔖 Save To Your Account