Building a Managed .NET Framework Application (8)
Listing 3.3 Adding Event Handler Declarations
1: #pragma once
2:
3: #using
4:
5: #using
6: #using
7: #using
8:
9: using namespace System;
10: using namespace System::Windows::Forms;
11:
12: __gc class CHelloNETForm : public Form
13: {
14: public:
15: CHelloNETForm(void);
16: ~CHelloNETForm(void);
17: protected:
18: Button* m_pbtnMessage;
19: Button* m_pbtnDone;
20: Label* m_pstMessage;
21: System::ComponentModel::Container* m_pComponents;
22: void InitForm();
23: void OnMessageClick( Object* source, EventArgs* e);
24: void OnDoneClick( Object* source, EventArgs* e);
25: };
Now open the HelloNETForm.cpp file. The two functions you just added will be at the bottom of the file. In OnMessageClick, set the m_pstMessage Text property to “Hello from the .NET World.” For the OnDoneClick function, call the function Close, which shuts down the application. The two functions should appear similar to the following:
1: void CHelloNETForm::OnMessageClick( Object* source, EventArgs* e )
2: {
3: m_pstMessage->Text = “Hello from the .NET World.”;
4: }
5:
6: void CHelloNETForm::OnDoneClick( Object* source, EventArgs* e )
7: {
8: Close();
9: }
The final change to the application is to have the main() function create and run the new Windows Form class. Make the changes shown in the following code segment:
#include “helloNETform.h”
int _tmain(void)
{
Application::Run(new CHelloNETForm());
return 0;
}
With those final additions, the application is ready to compile and run. This is the same as with the MFC application. Simply press the F5 key or select the Debug, Start menu command. The resulting Visual C++ .NET application should have the same appearance as the MFC application created earlier.
Taken From: SAMS-Tech Yourself MS Visual C++.NET in 24 Hours
Leave a Reply