July 9, 2011, 1:44 a.m.
posted by seele
Before you can effectively create an interface and write code, you need to understand what makes up a Visual C# 2005 project and how to add and remove various components 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, including 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 (grouped sets of code). Each object is defined by one or more files on your hard drive. In addition, you can build complex solutions composed of more than one project. The Solution Explorer window shown in Figure 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 View, Solution Explorer from the menu.
13. Use the Solution Explorer window to manage all the files that make up a project.
To better understand the Solution Explorer window, follow these steps:
Your Solution Explorer should now look like the one in Figure. Be sure to widen the Solution Explorer window so that you can read all the text it contains.
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 might 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 can be used 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 Visual C#; navigating the Solution Explorer window will be second nature to you before you know it.
Working with Solutions
A project is what you create with Visual C#. Often, the words project and program are used interchangeably, and 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 can 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 projectall the files that make up the project, for exampleas 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 you have to build multiple projects that work harmoniously to accomplish your goals. 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 might 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. By default, when you save a project, Visual C# creates a solution for the project. When you open the solution file, as you did in this example, all projects in the solution get loaded. If the solution contains only one project, then opening the solution is pretty much the same as opening that single project.
Did you Know?
You should group projects into a single solution only when the projects relate to one another. If you're working on a number of projects, 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 might 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. Each file that's created for a Visual C# 2005 source object has the extension .cs, denoting that it defines a Visual C# object. Make sure that you save your objects with understandable names, or things will get confusing as the size of your project grows.
All files that make up a project are text files. Some objects 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 that 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 filewith the same name as the form file but with the extension .resx. This secondary file would be in XML format and would contain all the binary data needed to create the form.
By the Way
If you want to see what the source file of a form file looks like, use Notepad to open one on your computer. Don't save any changes to the file, however, or it might never work again (</insert evil laugh here/>).
The following is a list of some of the components you might use in your projects:
Setting Project Properties
Visual Basic projects have properties, just as other objects such as controls do. Projects have many properties, many of them relating to advanced functionality not covered in this book. You need to be aware, of how to access project properties, however, and how to change some of the more commonly used properties.
To access the properties for a project, right-click the project name (Picture Viewer) in the Solution Explorer window and choose Properties from the shortcut menu. You could also double-click the project name in the Solutions Explorer to accomplish the same goal. Do one of these actions now.
The properties for a project are presented as a set of vertical tabs (see Figure).
15. The Project Properties are used to tailor the project as a whole.
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. Feel free to take a look at your Picture Viewer properties, but don't change any at this time. You can close the project properties by clicking the small X in the upper-right corner of the tab section in the IDE. You can also just click a different tab.
Adding and Removing Project Files
When you first start Visual C# 2005 and create a new Windows Application project, Visual 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 (feeling powerful yet?). You can also create and add class modules as well as other types of objects.
You can add a new or existing object to your project in one of three ways:
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 type is selected by default (see Figure). 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.
17. Regardless of the menu option you select, you can add any type of object you want using this dialog box.
Adding new forms and modules to your project is easy, and you can add as many as you want. You'll come to rely on the Solution Explorer more and more 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 might sometimes need to remove an object from a project. Removing objects from your project is even easier than adding them. To remove an object, right-click the object in the Solution Explorer window and select Delete. This not only removes the object from the project, it also deletes the source file from the disk.