Introduction into Visual Studio environment.

Starting a new project

Start Visual Studio by clicking the Start button and then choosing Programs->Microsoft Visual Studio .NET 2003->Microsoft Visual Studio .NET 2003. In the central window of the program that will appear on the screen click on the New project button.

Start a new project

In the new window select the Windows Forms Applications (.NET) template that is located in the .NET section of Visual C++ Projects. In the same window select the directory where you would like to store your project files and the name for your project. On the figure below I'm creating a new project named RWar_1 which should be placed in the folder named c:\IST482\RobotWars. This, in particular, means that Visual Studio will store all of the project files in the folder c:\IST482\RobotWars\RWar_1.

Select the right project type, enter the name of the project and the folder

When you click on the OK button, Visual Studio will create an empty form of your new project. The title of your future program will be set to "Form1". In order to change this, click on the Properties window located at the bottom left corner of the screen and edit the property Text like shown on picture below.

Change the title that appears on the form

Adding an Exit button

Let's now make our first application a little bit more friendly and add an exit button to it. To add a button, select the Button tool on the Toolbox panel located on the right of the application form

Choose the Button tool on the Toolbox panel
and then click on the form at the place where you want to have the button. A new button saying "button1" will appear on the form. You can move the button to another position. To do so, just drag it to where you like it an leave it there. If you want to change the text on the button (which you probably do), click once on the button and then go to the Properties window. Edit the property Text. Whatever you type in the property will appear on the button. If the button is not big enough to show all the text, you can resize it either with your mouse or by manually changing properties Width and Height.
Change the Text property of the new button to Exit

Double click now on the newly created button. This will bring you to the text editor to the section of the code, which will be executed when a user clicks on the button. Put the cursor inside the body of the function and type

Close();
The call of the method Close() of an object of the type Form will terminate the program execution.
Enter one line of code in the button1_Click function

Now we can try to start the program. First, however, it would be good to save it. Click on the icons as shown on the figure.

First save the project and then run it

Since the program is not yet compiled you'll be prompted to confirm that you really want to build it.

Agree to build the project

When the building process is over you can enjoy your first Visual C++ project working.

Enjoy you program working. Press the Exit button to stop it.

Adding panels, labels, and text boxes

To add a panel or a label to your form, select the corresponding tool on the Toolbox panel and left click on the form. Use mouse to resize the added element and locate in the proper place. To change the text of the label change its property Text in the Properties table:
Change label text

Similarly, to add a text box to your form, click on the TextBox tool on the Toolbox panel and then make a left click on the form. You can change the initial text in the text box by setting the Text property exactly the same way we just did for the label. However, unlike labels the text boxes are more likely to accessed from the code. Thus, it makes sense to change the generic name textBox1 to something more meaningful. To do that, edit the property (Name) as shown on the figure below.

Add Panel, Label, and TextBox on the form. Change the (Name) and Text properties of the TextBox element.

Once we changed the name of the TextBox object we placed on the form, we can access it in the code using that new name, not the generic name given by Visual Studio. On the picture above we set the name of the object to RobotNum; that is, in the code we now use

RobotNum->Text
to access the data user typed in the box or display a string. For example, to display a string "Hello" we need to use
RobotNum->Text = S"Hello";

Please play with all these new tools and try to create a form like one shown on the picture below:

Try to create a form like this

String and Int32 datatypes, converting back and forth

The .NET Framework defines value types for all the primitive types used in C++. You can continue to use C++ types, and the compiler will ensure that the correct .NET type is used. These types are shown in the table.
.NET Type    Size (Bits)    C++ Equivalent Types
Boolean 8 bool
Char 16 wchar_t
Byte 8 unsigned char
SByte 8 char
Int16 16 short
UInt16 16 unsigned short
Int32 32 int, long, _int32
UInt32 32 unsigned int
Int64 64 _int64
UInt64 64 Unsigned _int64
Single 32 float
Double 64 double
DateTime   std::time_t
Object   void*
String   std::wstring<>

These .NET types have methods to convert to other primitive types, to compare values, to create a value from a string, and to convert to a formatted string. Here is an example that convert an integer into a string:

#using <mscorlib.dll>
using namespace System;

void main()
{
	Int32   i;
	String *s;

	Console::Write(S"Enter an integer number: ");
	s = Console::ReadLine(); // read a string from the console
	i = s->ToInt32(0);       // convert it to integer
	i = i / 2;

	s = i.ToString();

	Console::Write(S"Your number divided by 2 is = ");
	Console::WriteLine(s);
}
In this example you can see how a string of the .NET type String gets converted into an integer (.NET type Int32), and then another integer gets converted back to a string.

Another way to perform conversion from type to type is to use static methods of the class Convert as shown in the example below. To see how this code works, we would recommend to double click on the Start button on the from you just created and typed this code into the body of the function buttob2_Click(...)

int robot_num = Convert::ToInt32(RobotNum->Text);
int field_width = Convert::ToInt32(FieldWidthBox->Text);
int field_height = Convert::ToInt32(FieldHeightBox->Text);
bool show_grid = ShowGridCheckBox->Checked;

String *msg = S"We are about to start with the following parameters:");
msg = String::Concat(msg, S"\nNumber of robots: ");
msg = String::Concat(msg, robot_num.ToString());
msg = String::Concat(msg, S"\nWidth of the battlefield: ");
msg = String::Concat(msg, field_width.ToString());
msg = String::Concat(msg, S"\nHeight of the battlefield: ");
msg = String::Concat(msg, field_height.ToString());
msg = String::Concat(msg, S"\nShow grid: ");
msg = String::Concat(msg, show_grid?S"Yes":S"No");
MessageBox::Show(msg);

Homework assignment