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.
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.
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.
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
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.
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.
Since the program is not yet compiled you'll be prompted to confirm that you really want to build it.
When the building process is over you can enjoy your first Visual C++ project working.
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.
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->Textto 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:
| .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);