|
The Windows controls featured in the .NET Framework are
highly varied and provide most of the necessary regular functionality a
normal application would need. They do this through various properties and
their different methods. Here is an example of a form with a few controls:
|
To enhance their functionality and speed up
application development, Visual Basic offers a very extended library of
functions to cover different issues including mathematics, finance, date,
time, and commerce, etc.
To use a Visual Basic function in your application,
you can add its reference to your project. To add a reference to the
Visual Basic library, on the main menu, you can click Project ->
project-X Properties... In the Properties Pages, you can Add New
Reference... This would display the Add Reference dialog box. In the .NET
property page and in the list under the Component Name header, you can
click Microsoft.VisualBasic
After making the selection, you can click OK. Also, in
the Property Pages, you can click OK
Alternatively, you can manually add a reference to the
Microsoft.VisualBasic.dll assembly in your file:
#using <Microsoft.VisualBasic.dll>
Then, you must locate and identify the particular
function you want to use. Here are examples
System::Void btnCalculate_Click(System::Object^ sender, System::EventArgs^ e)
{
double base, height, area;
// Use Visual Basic's IsNumeric() function to validate the value of the text box
if( !Microsoft::VisualBasic::Information::IsNumeric(this->txtBase->Text) )
base = 0.00;
else // If the user entered a number, convert it to a valid double number
base = Microsoft::VisualBasic::Conversion::Val(this->txtBase->Text);
if( !Microsoft::VisualBasic::Information::IsNumeric(this->txtHeight->Text) )
height = 0.00;
else
height = Microsoft::VisualBasic::Conversion::Val(this->txtHeight->Text);
// Perform the corresponding operation
area = base * height / 2;
// Display the result in the Result text box
this->txtArea->Text = area.ToString();
}
Creation of a Custom Library
|
|
The .NET Framework as a library is very huge. It is
even more enhanced when combined with Win32. But no matter how large a
library is, it cannot possibly meet every one's need. For this reason,
sometimes you will need to create an additional library of functions
and/or classes you can use in different programs. Fortunately, it is very
easy to create a library. The most difficult is probably about what you
will decide to put in the library.
You can start by creating a project (on the main menu,
File -> New -> Project...). In the Project Types list, you would select
Class Library and give it a name. Here is an example:
And click OK. In the Solution Explorer, you would open
the header file and type the code for the library. Here is an example:
// ItemDescription.h
#pragma once
using namespace System;
namespace ItemDescription
{
public ref class CPartDescription
{
private:
String ^ number;
String ^ discr;
double uprice;
public:
CPartDescription(void)
{
this->number = L"";
this->discr = L"";
this->uprice = 0.00;
}
CPartDescription(String ^ nbr, String ^ name, double price)
{
this->number = nbr;
this->discr = name;
this->uprice = price;
}
property String ^ ItemNumber
{
String ^ get() { return number; }
void set(String ^ nbr) { number = nbr; }
}
property String ^ ItemName
{
String ^ get() { return discr; }
void set(String ^ desc) { discr = desc; }
}
property double UnitPrice
{
double get() { return uprice; }
void set(double price) { uprice = price; }
}
virtual String ^ ToString() override
{
return String::Concat(this->ItemNumber,
L" ",
this->ItemName,
L" ",
this->UnitPrice.ToString());
}
};
}
To actually create the library, on the main menu, you
would click Build -> Build project-X.