User:Foxall/02

Ka Wiktionary

Hour 2. Navigating C#[wax ka badal]

The key to expanding your knowledge of C# is to become as comfortable as possible�as quickly as possible�with the C# design environment. Just as a carpenter doesn't think much about hammering a nail into a piece of wood, performing actions such as saving projects, creating new forms, and setting object properties should become second nature to you. The more comfortable you are with the tools of C#, the more you can focus your energies on what you're creating with the tools.

In this hour, you'll learn how to customize your design environment. You'll learn how to move, dock, float, hide, and show design windows, as well as how to customize menus and toolbars; you'll even create a new toolbar from scratch. After you've gotten acquainted with the environment, I'll teach you about projects and the files that they're made of (taking you beyond what was briefly discussed in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch01.htm#ch01 Hour 1], "A C# Programming Tour"), and I'll introduce you to the design windows with which you'll work most frequently. Finally, I'll show you how to get help when you're stuck.

The highlights of this hour include the following:

  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch02lev1sec2.htm#ch02lev1sec2 Navigating C#]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch02lev1sec1.htm#ch02lev1sec1 Using the Visual Studio .NET Start Page to open and create projects]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch02lev1sec2.htm#ch02lev3sec1 Showing, hiding, docking, and floating design windows]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch02lev1sec2.htm#ch02lev3sec8 Customizing menus and toolbars]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch02lev1sec3.htm#ch02lev1sec3 Adding controls to a form using the toolbox]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch02lev1sec4.htm#ch02lev1sec4 Viewing and changing object attributes using the Properties window]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch02lev1sec5.htm#ch02lev1sec5 Working with the files that make up a project]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch02lev1sec7.htm#ch02lev1sec7 How to get help]

>

Using the Visual Studio .NET Start Page

By default, the Visual Studio Start Page shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig01 Figure 2.1] is the first thing you see when you start C# (if C# isn't running, start it now). The Visual Studio Start Page is a gateway for performing tasks with C#. From this page, you can open previously edited projects, create new projects, edit your user profile, and browse information provided by Microsoft.

Figure 2.1. The Visual Studio Start Page is the default starting point for all Visual Studio programming languages, including C#.

graphics/02fig01.jpg

From this page, you can have C# load the last solution you edited, show the Open Project dialog box, show the New Project dialog box, or show an empty design environment. To view or edit the startup options, choose Options from the Tools menu to display the Options dialog box shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig02 Figure 2.2]. By default, the General section of the Environment folder is selected, which happens to contain the At Startup option.

Figure 2.2. Use the At Startup setting to control the first thing you see when C# starts.

graphics/02fig02.jpg

graphics/bookpencil.gif If the Visual Studio Start Page doesn't appear when you start C#, check the settings on the Options form; you may need to change At Startup to Show Start Page.
Creating New Projects 

To create new projects, click New Project on the Visual Studio Start Page. This shows the New Project dialog box shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig03 Figure 2.3]. The Project Types list varies from machine to machine, depending on which products of the Visual Studio .NET family are installed. Of course, we're interested only in the C# Project types in this book.

Figure 2.3. Use the New Project dialog box to create C# projects from scratch.

graphics/02fig03.jpg

graphics/bookpencil.gif You can create many types of projects with C#, but this book focuses mostly on creating Windows Applications, perhaps the most common of the project types. You will learn about some of the other project types as well, but when you're told to create a new project, make sure the Windows Application template is selected unless you're told otherwise.

When you create a new project, be sure to enter a name for it in the Name text box before clicking OK or double-clicking a project type icon. This ensures that the project is created with the proper path and filenames, eliminating work you would otherwise have to do to change these values later. After you specify a name, you can create the new project either by double-clicking the project type template icon or by clicking an icon once to select it and then clicking OK. After you've performed either of these actions, the New Project dialog box closes and a new project of the selected type is created.

By default, Visual Studio saves all your projects in subfolders of your My Documents folder. The hierarchy used by C# is

\My Documents\Visual Studio Projects\<Project Name> 

Notice how the name you give your project is used as its folder name. This makes it easy to find the folders and files for any given project and is one reason that you should always give your projects descriptive names. You can use a path other than the default by specifying a specific path on the New Project dialog box, although you probably won't often need to do so.

graphics/bookpencil.gif You can create a new project at any time (not just when starting C#) by opening the New submenu on the File menu and choosing Project.
Opening an Existing Project 

Over time, you'll open more projects than you create. There are essentially two ways to open projects from the Visual Studio Start Page. If it's one you've recently opened, the project name will appear in a list within a rectangle in the middle of the Start Page (refer to [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig01 Figure 2.1]). Because the name displayed for the project is the one given when it was created, this is yet another reason to give your projects descriptive names. Clicking a project name opens the project. I'd venture to guess that you'll use this technique 95% of the time. To open a project for the first time (such as when opening sample projects), click Open Project on the Visual Studio Start Page. Clicking this link displays a standard dialog box that you can use to locate and select a project file.

graphics/bookpencil.gif As with creating new projects, you can open an existing project at any time, not just when starting C#, by selecting File, Open.
>

Navigating and Customizing the C# Environment

C# lets you customize many of its interface elements, such as windows and toolbars, enabling you to be more efficient in the work that you do. Create a new Windows Application now (use the New Project dialog box or select File, New) so that you can see and manipulate the design environment. Name this project Environment Tutorial. (This exercise won't create anything reusable, but it will help you learn how to navigate the design environment.) Your screen should look like the one shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig04 Figure 2.4].

Figure 2.4. This is pretty much how the IDE appears when you first install C#.

graphics/02fig04.jpg

graphics/bookpencil.gif Your screen may not look exactly like that shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig04 Figure 2.4], but it'll be close. For example, the Output window won't be visible unless you've built a project. If you completed [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch01.htm#ch01 Hour 1], the window will be visible. By the time you've finished this hour, you'll be able to change the appearance of the design environment to match this figure�or to any configuration you prefer.
Working with Design Windows 

Design windows, such as the Properties and Solution Explorer windows shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig04 Figure 2.4], provide functionality for building complex applications. Just as your desk isn't organized exactly like that of your co-workers, your design environment doesn't have to be the same as anyone else's, either.

A design window may be placed into one of four primary states:

  • Closed
  • Floating
  • Docked
  • Auto hidden
Showing and Hiding Design Windows 

When a design window is closed, it doesn't appear anywhere. There is a difference between being closed and being hidden, as you'll learn shortly. To display a closed or hidden window, choose the corresponding menu item from the View menu. For example, if the Properties window isn't displayed in your design environment, you can display it by choosing Properties Window on the View menu (or press its keyboard shortcut�F4). Whenever you need a design window and can't find it, use the View menu to display it. To close a design window, click its Close button (the button on the right side of the title bar with the X on it), just as you would close an ordinary window.

 Floating Design Windows 

Floating design windows are visible windows that float over the workspace, as shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig05 Figure 2.5]. Floating windows are like typical application windows in that you can drag them around and place them anywhere you please, even on other monitors when you're using a multiple-display setup. In addition to moving a floating window, you can also change its size by dragging a border.

Figure 2.5. Floating windows appear over the top of the design environment.

graphics/02fig05.jpg

 Docking Design Windows 

Visible windows appear docked by default. A docked window is a window that appears attached to the side, top, or bottom of the work area or to some other window. The Properties window in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig04 Figure 2.4], for example, is docked to the right side of the design environment. To make a floating window a docked window, drag the title bar of the window toward the edge of the design environment to which you want to dock the window. As you drag the window, you'll drag a rectangle that represents the outline of the window. When you approach an edge of the design environment, the rectangle will change shape and "stick" in a docked position. If you release the mouse while the rectangle appears this way, the window will be docked. Although this is hard to explain, it's very easy to do.

graphics/bookpencil.gif You can size a docked window by dragging its edge opposite the side that's docked. If two windows are docked to the same edge, dragging the border between them enlarges one while shrinking the other.

To try this, you'll need to float a window that's already docked. To float a window, you "tear" the window away from the docked edge by dragging the title bar of the docked window away from the edge to which it is docked. Note that this technique won't work if a window is set to Auto Hide (which is explained next). Try docking and floating windows now by following these steps:

  1. Ensure that the Properties window is currently displayed (if it's not, show it using the View menu). Make sure the Properties window isn't set to Auto Hide by right-clicking its title bar and deselecting Auto Hide (if it's selected) from the shortcut menu, as shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig06 Figure 2.6].
Figure 2.6. You can't float a window that's set to Auto Hide.


  1. Drag the title bar of the Properties window away from the docked edge. When the rectangle representing the border of the window changes shape, release the mouse button. The Properties window should now appear floating.
  2. Dock the window once more by dragging the title bar toward the right edge of the design environment. Again, release the mouse button when the rectangle changes shape.
graphics/bulb.gif If you don't want a floating window to dock, regardless of where you drag it to, right-click the title bar of the window and choose Floating from the context menu. To allow the window to be docked again, right-click the title bar and choose Dockable.
Auto Hiding Design Windows 

A feature of C# design environment is the capability to auto hide windows. Although you might find this a bit disconcerting at first, after you get the hang of things, this is a very productive way in which to work because your workspace is freed up, yet design windows are available by simply moving the mouse. Windows that are set to Auto Hide are always docked; you can't set a floating window to Auto Hide. When a window auto hides, it appears as a tab on the edge to which it's docked�much like minimized applications are placed in the Windows taskbar.

Look at the left edge of the design environment in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig06 Figure 2.6]. Notice the two tabs on the left side of the IDE. One tab has a picture of two computers, and the other is labeled Toolbox. These tabs represent auto-hidden windows. To display an auto-hidden window, move the pointer over the tab representing the window. When you move the pointer over a tab, C# displays the design window so that you can use its features. When you move the pointer away from the window, the window automatically hides itself�hence the name. To make any window hide itself automatically, right-click its title bar and select Auto Hide from its shortcut menu. Alternatively, you can click the little picture of a pushpin appearing in the title bar next to the Close button to toggle the window's Auto Hide state.

 Performing Advanced Window Placement 

The techniques discussed in this section so far are basic methods for customizing your design environment. Things can actually get a bit more complicated if you want them to. Such complication presents itself primarily as the capability to create tabbed floating windows like the one shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig05 Figure 2.5]. Notice that at the bottom of the floating window is a set of tabs. Clicking a tab shows its corresponding design window, replacing the window currently displayed. These tabs are created much the same way in which you dock and undock windows�by dragging and dropping. For instance, to make the Solution Explorer window a floating window of its own, you would drag the Solution Explorer window tab away from the floating window. As you do so, a rectangle appears, showing you the outline of the new window. Where you release the rectangle determines whether you dock the design window you're dragging or whether you make the window floating. To make a design window a new tab of an already floating window, drag its title bar and drop it in the title bar of a window that is already floating.

In addition to creating tabbed floating windows, you can dock two floating windows together. To do this, drag the title bar of one window over another window (other than over the title bar) until the shape changes, and then release the mouse. [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig07 Figure 2.7] shows two floating windows that are docked to one another and a third window that is floating by itself.

Figure 2.7. Floating, docked, floating and docked�the possibilities are numerous!

graphics/02fig07.jpg

Using all the techniques discussed here, you can tailor the appearance of your design environment in all sorts of ways. There is no one best configuration. You'll find that different configurations work better for different projects and in different stages of development. For instance, when I'm designing the interface of a form, I want the toolbox to stay visible but out of my way, so I tend to make it float, or I turn off its Auto Hide property and leave it docked to the left edge of the design environment. However, after the majority of the interface elements have been added to a form, I want to focus on code. Then I dock the toolbox and make it auto hide itself; it's there when I need it, but it's out of the way when I don't. Don't be afraid to experiment with your design windows, and don't hesitate to modify them to suit your changing needs.

 Working with Toolbars 

Toolbars are the mainstay for performing functions quickly in almost all Windows programs (you'll probably want to add them to your own programs at some point, and [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch09.htm#ch09 Hour 9], "Adding Menus and Toolbars to Forms," shows you how). Every toolbar has a corresponding menu item, and buttons on toolbars are essentially shortcuts to their corresponding menu items. To maximize your efficiency when developing with C#, you should become familiar with the available toolbars. As your C# skills progress, you can customize existing toolbars and even create your own toolbars to more closely fit the way you work.

 Showing and Hiding Toolbars 

C# includes a number of built-in toolbars for you to use when creating projects. Two toolbars are visible in most of the figures shown so far in this hour. The one on the top is the Standard toolbar, which you'll probably want displayed all the time. The second toolbar is the Layout toolbar, which provides useful tools for building forms.

The previous edition of Visual Studio had about 5 toolbars; Visual Studio .NET, on the other hand, has more than 20 toolbars! The toolbars you'll use most often as a C# developer are the Standard, Text Editor, and Debug toolbars. Therefore, this hour discusses each of these. In addition to these predefined toolbars, you can create your own custom toolbars to contain any functions you think necessary, which you'll learn how to do later in this hour.

To show or hide a toolbar, choose View, Toolbars to display a list of available toolbars. Toolbars currently displayed appear selected (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig08 Figure 2.8]). Click a toolbar name to toggle its visible state.

Figure 2.8. Hide or show toolbars to make your work more efficient.

graphics/02fig08.jpg

graphics/bulb.gif A quick way to access the list of toolbars is to right-click any visible toolbar.
Docking and Resizing Toolbars 

Just as you can dock and undock C#'s design windows, you can dock and undock the toolbars. Unlike the design windows, however, C#'s toolbars don't have a title bar that you can click and drag when they're in a docked state. Instead, each docked toolbar has a drag handle (a set of horizontal lines along its left edge). To float (undock) a toolbar, click and drag the grab handle away from the docked edge. After a toolbar is floating, it has a title bar. To dock a floating toolbar, click and drag its title bar to the edge of the design environment to which you want it docked. This is the same technique you use to dock design windows.

graphics/bulb.gif A shortcut for docking a toolbar is to double-click its title bar.

Although you can't change the size of a docked toolbar, you can resize a floating toolbar (a floating toolbar behaves like any other normal window). To resize a floating toolbar, move the pointer over the edge you want to stretch and then click and drag to the border to change the size of the toolbar.

 Customizing Toolbars 

As your experience with C# grows, you'll find that you use certain functions repeatedly. To increase your productivity, you can customize any of C#'s toolbars, and you can create your own from scratch. You can even customize the C# menu and create your own menus. To customize toolbars and menus, you use the Customize dialog box shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig09 Figure 2.9], which is accessed by choosing View, Toolbars, Customize.

Figure 2.9. Create new toolbars or customize existing ones to fit the way you work.

graphics/02fig09.jpg

graphics/bookpencil.gif I strongly suggest that you don't modify the existing C# toolbars. Instead, create new toolbars. If you modify the predefined toolbars, you may find that you've removed tools that I refer to in later examples. If you do happen to change a built-in toolbar, you can reset it to its original state by selecting it in the Customize dialog box and clicking Reset.

The Toolbars tab on the Customize dialog box shows you a list of all the existing toolbars and menus. The toolbars and menus currently visible have a check mark next to them. To toggle a toolbar or menu between visible and hidden, click its check box.

 Creating a New Toolbar 

You're now going to create a new toolbar to get a feel for how toolbar customization works. Your toolbar will contain only a single button, which will be used to call C#'s Help program.

To create your new toolbar, follow these steps:

  1. From the Toolbars tab of the Customize dialog box, click New.
  2. Enter My Help as the name for your new toolbar when prompted.
  3. Click OK to create the new toolbar.

After you've entered a name for your toolbar and clicked OK, your new toolbar appears, floating on the screen�most likely somewhere outside the Customize dialog box (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig10 Figure 2.10]). Dock your toolbar now by double-clicking the blank area on the toolbar (the area where a button would ordinarily appear). Your screen should look like the one in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig11 Figure 2.11].

Figure 2.10. New toolbars are pretty tiny; they have no buttons on them.

graphics/02fig10.jpg

Figure 2.11. It's easier to customize toolbars when they're docked.

graphics/02fig11.jpg

 Adding Buttons to a Toolbar 

Now that you have an empty toolbar, the next step is to add the desired command buttons to it. Click the Commands tab of the Customize dialog box to display all the available commands.

The Commands tab contains the following:

  • A list of command categories
  • A list of the commands for the selected command category

The Categories list shows all available command categories, such as File and Edit functions. When you select a category, all the available commands for that category are shown in the list on the right.

You're going to add a toolbar button that appears as a Help icon and that actually displays Help when clicked.

To add the command button to your toolbar, follow these steps:

  1. Locate and select the category Help. All the available commands for the Help category will appear in the list on the right.
  2. From the Commands list, click and drag the Contents command to your custom toolbar. As you drag, the pointer changes to an arrow pointing to a small gray box. At the lower-right corner of the pointer is a little hollow box with an x in it. This indicates that the location over which the pointer is positioned is not a valid location to drop the command.
  3. As you drag the command over your toolbar (or any other toolbar for that matter), the x in the little box will change to a plus sign (+), indicating that the command can be placed in the current location. In addition, an insertion point (often called an I-beam because that's what it looks like) appears on the toolbar to indicate where the button would be placed if the command were dropped at that spot. When an I-beam appears on your toolbar and the pointer box contains a plus sign, release the mouse button. Your toolbar will now look like the one in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig12 Figure 2.12].
Figure 2.12. Building toolbars is a simple matter of dragging and dropping commands.

graphics/02fig12.jpg

You can alter any button on a toolbar by right-clicking the button to access its shortcut menu and then choosing Change Button Image. Feel free to experiment with changing the image of the button on your custom toolbar, but be sure to leave the buttons on the built-in toolbars as they are.

To remove a button from a toolbar, drag the button to remove it from the toolbar and drop it somewhere other than on the same toolbar. If you drop the button onto another toolbar, the button is removed from the original toolbar and placed on the toolbar on which you dropped it. If you drop the button in a location where no toolbar exists, the button is simply removed from the toolbar.

graphics/bookpencil.gif You can drag command buttons only in Customize mode. If you attempt to drag an item during normal operation, you'll simply click the button.
graphics/bulb.gif Although these techniques are illustrated using toolbars, they apply to menus, as well.
Moving Buttons on a Menu or Toolbar 

You should always attempt to group command buttons logically. For example, the Edit functions are all grouped together on the Standard toolbar, as are the File operations. A separator (space) is used to separate groups. To create a separator space, right-click the button that starts a new group and choose Begin a Group from the button's shortcut menu.

You probably won't get the exact groupings you want when you first add commands to a toolbar, but that's not a problem because you can change the position of a button at any time. To move a button on a toolbar, drag the button and drop it in the desired location (remember, you have to be in Customize mode to do this). To move a button from one toolbar to another, drag the button from its toolbar and drop it at the preferred location on the desired toolbar.

Now that your toolbar is complete (Hey, I never said it'd be fancy), click Close on the Customize dialog box to exit Customize mode. All toolbars, including the one you just created, are no longer in Customize mode and they can't be modified. Click the button you placed on your new toolbar and C#'s Help will appear.

Because your custom toolbar really doesn't do much, hide it now to save screen real estate by right-clicking any toolbar to display the Toolbar shortcut menu and then deselecting My Help.

graphics/bookpencil.gif As you work your way through this book, you should always have the Standard toolbar and menu bar displayed on your screen.

Typically, you should customize toolbars only after you're very familiar with the available functions and only after you know which functions you use most often. I recommend that you refrain from modifying any of the predefined toolbars until you're quite familiar with C#. As you become more comfortable with C#, you can customize the toolbars to make your project work area as efficient as possible.

>

Adding Controls to a Form Using the Toolbox

The toolbox is used to place controls, such as the common text box and list box, onto a form. The default toolbox you see when you first run C# is shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig13 Figure 2.13]. The buttons labeled Data, Components, Windows Forms, and so on are actually tabs, although they don't look like standard tabs. Clicking any of these tabs causes a related set of controls to appear. The default tab is the Windows Forms tab, and it contains many great controls you can place on Windows forms (the forms used to build Windows applications, in contrast to Web applications). All the controls that appear by default on the tabs are included with C#, and these controls are discussed in detail in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch07.htm#ch07 Hour 7], "Working with Traditional Controls," and [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch08.htm#ch08 Hour 8], "Advanced Controls." You'll learn how to add other controls to your toolbox as well.

Figure 2.13. The standard toolbox contains many useful controls you can use to build robust user interfaces.

graphics/02fig13.jpg

You can add a control to a form in one of three ways:

  • In the toolbox, click the tool that you want to place on a form, and then click and drag on the form where you want the control placed (essentially, you're drawing the border of the control). The location at which you start dragging is used for the upper-left corner of the control, and the lower-right corner is the point at which you release the mouse button and stop dragging.
  • Double-click the desired control in the toolbox. When you double-click a control in the toolbox, a new control of the selected type is placed in the upper-left corner of the form. The control's height and width are set to the default height and width of the selected control type.
  • Drag a control from the toolbox and drop it somewhere on a form.
graphics/bulb.gif If you prefer to draw controls on your forms by clicking and dragging, I strongly suggest that you dock the toolbox to the right or bottom edge of the design environment or float it; the toolbar tends to interfere with drawing controls when it's docked to the left edge, because it obscures part of the form.

The very first item on the Windows Forms tab, titled Pointer, isn't actually a control. When the pointer item is selected, the design environment is placed in a select mode rather than in a mode to create a new control. With the pointer item selected, you can select a control (by clicking it) to display all its properties in the Properties window; this is the default behavior.

Setting Object Properties Using the Properties Window 

When developing the interface of a project, you'll spend a lot of time viewing and setting object properties using the Properties window (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig14 Figure 2.14]). The Properties window contains four items:

  • An object drop-down list
  • A list of properties
  • A set of tool buttons used to change the appearance of the properties grid
  • A section showing a description of the selected property
Figure 2.14. Use the Properties window to view and change properties of forms and controls.

graphics/02fig14.jpg

 Selecting an Object and Viewing Its Properties 

The drop-down list at the top of the Properties window contains the name of the form with which you're currently working and all the controls (objects) on the form. To view the properties of a control, select it from the drop-down list or click the control on the form. You must have the pointer item selected in the toolbox to select an object by clicking it.

 Viewing and Changing Properties 

The first two buttons in the Properties window (Categorized and Alphabetic), enable you to select the format in which you view properties. When you select the Alphabetic button, the selected object's properties are listed in the Properties window in alphabetical order. When you click the Categorized button, all the selected object's properties are displayed by category. For example, the Appearance category contains properties such as BackColor and BorderStyle. When working with properties, select the view you're most comfortable with and feel free to switch back and forth between the views.

The Properties pane of the Properties window is used to view and set the properties of a selected object. You can set a property in one of the following ways:

  • Type in a value
  • Select a value from a drop-down list
  • Click a Build button for property-specific options
graphics/bookpencil.gif Many properties can be changed by more than one of these methods.

To better understand how changing properties works, follow these steps:

  1. Start by creating a new Windows Application project. Name this project Changing Properties.
  2. Add a new text box to a form by double-clicking the TextBox tool in the toolbox. You're now going to change a few properties of the new text box.
  3. Select the Name property in the Properties window by clicking it, and then type in a name for the text box�call it txtComments.
  4. Click the BorderStyle property and try to type in the word Big�you can't; the BorderStyle property supports only selecting values from a list. You can type a value that exists in the list, however. When you selected the BorderStyle property, a drop-down arrow appeared in the value column. Click this arrow now to display a list of the values that the BorderStyle property accepts. Select FixedSingle and notice how the appearance of the text box changes. To make the text box appear three dimensional again, open the drop-down list and select Fixed3D.
  5. Select the BackColor property, type in some text, and press the Tab key to commit your entry. C# displays an Invalid Property Value error. This happened because, although you can type in text, you're restricted to entering specific values (in the case of BackColor, the value must be a number within a specific range or a named color). Click the drop-down arrow of the BackColor property and select a color from the drop-down list. (Selecting colors using the Color Palette is discussed later in this hour, and detailed information on using colors is provided in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch10.htm#ch10 Hour 10], "Drawing and Printing.")
  6. Select the Font property. Notice that a Build button appears (a small button with three dots on it). When you click the Build button, a dialog box specific to the property you've selected appears. In this instance, a dialog box that allows you to manipulate the font of the text box appears (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig15 Figure 2.15]). Different properties display different dialog boxes when you click their Build buttons.
Figure 2.15. The Font dialog box gives you complete authority over the font of a control.

graphics/02fig15.jpg

By clicking a property in the Properties window, you can easily tell the type of input the property requires.

 Working with Color Properties 

Properties that deal with colors, such as BackColor and ForeColor, are unique in the way in which they accept values, yet all color-related properties behave the same way. In C#, all colors are expressed as a set of three numbers, each number having a value from 0 to 255. The set of numbers represents the Red, Green, and Blue (RGB) components of the color, respectively.

graphics/bookpencil.gif The value 0,255,0, for instance, represents pure green, whereas the values 0,0,0 represent black and 255,255,255 represents white. (See [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch10.htm#ch10 Hour 10] for more information on the specifics of working with color.)

A color rectangle is displayed for each color property in the Properties window; this color is the selected color for the property. Text is displayed next to the colored rectangle. This text is either the name of a color or a set of RGB values that defines the color. Clicking in a color property causes a drop-down arrow to appear, but the drop-down you get by clicking the arrow isn't a typical drop-down list. [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig16 Figure 2.16] shows what the drop-down list for a color property looks like.

Figure 2.16. The color drop-down list enables you to select from three sets of colors.

graphics/02fig16.jpg

The color drop-down list is composed of three tabs: Custom, Web, and System. Most color properties use a system color by default. [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch10.htm#ch10 Hour 10] goes into great detail on system colors, so I only want to mention here that system colors vary from computer to computer; they are the colors determined by the user when he or she right-clicks the desktop and chooses Properties from the desktop's shortcut menu. Use a system color when you want a color to be one of the user's selected system colors. When a color property is set to a system color, the name of the color appears in the property sheet.

The Custom tab shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig17 Figure 2.17] is used to specify a specific color, regardless of the user's system color settings; changes to system colors have no effect on the property. The most common colors appear on the palette of the Custom tab, but you can specify any color you desire.

Figure 2.17. The Custom tab of the color drop-down list lets you specify any color imaginable.

graphics/02fig17.jpg

graphics/bookpencil.gif The colors visible in the various palettes are limited by the number of colors that can be produced by your video card. If your video card doesn't support enough colors, some will appear dithered, which means they will appear as dots of colors rather than as a true, solid color.

The bottom two rows in the Custom color palette are used to mix your own colors. To assign a color to an empty color slot, right-click a slot in one of the two rows to access the Define Color dialog box (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig18 Figure 2.18]). Use the controls on the Define Color dialog box to create the color you desire, and then click Add Color. The new color appears on the color palette in the slot you selected, and it is automatically assigned to the current property.

Figure 2.18. The Define Color dialog box lets you create your own colors.

graphics/02fig18.jpg

The Web tab is used to pick colors from a list of named colors for building Web pages.

 Viewing Property Descriptions 

It's not always immediately apparent just exactly what a property is or does�especially for new users of Visual Studio. The Description section at the bottom of the Properties window shows a simple description of the selected property (refer to [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig14 Figure 2.14]). To view a description, simply click a property or value area of a property.

You can hide or show the Description section of the Properties window at any time by right-clicking anywhere within the Properties window (other than in the value column or on the title bar) to display the Properties window shortcut menu and choosing Description. Each time you do this, you toggle the Description section between visible and hidden. To change the size of the Description box, click and drag the border between it and the Properties pane.

Managing Projects 

Before you can effectively create an interface and write code, you need to understand what makes up a C# project and how to add and remove various components from within your own projects. In this section, you'll learn about the Solution Explorer window and how it's used to manage project files. You'll also learn specifics about projects and project files, as well as how to change a project's properties.

Managing Project Files with the Solution Explorer 

As you develop projects, they'll become more and more complex, often containing many objects such as forms and modules. Each object is defined by one or more files. In addition, you can build complex solutions composed of more than one project. The Solution Explorer window shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig19 Figure 2.19] is the tool for managing all the files in a simple or complex solution. Using the Solution Explorer, you can add, rename, and remove project files, as well as select objects to view their properties. If the Solution Explorer window isn't visible on your screen, show it now by choosing Solution Explorer from the View menu.

Figure 2.19. Use the Solution Explorer window to manage all the files that make up a project.

graphics/02fig19.jpg

To better understand the Solution Explorer window, follow these steps:

  1. Locate the Picture Viewer program you created in the Quick Tour by choosing File, Open, and then clicking Project.
  2. Open the Picture Viewer project. The file you need to select is located in the Picture Viewer folder that C# created when the project was constructed. The file has the extension .sln (for solution). If you're asked whether you want to save the current project, choose No.
  3. Select the Picture Viewer project item in the Solution Explorer. When you do, a button becomes visible toward the top of the window. This button has a picture of pieces of paper and has the ToolTip Show All Files (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig20 Figure 2.20]). Click this button and the Solution Explorer displays all files in the project.
Figure 2.20. Notice that the form you defined appears as two files in the Solution Explorer.

graphics/02fig20.jpg

Your design environment should now look like the one in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig20 Figure 2.20]. If your screen looks much different from the one in this figure, use the techniques you've learned in this hour to change your design environment so that it's similar to the one shown here. Be sure to widen the Solution Explorer window so that you can read all the text it contains.

graphics/bookpencil.gif Some forms and other objects may be composed of more than one file. By default, C# hides project files that you don't directly manipulate. Click the plus sign (+) next to the form item and you'll see a sub item titled Form1.resx. You'll learn about these additional files in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch05.htm#ch05 Hour 5], "Building Forms�Part I." For now, click the Show All Files button again to hide these related files.

You can view any object listed within the Solution Explorer using the object's default viewer by double-clicking the object. Each object has a default viewer but may actually have more than one viewer. For instance, a form has a Form Design view as well as a Code view. By default, double-clicking a form in the Solution Explorer displays the form in Form Design view, where you can manipulate the form's interface.

You've already learned one way to access the code behind a form�double-click an object to access its default event handler. You'll frequently need to get to the code of a form without adding a new event handler. One way to do this is to use the Solution Explorer. When a form is selected in the Solution Explorer, buttons are visible at the top of the Solution Explorer window that allow you to display the code editor or the form designer, respectively.

You'll use the Solution Explorer window so often that you'll probably want to dock it to an edge and set it to Auto Hide, or perhaps keep it visible all the time. The Solution Explorer window is one of the easiest to get the hang of in C#; navigating the Solution Explorer window will be second nature to you before you know it.

 Working with Solutions 

In truth, the Solution Explorer window is the evolution of the Project Explorer window from versions of Visual Studio prior to .NET, and the two are similar in many ways. Understanding solutions is easier to do when you understand projects.

A project is what you create with C#. Often, the words project and program are used interchangeably; this isn't much of a problem if you understand the important distinctions. A project is the set of source files that make up a program or component, whereas a program is the binary file that you build by compiling source files into something such as a Windows executable file (.exe). Projects always consist of a main project file and may be made up of any number of other files, such as form files, module files, or class module files. The main project file stores information about the project�all the files that make up the project, for example�as well as properties that define aspects of a project, such as the parameters to use when the project is compiled into a program.

What then, is a solution? As your abilities grow and your applications increase in complexity, you'll find that to accomplish your development goals, you'll have to build multiple projects that work harmoniously. For instance, you might build a custom user control such as a custom data grid that you use within other projects you design, or you may isolate the business rules of a complex application into separate components to run on isolated servers. All the projects used to accomplish those goals are collectively called a solution. Therefore, a solution (at its most basic level) is really nothing more than a grouping of projects.

graphics/bulb.gif You should group projects into a single solution only when the projects relate to one another. If you have a number of projects that you're working on, but each of them is autonomous, work with each project in a separate solution.
Understanding Project Components 

As I stated earlier, a project always consists of a main project file, and it may consist of one or more secondary files, such as files that make up forms or code modules. As you create and save objects within your project, one or more corresponding files are created and saved on your hard drive. All files that are created for C# source objects have the extension .cs, designating that they define C# objects. Make sure that you save your objects with understandable names, or things might get confusing as the size of your project grows.

All the files that make up a project are text files. Some objects, however, need to store binary information, such as a picture, for a form's BackgroundImage property. Binary data is stored in an XML file (which is still a text file). Suppose you had a form with an icon on it. You'd have a text file defining the form (its size, the controls on it, and the code behind it), and an associated resource file with the same name as the form file but with the extension .resx. This second file would be in XML format and would contain all the binary data needed to create the form.

graphics/bookpencil.gif If you want to see what the source file of a form file looks like, use Notepad to open a form file on your computer. Don't save any changes to the file, however, or it may never work again.

The following is a list of some of the components you may use in your projects:

  • Forms Forms are the visual windows that make up the interface of your application. Forms are defined using a special type of module.
  • Class Modules Class modules are a special type of module that enable you to create object-oriented applications. Throughout the course of this book, you're learning how to program using an object-oriented language, but you're mostly learning how to use objects supplied by C#. In [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch17.htm#ch17 Hour 17], "Designing Objects with Classes," you'll learn how to use class modules to create your own objects. Forms are derived from a special type of class module.
  • User Controls User controls (formerly ActiveX controls, which are formerly OLE controls) are controls that can be used on the forms of other projects. For example, you could create a User control with a calendar interface for a contact manager. Creating user controls requires the skill of an experienced programmer; therefore, I won't be covering them in this book.
 Setting Project Properties 

C# projects have properties, just as other objects do, such as forms and controls. Projects have lots of properties, many of them relating to advanced functionality that I won't be covering in this book. However, you need to be aware of how to access project properties and how to change some of the more commonly used properties.

To access the properties for a project, right-click the project in the Solution Explorer window and choose Properties from the shortcut menu. Do this now.

graphics/bookpencil.gif In earlier versions of Visual Studio, you accessed the project properties via the Project menu. You can still do this, but you must have the project selected in the Solution Explorer, or the Properties menu won't appear on the Project menu. If you don't remember this, you could spend a lot of time trying to find the properties�I sure did.

The Tree View control on the left side of the dialog box is used to display a property page (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig21 Figure 2.21]). When you first open the dialog box, the General page is visible. On this page, the setting you'll need to worry about most is the Startup Object property. The Startup Object setting determines the name of the class that contains the Main() method that you want called on program startup. The (Not Set) option, as shown in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig21 Figure 2.21], is valid if only one Main() method exists in your application.

Figure 2.21. Project properties let you tailor aspects of the project as a whole.

graphics/02fig21.jpg

The Output Type option determines the type of compiled component defined by this source project. When you create a new project, you select the type of project to create (such as Windows Application), so this field is always filled in. At times, you might have to change this setting after the project has been created, and this is the place to do so.

Notice that the project folder, project filename, and output name are displayed on this page as well. If you work with a lot of projects, you may find this information valuable, and this is certainly the easiest spot to obtain it.

graphics/bookpencil.gif The output name determines the filename created when you build a distributable component. Distributing applications is discussed in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch22.htm#ch22 Hour 22].

As you work through the hours in this book, I'll refer to the Project Properties dialog box as necessary, explaining pages and items in context with other material.

 Adding and Removing Project Files 

When you first start C# and create a new Windows Application project, C# creates the project with a single form. You're not limited to having one form in a project, however; you can create new forms or add existing forms to your project at will. You can also create and add code files and classes, as well as other types of objects.

You can add a new or existing object to your project in one of three ways:

  • Choose the appropriate menu item from the Project menu.
  • Click the small drop-down arrow that is part of the Add New Item button on the Standard toolbar, and then choose the object type from the drop-down list that is displayed (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig22 Figure 2.22]).


  • Right-click the project name in the Solution Explorer window, and then choose Add from the shortcut menu to access a submenu from which you can select object types.

When you select Add ObjectType from any of these menus, a dialog box appears, showing you the objects that can be added to the project. Your chosen item is selected by default (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig23 Figure 2.23]). Simply name the object and click Open to create a new object of the selected type. To create an object of a different type, click the type to select it, name it, and then click Open.

Figure 2.23. Regardless of the menu option you select, you can add any type of object you want using this dialog box.

graphics/02fig23.jpg

Adding new forms and modules to your project is easy, and you can add as many as you like. You'll come to rely more heavily on the Solution Explorer to manage all the objects in the project as the project becomes more complex.

Although it won't happen as often as adding project files, you may sometimes need to remove an object from a project. Removing objects from your project is even easier than adding them. To remove an object, simply right-click the object in the Solution Explorer window and select Exclude from Project. This removes the object from the file but does not delete the source file from the disk. Selecting Delete, on the other hand, removes the file from the project and deletes it from the disk. Don't select Delete unless you want to totally destroy the file and you're sure that you'll never need it again in the future.

Getting Help 

Although C# was designed to be as intuitive as possible, you'll find that you occasionally need assistance in performing a task. It doesn't matter how much you know, C# is so complex and contains so many features that you'll have to use Help sometimes. This is particularly true when writing C# code; you won't always remember the command you need or the syntax of the command. Fortunately, C# includes a comprehensive Help feature.

To access Help from within the design environment, press F1. Generally speaking, when you press F1, C# shows you a help topic directly related to what you're doing. This is known as context-sensitive help, and when it works, it works well. For example, you can display help for any C# syntax or keyword (functions, objects, methods, properties, and so on) when writing C# code by typing the word into the code editor, positioning the cursor anywhere within the word (including before the first letter or after the last), and pressing F1. You can also get to help from the Help menu on the menu bar.

graphics/bookpencil.gif C#'s Help won't be displayed if your program is in Run mode when you press F1. Instead, the help for your application will appear�if you've created Help.

Help displays topics directly within the design environment instead of in a separate window. This is a new feature of .NET. Personally, I think this method is considerably inferior to the old style of Visual Studio having Help float above the design environment. When Help is displayed within the design environment, you can't necessarily see the code, form, or other object with which you're working. To make Help float above the design environment, choose Options from the Tools menu to display the Options dialog box, click Help in the Tree view on the left, and select External Help.

C# includes a Help feature called Dynamic Help. To display the Dynamic Help window, choose Dynamic Help from the Help menu. The Dynamic Help window shows Help links related to what it is you're working on (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#ch02fig24 Figure 2.24]). For instance, if you select a form, the contents of the Dynamic Help window show you Help links related to forms. If you click a text box, the contents of the Dynamic Help window adjust to show you Help links related to text boxes. This is an interesting feature, and you may find it valuable.

graphics/02fig24.jpg

Summary 

In this hour, you learned how to use the Visual Studio Start page�your gateway to C#. You learned how to create new projects and how to open existing projects. The C# environment is your workspace, toolbox, and so much more. You learned how to navigate the environment, including how to work with design windows (hide, show, dock, and float).

You'll use toolbars constantly, and now you know how to modify them to suit your specific needs. You learned how to create new toolbars and how to modify existing toolbars. This is an important skill that shouldn't be overlooked.

C# has many different design windows, and in this hour, you began learning about some of them in detail. You learned how to get and set properties using the Properties window, how to manage projects using the Solution Explorer, and how to add controls to a form using the toolbox. You'll use these skills often, so it's important to get familiar with them right away. Finally, you learned how to access C#'s Help feature, which I guarantee you will find very important as you learn to use C#.

C# is a vast and powerful development tool. Don't expect to become an expert overnight; this is simply impossible. However, by learning the tools and techniques presented in this hour, you've begun your journey. Remember, you'll use most of what you learned in this hour each and every time you use C#. Get proficient with these basics and you'll be building cool programs in no time!

Q&A
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#qad1e9108 Q1:] How can I easily get more information about a property when the Description section of the Properties window just doesn't cut it?
A1: Click the property in question to select it, and then press F1; context-sensitive help applies to properties in the Properties window, as well.
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/02.htm#qad1e9118 Q2:] I find that I need to see a lot of design windows at one time, but I can't find that "magic" layout. Any suggestions?
A2: Run at a higher resolution. Personally, I won't develop in less than 1024x768. As a matter of fact, all my development machines have two displays, both running at this resolution. You'll find that any investment you make in having more screen real estate will pay you big dividends.

Workshop

The Workshop is designed to help you anticipate possible questions, review what you've learned, and get you thinking about how to put your knowledge into practice. The answers to the quiz are in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01.htm#app01 Appendix A], "Answers to Quizzes/Exercises."

 Quiz
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec2.htm#ch02ans01 1:] How can you make the Visual Studio Start Page appear at startup if this feature has been disabled?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec2.htm#ch02ans02 2:] Unless instructed otherwise, you are to create what type of project when building examples in this book?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec2.htm#ch02ans03 3:] To make a docked design window appear when you hover over its tab and disappear when you move the mouse away from it, you change what setting of the window?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec2.htm#ch02ans04 4:] How do you access the Toolbars menu?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec2.htm#ch02ans05 5:] What design window do you use to add controls to a form?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec2.htm#ch02ans06 6:] What design window is used to change the attributes of an object?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec2.htm#ch02ans07 7:] To modify the properties of a project, you must select the project in what design window?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec2.htm#ch02ans08 8:] Which Help feature adjusts the links it displays to match what it is you are doing?
 Exercises
  1. Create a custom toolbar that contains Save All, Start, and Stop Debugging�three buttons you'll use a lot throughout this book.
  2. Use the Custom Color dialog box to create a color of your choice, and then assign the color to the BackColor property of a form.