Chapter 1. The Windows Application Environment: 1.1 Creating A New Project
Chapter 1. The Windows Application Environment: 1.1 Creating A New Project
1. Open the File menu > New Project or click New Project in the Start Page, either of which opens a New
Project dialog. Disclaimer: The main window of your Visual Studio may be slightly different, but don’t
worry about it.
2. Select Windows Forms Application template. It is a good practice to change the suggested project
name (WindowsApplication1) into a descriptive name for easy identification.
3. Select the Create Directory For Solution check box, by default, Visual Studio creates a new folder for
the project under the folder you have specified in the Location box and then click the OK button. A new
Visual C# project is created! Figure 1.1 shows Visual Studio IDE Form Designer for the newly created
project.
Form1.cs is a Solution
Explorer Component of
the newly created project.
The default environment is crowded, that is hide a few of the toolbars not in use through customization
the way you want to work with. Choose menu View > Toolbars > Customize which leads to a dialog box
with Toolbar and Customize tabs. The Toolbar tab are toggles which can turn on or off by clicking. The
Commands tab has Categories which shows the list of commands. You can drag each command from
the command list to the toolbar. You can hide just about any component of the IDE, except for the main
menu — after all, you have to be able to undo the changes!
3. Save the project at any time by choosing the File menu > Save All command. You’ll be prompted
about the project’s folder, and Visual Studio will save the project under the folder you specified in the
Location box.
A solution may contain multiple projects, you can right-click the project you want to set as the startup
project. You can also add items to a project with the Add Item command of the context menu, or remove
a component from the project with the Exclude From Project command. This command removes the
selected component from the project, but doesn’t affect the component’s file on the disk. The Delete
command removes the selected component from the project and also deletes the component’s file from
the disk.
Properties Window
The Properties window (Figure 1.4) allows you to view and modify the properties that determine/adjust
the appearance of the control and (in some cases) its function. The Properties View is toggle between
Categorized View and Alphabetical View. The button displays the properties in categories according
to their role. For instance, the properties that determine the appearance of the control are listed
alphabetically under the header Appearance, the properties that determine the control’s behavior are
listed alphabetically under the header Behavior, and so on. The button on the Properties window
displays all properties in alphabetical order.
The left column of the Properties window shows the property’s name while the right column shows each
property’s value. Many properties are set to a single value, such as a number or a string. If the possible
settings of a property are relatively few, meaningful constants in a drop-down list are displayed. Other
properties are set through a more elaborate interface. For example, Color properties are set from within
a Color dialog box displayed right in the Properties window. Font properties are set through the usual
Font dialog box. Collections are set in a Collection Editor dialog box, in which you can enter one string
for each item of the collection.
Alphabetical View
Output Window
The Output window (Figure 1.6) is where many of the tools, including the compiler, send their output.
Every time you start an application, a series of messages is displayed in the Output window. The compiler
generates these messages, and you need not understand them at this point.
Event-handler
The Windows Forms Designer allows you to add controls to a form in different ways.
1. When you double-click a control on the Toolbox, Visual Studio places an instance of the control
on the form in a default location and at a default size. Then you can use the mouse to relocate and
resize the control.
1 – Working With Windows Forms 5
2. When you click a control in the Toolbox, the mouse cursor changes while the mouse is over the
form. The new cursor looks like a plus sign with a small image of the control's Toolbox icon next to
it. If you click the form, Visual Studio adds a control at that location with a default size.
Figure 13.9 - Edge alignment and baseline alignment when placing control on a form
As you place controls on the form, you can align them in groups by using the commands of the Format
menu. Select multiple controls on the form by using the mouse and the Shift (or Ctrl) key, and then
align their edges or their middles with the appropriate command of the Format menu. To align the left
edges of a column of TextBoxes, choose the Format>Align>Left command. You can also use Format>
Make Same Size command to adjust the dimensions of the selected controls.
As you move controls around with the mouse, a blue snap (Figure 13.9) line appears when the controls
become nearly aligned with another control. Release the mouse while the snap line is visible to leave
the control aligned with the one indicated by the snap lines. The blue snap lines indicate edge alignment.
Most of the time, we need to align not the edges of two controls, but their baselines (the baseline of the
text on the control). The snap lines that indicate baseline alignment are red. Figure 13.9 shows both
types of snap lines.
2. Another way to set control's property is using Smart Tags. Many controls display a smart tag
when you select them on the Designer and it looks like a little box containing a right-pointing
triangle. When you click the smart tag, a small dialog appears to let you perform common tasks
for the control quickly and easily.
3. You may also set the control properties programmatically at run time:
//this code sets the checkbox's text property
this.RememberMeCheckBox.Text = "Remember me";
The values of the TabIndex properties of the various controls on the form need not be consecutive. To
specify the tab order of the various controls: in the Properties window set the TabIndex property or Click
View menu > Tab Order. The tab order of each control will be displayed on the corresponding control,
as shown in Figure 1.11.
As you place other controls on the form don't forget to lock to prevent
moving them around by mistake. To lock the controls in their places:
set each control's Locked property to True or by locking all the controls
on the form at once via the Format > Lock Controls command.
AcceptButton The button that's automatically activated when you press Enter, no
matter which control has the focus at the time. Usually the OK button on
a form is set as AcceptButton for a form.
CancelButton The button that's automatically activated when you hit the Esc key.
Usually, the Cancel button on a form is set as CancelButton for a form.
BackColor Sets the form background color.
Enabled If True, allows the form to respond to mouse and keyboard events; if
False, disables form.
Font This property specify font type, style, size
FormBorderStyle Determines the style of the form's border and the appearance of the form;
its value is one of the FormBorderStyle enumeration's members.
None: Borderless window that can't be resized.
Sizable: This is default value and will be used for resizable window
that's used for displaying regular forms.
Fixed3D: Window with a visible border, "raised" relative to the
main area. In this case, windows can't be resized.
FixedDialog: A fixed window, used to create dialog boxes.
FixedSingle: A fixed window with a single line border.
FixedToolWindow: A fixed window with a Close button only. It looks
like the toolbar displayed by the drawing and imaging applications.
SizableToolWindow: Same as the FixedToolWindow but resizable. In
addition, its caption font is smaller than the usual.
Name The actual name of the form.
Text The text that will appear at the form’s title bar.
StartPosition Determines the initial form’s position when it's first displayed. It will have
any of the following values:
CenterParent: The form is centered in the area of its parent form.
CenterScreen: The form is centered on the monitor.
Manual: The location and size of the form will determine its starting
position.
WindowsDefaultBounds: The form is positioned at the default
location and size determined by Windows.
WindowsDefaultLocation: The form is positioned at the Windows
default location and has the dimensions you've set at design time.
CenterToParent Centers the position of the form within the bounds of the parent form.
CenterToScreen Centers the form on the current screen.
KeyPress Occurs when a key is pressed while the form has focus.
Load Occurs before a form is displayed for the first time.