Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Creating your first project in Visual C++ .NET

2.11/5 (11 votes)
15 Aug 2005CPOL6 min read 1   2  
How to Create your first project created in .NET environment espetially using VC++...?

Introduction

In this article I will show how to create a simple dialog based application using Visual Studio .NET & through doing it we will see where the class wizard is & how adding a class to a new dialog & calling that dialog by creating an instance of its class & calling or invoking its member function

Creating a new project

In Visual Studio .NET the process of creating a new project is like in Visual C++ 6 but there is some differences in the environment or interface itself; to create a new vc++ project in .NET do the following steps:

  • Open Visual Studio .NET from Programs menu.
  • Choose File | New | Project, New project dialog will be displayed (Figure 1) specify the type of project to be Visual C++ & the template used is MFC Application & enter the name of the project & specify the directory in which the project files will be saved

  • After Clicking Ok, MFC Application wizard will be displayed (Figure 2), from Application Type section specify the application type to be "Dialog Based" & click Finish

Sample screenshot Sample screenshot

Figure (1) Figure (2)

Now MFC Application Wizard has created a new application or project for you & open the development environment including a dialog in the design mode, so now you can make the changes you want to the dialog, compile & run your project to see your changes actually done

Design and Source Code

Design the dialog to be like the following figure, & make the ID of the first name edit box to be "IDC_FIRST" and the ID of the Last name edit box to be "IDC_LAST"

The intent here is to allow the user to enter his first & last name in the edit box & when clicking Go button another dialog will be displayed to the user & a welcome message will be displayed to him.

Sample screenshot

Figure (3)

To add a new dialog, right-click on the dialogs section in Resource View and choose insert dialog from the popup menu (Figure 5), a new dialog will be with ok and cancel buttons added to the design view delete cancel button & leave ok button as it; design the new dialog to be like shown in (Figure 4)

Note After adding a static box, right-click on it choose properties (Alt + F4) and change its ID to anything like IDC_STATIC1 (Figure 6)

Sample screenshot Sample screenshot Sample screenshot

Figure (4) Figure (5) Figure (6)

Adding a new class to a dialog in .NET

Keep the new added dialog open in the design view and while this new dialog opens choose "Add Class…" from Project menu, Enter the Class name "Say CDialog1" and specify the Base Class to be "CDialog" and finally click Finish

Note that there is a convention in class names in which the class name will start with the letter "C" and followed by the actual name of the class so when entering the name of the class,

Sample screenshot

Figure (7)

Adding Variables to Controls

In Visual C++ 6 you can add or attach variables to controls by using Class Wizard but Class wizard here in .NET is changed or you can say it is partitioned to wizards that are distributed in the menus in the environment, you will add variables here in .NET just simply by right-clicking the control you want to attach a variable & choose "Add Variable…", a figure like the following below will be displayed (Figure 8)

Sample screenshot

Figure (8)

In the Add Member Variable Wizard you can specify the name of the variable in "Variable Name" & you can specify the Access type List to be (Private, Public, or Protected), also you can specify the "Variable Type" to be (CButton, CListBox, CEdit…etc)

Note that if add or attach a variable to a control you will see the category & control ID combo boxes are enabled.

Notice

  • When adding a variable of type control specify in the category combo box the type "Control" (e.g. CButton, CEdit, CListBox, CComboBox…etc)

  • And when adding a variable that may has possible values stored in it, specify the category to be of type "Value" (e.g. CString, int, BOOL, Float….etc)

Variables that should be added

Two dialogs are now in the resource view the first one is like (Figure 3) and the second is like (Figure 4)

From the previous section you can attach variables to controls so add the following variables to the controls specified by its IDs

First Dialog (Figure 3)

ID

Variable Name

Variable Type

Access

IDC_FIRST

m_StrFisrt

CString

Public

IDC_LAST

m_StrLast

CString

Public

Second Dialog (Figure 4)

IDC_STATIC1

m_StrWelcome

CString

Public

Adding Events to Controls

Before knowing how adding events, we first should know what events are and also what event handler function mean or do…?

Events are actions that take places or occur by the user to say as an example the event of clicking on the a specific button and also the event of pressing just one click by the mouse or even double-click also the event of changing the selection in list or combo box control and also the event of changing the mouse position…Microsoft add many events that can took place by the user ….

Where events are the action themselves, there must be a handler function for these events to be invoked when the event takes place, to say as an example when the user double-click the mouse a specific function will be invoked or executed, this function is called the handler function for this event.

When the event takes place, in this case we say the event fires and when the event fires it handler function will be invoked or executed…

To add an event to a specific control, right-click on it and choose its "Properties" (Alt + Enter) and from properties window click on the events icon shown above on the window (see Figure 9-a)

Sample screenshot Sample screenshot

Figure (9-a) Figure (9-b)

To add an event handler function for a specific event just click on the arrow beside the event name and choose "<Add>…." Look at (Figure 9-b) it shows how you can add the event so-called KILL_FOCUS

Note that to add BN_CLICKED event to a button this is can be done simply just by double-clicking on the button in the design view. Also remember that BN_CLIKED is an event that takes place or fires when the user clicks on the button.

Add an event handler function to the "Go" button's BN_CLICKED event; as I said by double-click on the button or from properties window…and write the following code to the "Go" button in the main window Figure (3)

void CDialogsDlg::OnMbClickedButton1()
{
      CDialog1 dlg;

      UpdateData(TRUE);
      dlg.str_first=m_StrFirst;
      dlg.str_last=m_StrLast;
      dlg.DoModal();

}

Add the following lines of code in the BN_CLICKED event handler function added for the "Back" button in the second window or dialog Figure (4)

void Dialog1::OnBnClickedButton1()
{
     CDialogsDlg dlg;
     dlg.m_StrFirst=str_first;
     dlg.m_StrLast=str_last;
     this->EndDialog(0);
     dlg.DoModal();


}

Note that in the code there something that may be strange it is the variables str_first and str_last both arr declared in the class of the second dialog (Figure 4) to be of type "CString"

two functions here that deserve discussing, they are DoModal() and EndDialog(). Briefly, EndDialog() function is used to close the window and destroys it from the system, While DoModal() function is used here just to call and display the dialog.

Compilation & run

you can compile your application from Build | Compile or by pressing the shortcut (Ctrl + F7), after success in the compilation you are now ready to run your application by choosing Debug | Start to run or execute the project and run the debugger which aids you to detect errors and exceptions if there are found or simply by pressing (Ctrl + F5) just to run the application without the debugger.

After Reading this article plz rate it below....

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)