Working with Projects and Solutions
Each time you want to develop an application, you create a project. A project is a collection of files, such as code files, resources, data, and every kind of file you need to build your final assembly. A Visual Basic project is represented by a .Vbproj file, which is an Extensible Markup Language (XML) file containing all the information required by Visual Studio to manage files that constitute your project. Projects are organized into solutions. A solution is basically a container for projects. In fact, solutions can contain infinite projects of different kinds, such as Visual Basic projects, projects produced with programming languages other than Visual Basic, class libraries, projects for Windows client applications, Windows Communication Foundation services, and so on. In other words, a solution can include each kind of project you can create with Visual Studio 2012. Solutions also can contain external files, such as documents or help files, and are represented by a .Sln file that has an XML structure and stores information required to manage all the projects contained in the solution. Visual Studio 2012 can also open solutions created with previous versions of the IDE.
Typically you manage your projects and solutions using the Solution Explorer window discussed later in this chapter. We now focus on the creation of Visual Basic 2012 projects.
Creating Visual Basic Projects
Creating a new Visual Basic project is a simple task. You can click either the File, New Project command or the New Project link from the Start Page. In both cases, Visual Studio shows the New Project window, which you can see in Figure 2.5.
Figure 2.5. The New Project window for Visual Basic.
As you can see, the look of the New Project window is quite different from previous versions of Visual Studio and still retakes the Windows 8 style. To understand what kind of Visual Basic applications you can create, you simply need to select the Visual Basic node on the left side of the window. After you click the node, you see a lot of different kinds of applications you can create with Visual Basic 2012, such as Windows applications, web applications, Office System customizations, Silverlight applications, Windows Communication Foundation Services, the new Windows 8 Store Apps for Windows 8, and so on.
Each kind of application is represented by a specific project template, which provides a skeleton of a project for that particular kind of application, including all the references or the basic code files required. For example, the WPF Application project template provides a skeleton of a project for creating a Windows application using the Windows Presentation Foundation technology, therefore including references to the WindowsBase.dll assembly, specific Imports directives, and WPF objects represented by the appropriate code files. Moreover, Visual Studio will automatically enable the WPF designer. Notice the detailed description for each template on the right side of the window every time you select a particular template.
When you create a new project, Visual Studio usually creates a solution containing that project. If you plan to add other projects to the solution, it can be a good idea to create a directory for the solution. This allows for a better organization of your projects, because one directory can contain the solution file and this directory can then contain subdirectories, each of them related to a specific project. To accomplish this, ensure that the Create Directory for Solution check box is selected. The New Project window also offers some interesting features: the .NET Framework multi-targeting, the ability of searching through templates, the ability of managing templates, and finding samples on the Internet. We now discuss each of these features.
Multi-targeting
Like in Visual Studio 2010, in Visual Studio 2012 you can choose which version of the .NET Framework your application targets. This can be useful if you plan to develop applications with high compatibility levels and without new language features but still want to take advantage of the new features of the IDE. You can choose one of the following:
- .NET Framework 4.5 (proposed by default)
- .NET Framework 4.0
- .NET Framework 3.5
- .NET Framework 3.0
- .NET Framework 2.0
To accomplish this, just select the appropriate version from the combo box at the top of the window, as shown in Figure 2.6.
Figure 2.6. Choosing the .NET Framework version for your application.
Accessing Recent and Online Templates
Visual Studio 2012 provides the capability to access the most recently used templates and install additional templates from the Internet. You can easily access the most recently used project templates by clicking the Recent Templates item on the left side of the New Project window. In this way, you get a list of the recently used project templates in case you still need them (see Figure 2.7). You can also find additional online templates and install them to the local system. To accomplish this, simply click the Online Templates item in the New Project window. Visual Studio checks for the availability of online templates and shows a list of all the available templates (see Figure 2.8).
Figure 2.7. Accessing the most recently used projects templates.
Figure 2.8. Additional online templates you can add to Visual Studio 2012.
As you can see, Visual Studio is listing all the online templates for both Visual Basic and Visual C#, showing a description of the template, information about the author, and a small picture with ratings when available. To download and install a template, simply double-click its name. After a few seconds you will be prompted to agree to the installation. You will be warned in the case that the new extension for Visual Studio does not contain a digital signature. If you trust the publisher and want to continue, click Install. After a few seconds, you will see the newly installed project templates available among the other ones.
As in the previous versions of Visual Studio, you can still export projects as reusable project templates. We discuss this feature later in Chapter 53, “Advanced IDE Features.”
Searching for Installed Templates
Visual Studio 2012 provides lots of default project templates, and as you saw before, you have the ability of adding your own custom ones. Therefore, finding the necessary template at a certain moment can be difficult. Because of this, the New Project window provides a search box that is located in the upper-right corner of the window (see Figure 2.9). Just begin typing the name of the project template you are looking for, and the New Project window shows all the project templates that match your choice. Each time you type a character, the window updates showing the results matching your search string. As you can see in Figure 2.9, Visual Studio is showing all the project templates whose names match the wor search string.
Figure 2.9. Searching for installed project templates using the new search feature.
Finding Code Samples on the Internet
One of the great new features of Visual Studio 2012 is the ability to search for sample code on the Internet directly from the New Project dialog box. Visual Studio performs this search inside the MSDN Code Gallery (http://code.msdn.microsoft.com), a website where developers can publish sample code they want to share with others. To find code samples, click the Samples item at the bottom of the list of templates in the New Project dialog box (see Figure 2.5). After you select this item, you will be able to browse the Code Gallery from within the dialog box. For example, if you search for a specified language, you will then be able to discover samples of each language based on technologies (such as Windows, Web, Cloud, Windows Phone, and so on). For each of these categories, you will find additional subcategories where samples are divided by framework; for instance, Windows development samples are divided into WPF, Windows Forms, Windows Runtime, and so on. Figure 2.10 shows how you can browse samples online from Visual Studio 2012.
Figure 2.10. Browsing online code samples from Visual Studio 2012.
You select a sample from the list and then click OK or simply double-click the sample. Visual Studio 2012 will download the sample to your hard disk and will ask you to accept the license agreement that the developer who published the code added to the sample. After it’s downloaded, the sample is opened in Visual Studio 2012 under the form of a normal project. This is possible because, to upload a sample to the gallery, you must supply a full project (or an entire solution) to enable other developers to reuse your code with ease.
Creating Reusable Projects and Items Templates
As in the previous versions, in Visual Studio 2012 you can create your custom projects and items templates, exporting them to disk and making them available within the IDE. (This topic is discussed in Chapter 53.) Now that you have seen the main new features for project creation, you are ready to create your first Visual Basic project.
Creating Your First Visual Basic 2012 Project
This section shows how easily you can create a Visual Basic 2012 application. If you have experience with Visual Studio, you’ll notice the differences in the development environments between the new version and the previous ones. You can create a new project for the Console by first opening the New Project window and then selecting the Console Application project template.
Name the new project MyFirst2012Program and then click OK (see Figure 2.11).
Figure 2.11. Creating your first VB 2012 application.
After a few seconds the new project is ready to be edited. Figure 2.12 shows the result of the project creation.
Figure 2.12. The creation of the first VB 2012 project.
As mentioned at the beginning of this chapter, the code editor and floating windows are based on Windows Presentation Foundation. You see the new Windows 8 look for the tooling, but this change does not modify your development experience. You should feel at home with the new version of the environment because you can still find tools as in the previous versions—maybe just with fewer colors. For example, you can access Solution Explorer, the Properties window, or the Error List exactly as you did before. An interesting feature in the code editor is that by pressing Ctrl and moving up and down the mouse wheel, you can zoom in and out with the code editor without the need to change the font settings each time the Visual Studio options changes. For our testing purposes, we could add a couple of lines of code to the Main method, which is the entry point for a Console application. Listing 2.1 shows the complete code for creating a VB 2012 application.
Listing 2.1. Creating the First VB 2012 Application
Module Module1 Sub Main() Console.WriteLine("Hello Visual Basic 2012!") Console.ReadLine() End Sub End Module
The code simply shows a message in the Console window and waits for the user to press a key. This is obviously a basic application, but you need it as the base for understanding other topics in this chapter.
Finding Visual Basic Projects
As in the previous versions, Visual Studio 2012 stores by default its information in a user-level folder called Visual Studio 2012 that resides inside the My Documents folder. Here you can find settings, add-ins, code snippets, and projects. For example, if you run Windows Vista, Windows 7, or Windows 8, your projects should be located in C:\Users\UserName\Documents\Visual Studio 2012\Projects, in which UserName stands for the user logged in to Windows. Of course, you can change the default projects directory by opening the Options window (Tools, Options command), selecting the Projects and Solutions item on the left side, and replacing the value for the Projects location text box.
Working with the Code Editor
The code editor, together with designers, is the place where you spend most of your developer life in Visual Studio, so knowing how to get the best out of it is very important. This is what I explain throughout the whole book. In this chapter you see just a few of the code editor features because the other ones are related to specific topics discussed later. We now focus on the zoom functionality in the code editor and IntelliSense.
Zooming the Code
You can zoom the code editor in and out by simply pressing the Ctrl key and moving the mouse wheel up and down. This is a useful feature, particularly if you are a presenter of technical speeches because you can enlarge the code without modifying Visual Studio settings in the Options window. Figure 2.13 shows an example of this feature; notice how the font for the code is greater than the default settings and how the zoom percentage is visible at the bottom-left corner.
Figure 2.13. The code editor zoom feature enables real-time enlarging and reducing of the font size of the code without changing settings in Visual Studio.
If you already used this feature in Visual Studio 2010, you might remember how the scroll bar on the right also changed its size according to the zoom. This has been fixed in Visual Studio 2012; the bar size is always the same and independent from the zoom.
IntelliSense Technology
IntelliSense is one of the most important technologies in the coding experience with Visual Studio. IntelliSense is represented by a pop-up window that appears in the code editor each time you begin typing a keyword or an identifier and shows options for auto-completing words. Figure 2.14 shows IntelliSense in action when adding a new instruction in code.
Figure 2.14. IntelliSense in action.
To auto-complete your code typing, you have the following alternatives:
- Tab—Pressing this auto-completes your words and enables you to write other code.
- Space—Pressing this auto-completes your words, adding a blank space at the end of the added identifier and enabling you to write other code.
- Enter—Pressing this auto-completes your words, adding a couple of parentheses at the end of the completed identifier and positioning the cursor on a new line. Use this technique when you need to invoke a method that does not require arguments.
- Left parenthesis—Pressing this auto-completes your words, adding a left parenthesis at the end of the completed identifier and waiting for you to supply arguments.
- Ctrl + Space—Pressing this brings up the full IntelliSense listing.
IntelliSense has been improved since Visual Studio 2008. In fact, it is activated just when typing one character and is also active versus Visual Basic reserved words. Moreover, it remembers the last member you supplied to an object if you invoke that particular object more than once. For example, if you use IntelliSense to provide the WriteLine method to the Console object as follows:
Console.WriteLine()
and then try to invoke IntelliSense on the Console object again, it proposes as the first alternative of the WriteLine method you supplied the first time. IntelliSense is important because it lets you write code more quickly and provides suggestions on which member to add to your code.