Friday, May 6, 2011

Chapter 2 - Controls

The controls in C# are useful tools that can be placed in forms to perform various tasks and we already used oen of them: the button. The picture below show the Toolbox that contains the controls of C#. They are categorized into Common Controls, Containers, Menus & Toolbars, Data, Components, Printing, Dialogs. For now, we will focus only on some of most common controls:  Button, Label, ComboBox, ListBox, PictureBox, TextBox etc.
Toolbox

To insert a control into your form, you just need to drag the control from the toolbox and drop it into the form. You can reposition and resize it as you like, and you already did this in chapter one. So lets go further and build an calculator. Since windows has a nice built-in calculator we wont reinvent the wheel, we will only make a calculator that sums 2 numbers so that you can get familiar with text-boxes and label. For this you have to drop on our form 2 text-boxes and 3 labels. You can see them marked with yellow in the picture bellow:


Drag them on the form and arrange them as you like. This is how I put mines:

As you see they are nicely arranged as I used a useful tool C# provides for this purpose. You can find the toolbar under the main menu, it looks like this:
Layout toolbar
It is called Layout toolbar and it should be available for you by default when you see the form (this is called design view). When you see the code (code view) it diapers. If not displayed by default you right-click the main toolbar and check Layout. In order to align several controls at a time you need to select them by keeping the CONTROL key pressed. Go ahead a play a while with it until you are happy with your control alignment.