101
C / C++ /C# / Coding for the NET FRAMEWORK using C++ and VS Express 2005
« on: March 21, 2007 »
Preface
Inspired by this thread http://dbfinteractive.com/index.php?topic=1585.0
I decided to have a go with c++ using the NET framework.
Generally ppl tend to use the NET framework with languages like C#, J#
or maybe VB.NET. But you can use "normal" C++ to code NET framework
application as well.
In the following I describe the steps to create an C++ project using
the NET framework with Visual Suite Express 2005 edition.
Step 01 - Create the Project
Create a new Visual C++ project [ File -> New -> Project ... ]. Then
select CLR as the Project Type. And for easy GUI development
choose the Windows Forms Application as Template.
Enter a name (e.g. CppWithClr ) and press OK.
Step 02 - Create the GUI
Now you can easily draw the GUI with the DESIGN View of your
Form. You should see an empty Window with the Title Form1.
Use the toolbar to the right and add for this example a Textbox
and a button to the window.
( You to this by just drawing them on the window after you selected
them from the toolbar )
Step 03 - Modify form and control properties
As you can see, the button has the text button1 on it and in the
title bar of our window stands Form1 which isnt very "cool".
To change the properties of any GUI element just simply right click
on it and you got to the property-panel of this gui element.
In our case - you might right-click the button, choose Properties
and edit the Text under Appearance to your desire.
The same you can do with the window title. Simple right-click on a
spare region in the window and change the TEXT under apperance.
Step 04 - Add program code
Adding some program code it comparable easy. Just double click on our
button and you switch to the code-view of our Form1 code. There you
can see that by clicking on our button the IDE automatically created
the button_Click event function (this is executed, when you
press the button).
And right in this method we can put our code.
For now, we only want to show some text in our textbox we created - the
name of our textbox as long as we didn't change it - should be
textBox1[/b]. Updating text in a textbox is comparably easy. Just add
the following code :
Step 05 - Test the application
Now save your application and test it by pressing F5.
The generated code looks like the following :
The main method looks like this ( its auto-generated ... )
Our Form1.h to which we added our little actin routine looks like
this :
I added the created executable to this post. To run it you need the NET framework to
be installed on your system.
Inspired by this thread http://dbfinteractive.com/index.php?topic=1585.0
I decided to have a go with c++ using the NET framework.
Generally ppl tend to use the NET framework with languages like C#, J#
or maybe VB.NET. But you can use "normal" C++ to code NET framework
application as well.
In the following I describe the steps to create an C++ project using
the NET framework with Visual Suite Express 2005 edition.
Step 01 - Create the Project
Create a new Visual C++ project [ File -> New -> Project ... ]. Then
select CLR as the Project Type. And for easy GUI development
choose the Windows Forms Application as Template.
Enter a name (e.g. CppWithClr ) and press OK.
Step 02 - Create the GUI
Now you can easily draw the GUI with the DESIGN View of your
Form. You should see an empty Window with the Title Form1.
Use the toolbar to the right and add for this example a Textbox
and a button to the window.
( You to this by just drawing them on the window after you selected
them from the toolbar )
Step 03 - Modify form and control properties
As you can see, the button has the text button1 on it and in the
title bar of our window stands Form1 which isnt very "cool".
To change the properties of any GUI element just simply right click
on it and you got to the property-panel of this gui element.
In our case - you might right-click the button, choose Properties
and edit the Text under Appearance to your desire.
The same you can do with the window title. Simple right-click on a
spare region in the window and change the TEXT under apperance.
Step 04 - Add program code
Adding some program code it comparable easy. Just double click on our
button and you switch to the code-view of our Form1 code. There you
can see that by clicking on our button the IDE automatically created
the button_Click event function (this is executed, when you
press the button).
And right in this method we can put our code.
For now, we only want to show some text in our textbox we created - the
name of our textbox as long as we didn't change it - should be
textBox1[/b]. Updating text in a textbox is comparably easy. Just add
the following code :
Code: [Select]
textBox1->Text = \
"Hello world from C++ with CLR";
Step 05 - Test the application
Now save your application and test it by pressing F5.
The generated code looks like the following :
The main method looks like this ( its auto-generated ... )
Code: [Select]
#include "Form1.h"
using namespace CppWithClr2;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}Our Form1.h to which we added our little actin routine looks like
this :
Code: [Select]
#pragma once
namespace CppWithClr2 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ textBox1;
protected:
private: System::Windows::Forms::Button^ button1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(43, 19);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(198, 20);
this->textBox1->TabIndex = 0;
//
// button1
//
this->button1->Location = System::Drawing::Point(47, 50);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(193, 32);
this->button1->TabIndex = 1;
this->button1->Text = L"Show HelloWorld";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 94);
this->Controls->Add(this->button1);
this->Controls->Add(this->textBox1);
this->Name = L"Form1";
this->Text = L"HelloWorld C++/CLR";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
textBox1->Text = \
"Hello world from C++ with CLR";
}
};
}I added the created executable to this post. To run it you need the NET framework to
be installed on your system.

