Getting Started

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); } function Print() { window.focus(); if(window.print) { window.print(); window.setTimeout('window.close();',5000); } }
Team-Fly    

Special Edition Using Microsoft® Visual Basic® .NET
By Brian Siler, Jeff Spotts
Table of Contents
Chapter 4.  Understanding the Development Environment


The easiest way to learn is by doing; that is why we began the book with chapters that included the step-by-step creation of sample applications. However, the user interface changes in Visual Studio .NET are so far reaching that an in-depth exploration of them may be helpful as well. During this chapter we will explain many different aspects and new features of Visual Studio .NET. Please follow along where applicable, and feel free to refer to this chapter while reading the rest of the book.

The Visual Studio Start Page

Just about every recent update to a Microsoft product has included a Web browser interface somewhere. Visual Studio .NET is no exception to this trend. As you can see from Figure 4.1, starting the latest version of Visual Studio displays the Visual Studio Start Page, which is a Web page containing links to recent projects and other items of interest.

Figure 4.1. One result of Visual Studio's cosmetic overhaul is the addition of the Visual Studio Start Page.

graphics/04fig01.gif

The Start Page lists the names of recently opened Visual Studio projects, plus includes hyperlinks to create a new project or open a project file from disk. You can, of course, accomplish these same tasks from the File menu, although because the Web page requires only a single mouse click, you may find it to be a bit quicker.

Note

The Start Page is really a Web page; the files associated with it are stored in Visual Studio's \common\ide\html directory. An adventurous user might want to try customizing these files to make the Start Page even more useful.


The Start Page also includes sections that link you to Web resources and customizes certain settings within the development environment, which we will discuss at the end of the chapter.

Creating and Opening Projects

As with previous versions, one basic unit of work in Visual Basic is a project. When you use Visual Basic .NET, you are generally working with one or more open projects. Projects are organizers for related components (screens, code, and so forth) of an application. Solutions, which will be discussed in the following section, are containers for one or more projects. To start a new project, click the Create a New Project link on the Start Page to display the New Project dialog box (see Figure 4.2).

Figure 4.2. Visual Studio .NET's New Project dialog box includes not only Visual Basic projects, but also those for other programming languages in the Visual Studio family.

graphics/04fig02.gif

There are many different types of VB projects; the one you select depends on the type of application you want to create. There are projects to create user interfaces for both Windows and the Web, as well as projects to create services and business-layer components used by these applications. The following list summarizes the different types of projects you can create using Visual Basic .NET:

Windows Application graphics/icon25.gifCreates an application with a Windows user interface, similar to the Standard EXE application in previous versions of Visual Basic.

Class Library graphics/icon05.gifCreates a DLL containing one or more classes, which can be accessed from other applications. This type of application is where you create your business objects for use in a client-server or multi-tier application. (Similar to VB6's ActiveX DLL project.)

Windows Control Library graphics/icon26.gifCreates a Windows custom control. (Similar to an ActiveX control in previous versions of VB.)

Web Application graphics/icon22.gifCreates a Web application on a Web server. Although you can design Web applications in Visual Studio much like Windows applications, the output is a Web site instead of a Windows executable. This allows others to access your application over a network using their Web browsers.

Web Service graphics/icon24.gifCreates a project on a Web server, which provides services to other applications over the Web.

Web Control Library graphics/icon23.gifCreates a custom control using Web forms.

Console Application graphics/icon07.gifA Windows application without forms, this application does all of its input/output in a command prompt window.

Windows Service graphics/icon27.gifA service accessible over a Windows network.

Empty Project graphics/icon08.gifA blank Visual Studio project.

Empty Web Project graphics/icon09.gifA blank Visual Studio Web project.

New Project in Existing Folder graphics/icon12.gifCreates an empty project associated with a specified folder.

Many of these different project types are covered in more detail throughout the book. As you may have noticed, two main categories stand out Web projects and Windows projects. This book contains two major sections, each dealing with a specific area Part III, "Working with Windows," and Part IV, "Working with the Web."

Working with Project Files

Let's try a simple exercise in creating a new project. From the Visual Studio home page, click the link titled Create a New Project and you will see the New Project dialog box. Under the list of project types, select the Visual Basic folder. Select the Windows Application project type by single-clicking the Windows Application icon. Next, enter MyWinApp in the Name box. Also, make a mental note of the directory listed in the Location box. Finally, click the OK button and Visual Studio will set up your new project. The new project contains a single Windows form called Form1.vb. (This should be familiar to users of previous versions of Visual Basic, because the Standard EXE project also by default includes a single form.)

Next, let's look at some of the files that have been created behind the scenes, even before you have written the first line of Visual Basic code. Minimize your Visual Studio window so that you can see your Windows desktop. Right-click the My Computer icon and choose Explore from the menu to launch the Windows Explorer. Now, navigate to the directory that was listed in the Location box when you created the project. (For example, my directory is C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\MyWinApp.) Notice that several files have been created by Visual Studio .NET in this directory. The following files are most important in the context of our discussion of projects and solutions:

  • Form1.vb and Form1.resx These files contain the VB code and design of your form. They replace the .frm and .frx files from previous versions of Visual Basic.

  • MyWinApp.vbproj This file is the project file, equivalent to the .vbp and .mak files from previous versions of Visual Basic.

  • Assemblyinfo.vb Compiled components in the .NET world are known as assemblies. This file contains general information about your assembly, such as the product version number.

  • MyWinApp.sln This is the solution file. As we will learn in the next section, a solution organizes a group of one or more projects.

In addition to the previously mentioned files, Visual Studio creates some additional files and directories that contain user settings and intermediate files used during the compile process. We will discuss some of these later in the book. It is important to note that the files listed previously are the only ones necessary to move your project code to another VB developer's computer. (The solution file isn't strictly necessary because it can be recreated, but is especially helpful if your solution contains multiple projects.) The bin and obj directories contain compiler output files and will be automatically re-created if they are deleted or missing.

Tip

By default, Visual Studio stores projects in the My Documents\Visual Studio Projects folder. You may find it more convenient to create a shorter folder name in which to save your projects, especially if you like using the command prompt. I usually use place my Visual Basic projects in their own folders underneath a Projects or VBCode folder, which is located only one level deep on the hard drive. The default directory For projects can be changed in the Visual Studio Environment settings, as described at the end of this chapter.


For more information on distributing your finished programs, p.761

It is also interesting to note that that most files in Visual Studio are text-based and use the XML format. This means they can be viewed (and edited) using any text editor such as Notepad. Taken a step further, you can actually create an entire VB program using a text editor and then compile it using the command-line compiler. This means it is possible to create Visual Basic applications without even using Visual Studio .NET Of course, for large projects, using a text editor may be somewhat impractical, and we think that Visual Studio provides enough value to warrant its existence as a development tool!

Tip

Placing a shortcut to notepad.exe (or another text editor) in your SendTo directory is invaluable because it allows you to right-click any file and view it. Your SendTo directory is either located under the Documents And Settings folder for your user ID, or the Windows directory.


When you created the sample MyWinApp project, all the files were saved to disk immediately. This is possible because Visual Basic .NET requires you to set a project Name and Location before you start working on the project. The location is the home directory for your project, which contains associated project files. This is a welcome departure from the old approach, which requires manual creation of a project directory and then saving each file in a project individually. In Visual Basic .NET, the project directory is created by default using the project name, and new forms and code files you create in Visual Studio are saved in that directory.

Understanding Solutions

The term project has been around since the beginning of Visual Basic. However, starting with Visual Studio .NET, a new organizational term has been introduced the solution. Users of Visual Studio .NET should understand the difference between a project and a solution. Projects, as you already know, contain the component pieces of your application, such as forms or modules of code. Each of these pieces is stored in a separate file on the disk.

Solutions represent the Visual Studio workspace. Therefore, they may encompass multiple projects. (Solutions are roughly similar to Visual Basic project groups from previous versions, with the exception that you are always working in a solution, even if it only contains a single project.)

Note

A project can be thought of as a file that points to a bunch of other files. For example, if you have created a VB application with 10 forms, the project file is what knows these 10 forms are part of the same project. Similarly, the solution file knows which projects go together, and represents a snapshot of a group of open projects.


To further clarify, there are two solution-related options available when creating a new project:

  • Add to Solution Adds the project to whatever projects you already have open. For example, when you create a Class Library project, you will want to create another type of project (such as a Windows Application) to test your class. In this case, the solution would contain two projects.

  • Close Solution Closes whatever solution you were working on previously, and creates the new project in its own solution.

Note

Another interesting new feature in Visual Basic .NET is the ability to add items to your solution that are not necessarily related to your Visual Basic code. For example, you can associate text files, SQL queries, Word documents, or pictures with projects and solutions and launch them from within Visual Studio .NET.


To display these options, click the More button on the New Project dialog box, as pictured in Figure 4.2.

Understanding the Solution Explorer

One of the most important parts of Visual Studio .NET is the Solution Explorer, as shown in Figure 4.3. The Solution Explorer displays the names of all the different items in your project in a hierarchical list. At the top of the hierarchy is the name of the current solution; beneath the solution name each project is listed with sections for files and references. After the projects other non-project items and files are listed.

Figure 4.3. The Solution Explorer can be displayed from the View menu.

graphics/04fig03.gif

Adding Items to a Project

Let's continue with our sample project from the previous section, MyWinApp, to demonstrate adding items to the project using the Solution Explorer. (If you have already closed the sample application, just click on the appropriate link on the Visual Studio Home Page to re-open it.)

Figure 4.3 shows the Solution Explorer as we left it from the last section. Note there is one form in our project, aptly named Form1.vb. Let's add an additional form to the project. To do this, right-click the project name (second line from the top) and expand the Add menu. Choose the Add New Item option and you will see the dialog box pictured in Figure 4.4.

Figure 4.4. There are many different types of items you can add to your Visual Basic projects.

graphics/04fig04.gif

As you can see from the figure, these available project items are divided into categories. Expand the Local Project Items folder and then single-click the UI folder underneath it. This will filter the list of item templates on the right side of the screen to include only the user-interface items. For this example, we want to add a new Windows Form to our project, so single-click Windows Form in the template list. Finally, notice the default name of form2.vb is suggested. Normally good programming practice would dictate that we change such a non-descriptive name, but for the purposes of this UI discussion, just click the Open button to accept the default name. The new form should be added to the project. Your Solution Explorer should now look similar to Figure 4.5.

Figure 4.5. The Solution Explorer after adding an additional form to the sample project.

graphics/04fig05.gif

Note

Users of previous versions of Visual Basic may be wondering about the new Inherited Form option, which allows adding a new form that mimics another form's visual characteristics. For more on Inherited forms, see Chapter 10, "Understanding Windows Forms."


Navigating with the Solution Explorer

Now that we have seen how you can add items to a project with the Solution Explorer, let's look further at its main purpose: project navigation. Much like using the Windows Explorer to manipulate files on your hard drive, you use the Solution Explorer to access components within your Visual Studio projects and solutions. Previous versions of Visual Basic had something called the Project Explorer, and the Solution Explorer works very much the same way with respect to navigating between forms. You can jump to either the code window or the designer window for a form by right-clicking the object and selecting the appropriate menu option, or using the toolbar.

To demonstrate, right-click the Form1.vb object in the Solution Explorer window. A menu will appear. Click View Designer and you will see the visual component of the form, which appears as a gray square. Right-click the form again and choose View Code. You will see the VB code stored in the form object. These options are also toolbar buttons on the Solution Explorer. A third button, the Properties button, allows you to view the file properties of the selected object.

Note

The file properties of a form in the Solution Explorer are different from the object properties that can be accessed by right-clicking the object in the designer.


Adding Additional Projects

Our sample solution currently only contains one project. Let's make things more interesting by adding an additional project to see how it affects the Solution Explorer. Right-click the solution name, MyWinApp, which is listed on the topmost line of the Solution Explorer. Choose Add, then New Project and you will again see the New Project dialog box pictured in Figure 4.2. This time select the Class Library project template and click the OK button. Visual Studio will create a new Class Library project in the current solution and cause the Solution Explorer to look similar to Figure 4.6.

Figure 4.6. The Solution Explorer with multiple projects open.

graphics/04fig06.gif

Note

When multiple projects are open, the project shown in boldface is the startup project. The startup project usually represents the UI portion of your solution and is the project that will be started when you click the Run button.


The solution shown in Figure 4.7 is fairly typical in that it includes a class that (presumably) contains business logic along with a UI project to use it. However, a solution is just an organizational structure for projects. Before a project can use functionality from another project, you will need to add a reference.

Figure 4.7. The References dialog box is organized according to components from the .NET framework, COM components, and Projects.

graphics/04fig07.gif

Adding References to a Project

As you may have already noticed, each project in the Solution Explorer contains a References folder. By referencing a library of functions that is external to a project, you can make those functions available to the project. Often these functions are located in a DLL file on your hard drive. One example might be a DLL that interacts with video camera hardware and provides functions to control the camera from Visual Basic. (DLL stands for Dynamic Link Library and is just a type of file that can be installed on your system. In Visual Studio .NET, Microsoft's documentation calls these groups of external functions assemblies and points out that they may be contained in a variety of files.)

Note

The fact that project references are displayed in this manner is a change new to this version of Visual Basic. In previous versions, references were only accessible from a menu.


Expand the References folder and you will see several entries, most of which begin with System. These references contain functionality provided by the .NET framework that is included in new projects by default. For example, System.Windows.Forms is necessary for drawing a form.

When you add a reference, it does not necessarily have to be an externally compiled program or group of system functions. Often during VB development you will reference other VB projects. Consider our sample solution, which contains a Class Library project, ClassLibrary1. If you want to use this class's methods from the MyWinApp project, you might create an instance of the class by placing the following lines of code somewhere in form1.vb:

 Dim objTemp As ClassLibrary1.Class1  objTemp = New ClassLibrary1.Class1 

For more information on creating your own classes, p.223

However, without adding a reference, the VB compiler will find errors in the code because it does not know the class name. To make the classes in the ClassLibrary1 project known to the MyWinApp project, you have to add a reference. Just because two projects are loaded in the same solution does not mean that their code components are meant to interact.

Let's continue our example by adding a reference. Right-click the word References under the MyWinApp project and choose Add Reference from the menu. The References dialog box shown in Figure 4.7 will appear.

Click the Projects tab, and you will see the class library project. Click the Select button and OK, and ClassLibrary1 will be added to the references list of the MyWinApp project.

If you do not feel you have a firm enough grasp on references, do not worry! This section was intended to introduce them from the point of view of the Project Explorer, rather than explore their relationship to VB code.

To learn more about assemblies, references, and namespaces, p. 113


    Team-Fly    
    Top
     



    Special Edition Using Visual Basic. NET
    Special Edition Using Visual Basic.NET
    ISBN: 078972572X
    EAN: 2147483647
    Year: 2001
    Pages: 198

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net