The series is only to program using windows forms. Using VC++ for console application is similar to C++. So that aspect of VC++ will not be dealt here in this blog.
So lets get started!!! :)
Getting Started
First of all install Visual C++ express edition in your system.
Open the visual C++ 2008.
You will get a screen something like this(it may vary a bit).

The start page consists of recent projects on the left side of page, getting started tab immediately below the recent projects tab and Microsoft's newsfeed taking up majority of space. You can get resources to learn visual c++ from the link they have provided (that is if you have internet connection in home).
You can start new project, load existing projects from the recent projects tab. You will see a list of recent projects you have worked on (that is if you already worked on some project. Else you will get only two tabs saying Open and create project).
First go to the File menu on the top left of the window. Click on New->Project (Alternately you can also press Ctrl + Shift + N). You will get a window like below.

You can see many templates of different types of projects you can do. There is class library, CLR Console Application, CLR empty project, and Windows Forms Application template available (that is if you are using Visual C++ express edition. For other edition you will have more templates). You can create Turbo C style projects by creating CLR console application. For creating windows form application (A window style app with window like this browser) choose Windows Form Application template. Enter the name of your project in the Name tab at the bottom of window. You can change the location of the project by clicking on Browse button an choosing appropriate folder. Click on Ok button to create new project. The window is as show below again.

Now that we are familiar with the create project window, its time to create our first project. Lets start with customary hello world program. First type the name HelloWorld (Or anything you want :D) in the Name tab. Click on Ok.
After few moments the window changes to something like this.

The middle box you see is called form. It has been titled Form1. Just notice the ovals i have marked. This are different windows that will help in programming a windows form application.
The left most is the Solution Explorer where you can see all the class headers and definition.
Then there is Properties window where all form elements' properties are shown and can be modified. The last oval shows Toolbox which is autohidden (all this windows may not be as shown. If they are not visible search around till you find something like the toolbox. If not then go to view menu and choose the appropriate window). Hover over the toolbox and the toolbox pops out as shown below.

You can make the toolbox remain visible always by clicking on the small button on the top (shown in circle) which says auto hide. You can also shift the window to some other place on our screen by clicking and dragging the window by its title bar (The place where name 'Toolbox' appears). As soon as you drag the window the screen changes to something like this.

As you can see you will see few new buttons appearing on the screen (As shown inside all circles). You can dock any windows by taking the title bar of the window to any of the buttons with arrows and the window is docked accordingly. Just experiment with all windows till you get a look of the screen you are comfortable with. After redocking my window looked like this.

Now time to add few features to our form. From the toolbox choose Textbox from the list. Click on it once. Click anywhere on the form once. You will notice a textbox has appeared on the form in place where you clicked the mouse as shown below.

Click on the textbox on the form(the white color box) and move it around the form to the place where you want the textbox.
Next lets add a button to the form. Again choose button from the toolbox and drop it on the form. The screen should look like this after the operation.

Next move the button around the form. You will notice that as you move the button around you will get some blue (or red) lines appearing. This are snap lines and they help in designing the window better by allowing us to align the controls in an order. Use them to align the button to the left edge of the textbox. You can also resize the control by using the small squares around the control by dragging the control till you get desired size. The form should look like this after the process.

Now lets rename the properties of various controls of the form. For this click on the form first ( the bigger box in the middle. You can know which control you have selected by looking at the property window where the name of the control is displayed at the top). Then search for Text property in the property window. Click on the right hand column and rename it as "First Application" and press enter. As soon as you do this the name on the top of the form changes to "First Application". The screen will look like this.

Next do the same to button Control. ( You dont need to change the text property of the textbox now. But if you want to you can change it and see the result). Change the Button text to "Hello".
You can also change the name of the control and name each control by your own name. This can be done by changing the "Name" property in the property window to name you want to give to the control. Right now leave the name as it is.
The window must look like something like this.

Next its time for some coding. Add a code to the button control so that when user clicks on the button it does some work. To add code to button, double click on the button control. You will get a new view of screen like this.

If you just scroll the window from top, you will see lot of code. This is the code generated by the IDE in order to create the different controls you added. For now you dont have to worry about it.
Just press enter where the cursor was previously present. Type following code:
textBox1->Text = "Hello World!";
You dont need to type the entire stuff. As soon as you press 'tex' press Ctrl + Space on your keyboard. You will get a drop down menu called "IntelliSense" showing different properties and variable. Select textBox1 and type ->. Again you will get a drop down menu. Type 'te' and the control in the drop down menu will show 'Text'. Hit enter and half the code is done. Type in rest of the code to finish coding. The screenshots of Intellisense is as shown below.

Intellisense is one of the most powerful feature of Visual C++. It makes coders work in coding lot simpler. You dont need to type all code with this. You dont need to remember all the variables you have declared in your project as all variables that are visible (that is all variables available in the scope) are shown in the drop down menu. In large projects this is very useful. In short it makes coders life lot easier.
Now that we have written the code, its time to execute the program. You will notice a small green triangle in the top of the window below menu bar. Click on it. Alternatively you can go to Debug menu and click start debugging or press F5. Once you do this you will get a window like this.

Click on Yes. You will see the build process at the bottom of the window. If you have followed all instructions then you will see a new window coming up like this.

The new window looks exactly like the one you just designed. Click on the Hello button. You will get "Hello World!" displayed on the textbox control immediately.

Congratulations!! You have just made the first application using VC++.
good work.
ReplyDelete