Just as you would create a managed class simply by
using the __gc keyword, to create your own custom class using a .Net
existing class, using the __gc keyword and inherit from the desired class,
provided the class can be inherited. The simplest class you can inherit
from is the form, which allows you to create a fundamental application.
In the previous example, we create a class by defining
its header file first, then its source file. From the early uses of MFC,
Microsoft Visual C++ allows you to create a class, combining a header and
a source files in one step. Unfortunately, probably because MSVC .Net is
still in its early stages, the dialog box used to add a class doesn't
provide an option to make the class a managed object. You will have to
edit the file(s) manually. Otherwise, you can still create your class in
two steps.
- To create a class inherited from Form, on the main menu, click
Project -> Add New Item...
- In the Categories tree view, make sure that Visual C++ is selected.
In the Templates list view, click Header File (.h)
- Replace the contents of the Name text box with SimpleForm and click
Open
- Define the class as follows:
#pragma once
#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
__gc class CSimpleForm : public Form
{
public:
CSimpleForm();
void InitForm();
private:
GroupBox* grpBox;
Label *lblLength;
Label *lblHeight;
TextBox* txtLength;
TextBox* txtHeight;
Label* lblPerimeter;
Label* lblArea;
TextBox* txtPerimeter;
TextBox* txtArea;
Button* btnCalculate;
void CalculateClick(Object *Sender, EventArgs *Args);
Button* btnClose;
void CloseClick(Object *Sender, EventArgs *Args);
};
|
- To implement the class, on the main menu, click Project -> Add
New Item...
- In the Categories tree view, make sure that Visual C++ is selected.
In the Templates list view, click C++ File (.cpp)
- Replace the contents of the Name text box with SimpleForm and click
Open
- Implement the class as follows:
#include "simpleform.h"
#include "Recto.h"
#using <mscorlib.dll>
CSimpleForm::CSimpleForm()
{
InitForm();
}
void CSimpleForm::InitForm()
{
this->Text = S"Rectangle Characteristics";
this->Size = Drawing::Size(336, 224);
this->MaximizeBox = false;
this->MinimizeBox = false;
this->StartPosition = FormStartPosition::CenterScreen;
this->FormBorderStyle = FormBorderStyle::FixedDialog;
this->grpBox = new GroupBox;
this->grpBox->Location = Point(16, 8);
this->grpBox->Size = Drawing::Size(210, 170);
this->grpBox->Text = S"Rectangle";
this->lblLength = new Label;
this->lblLength->Location = Point(16, 32);
this->lblLength->AutoSize = true;
this->lblLength->Text = S"&Length:";
this->grpBox->Controls->Add(lblLength);
this->txtLength = new TextBox;
this->txtLength->Location = Point(80, 32);
this->txtLength->Text = S"0.00";
this->grpBox->Controls->Add(txtLength);
this->lblHeight = new Label;
this->lblHeight->Location = Point(16, 64);
this->lblHeight->AutoSize = true;
this->lblHeight->Text = S"&Height:";
this->grpBox->Controls->Add(lblHeight);
this->txtHeight = new TextBox;
this->txtHeight->Location = Point(80, 64);
this->txtHeight->Text = S"0.00";
this->grpBox->Controls->Add(txtHeight);
this->lblPerimeter = new Label;
this->lblPerimeter->Location = Point(16, 104);
this->lblPerimeter->AutoSize = true;
this->lblPerimeter->Text = S"&Perimeter:";
this->grpBox->Controls->Add(lblPerimeter);
this->txtPerimeter = new TextBox;
this->txtPerimeter->Location = Point(80, 96);
this->txtPerimeter->Text = S"0.00";
this->grpBox->Controls->Add(txtPerimeter);
this->lblArea = new Label;
this->lblArea->Location = Point(16, 136);
this->lblArea->AutoSize = true;
this->lblArea->Text = S"&Area:";
this->grpBox->Controls->Add(lblArea);
this->txtArea = new TextBox;
this->txtArea->Location = Point(80, 128);
this->txtArea->Text = S"0.00";
this->grpBox->Controls->Add(txtArea);
this->Controls->Add(grpBox);
this->btnClose = new Button;
this->btnClose->Location = Point(240, 12);
this->btnClose->Text = S"&Close";
this->btnClose->add_Click(new EventHandler(this, CloseClick));
this->Controls->Add(btnClose);
this->btnCalculate = new Button;
this->btnCalculate->Location = Point(240, 40);
this->btnCalculate->Text = S"C&alculate";
this->btnCalculate->add_Click(new EventHandler(this, CalculateClick));
this->Controls->Add(btnCalculate);
}
void CSimpleForm::CloseClick(Object *Sender, EventArgs *Args)
{
Close();
}
void CSimpleForm::CalculateClick(Object *Sender, EventArgs *Args)
{
double dblLength = txtLength->Text->ToDouble(0);
double dblHeight = txtHeight->Text->ToDouble(0);
CRectangle *Recto = new CRectangle;
Recto->setLength(dblLength);
Recto->setHeight(dblHeight);
this->txtPerimeter->Text = Recto->Perimeter().ToString();
this->txtArea->Text = Recto->Area().ToString();
}
|
- Save the project.
|