AL RI TE IDE MA This chapter describes Visual Studio’s integrated development environment (IDE). It explains the most important windows, menus, and toolbars that make up the environment, and shows how to customize them to suit your personal preferences. It also explains some of the tools that provide help while you are writing Visual Basic applications. GH TE D Even if you are an experienced Visual Basic programmer, you should at least skim this material.
Chapter 1 To avoid confusion, you should probably not customize the IDE’s basic menus and toolbars too much. Removing the help commands from the Help menu and adding them to the Edit menu will only cause confusion later. It’s less confusing to leave the menus more or less alone. Hide any toolbars you don’t want and create new customized toolbars to suit your needs. Then you can find the original standard toolbars if you decide you need them later.
IDE brings additional chances for confusion. It will be less confusing to shrink the Visual Basic IDE to an icon and open the file using an external editor such as Word or WordPad. If you won’t use a file very often with the project, don’t add it to the project. IDE Over view Figure 1-1 shows the IDE immediately after starting a new project. The IDE is extremely configurable, so it may not look much like Figure 1-1 after you have rearranged things to your own liking.
Chapter 1 The key pieces of the IDE are labeled with numbers in Figure 1-1. The following list briefly describes each of these pieces. ❑ (1) Menus — The menus contain standard Visual Studio commands. These generally manipulate the current solution and the modules it contains, although you can customize the menus as needed. Visual Studio changes the menus, and their contents depending on the object you currently have selected.
IDE example, when you compile an application, the IDE sends messages here to tell you what it is doing and whether it succeeded. The following sections describe the major pieces of the IDE in more detail. Menus The IDE’s menus contain standard Visual Studio commands. These are generally commands that manipulate the project and the modules it contains.
Chapter 1 Figure 1-2: The File menu holds commands that deal with the solution and its files. Following is a description of the commands contained in the File menu and its submenus: 6 ❑ New — The New submenu shown in Figure 1-2 contains commands that let you create a new Visual Basic project, Web site project (generally ASP.NET or a Web Service), or file (text file, bitmap, Visual Basic class, icon, and many others).
IDE Figure 1-3: The File\New\File command displays this dialog to let you select the new file’s type. Figure 1-4: The File\Open\Convert command displays this dialog to help you convert Visual Basic 6 applications to Visual Basic 2005. ❑ Close Project — This command closes the entire project and all of the files it contains. If you have a solution open, this command is labeled Close Solution. ❑ Save Form1.vb — This command saves the currently open file, in this example, Form1.vb. ❑ Save Form1.
Chapter 1 Figure 1-5: The File\Export Template command displays this dialog to help you create project or items templates that you can easily use in other projects. ❑ Page Setup and Print — The Page Setup and Print commands let you configure printer settings and print the current document. These commands are enabled only when it makes sense to print the current file. For example, if you are viewing a source code file or a configuration file (which is XML text), you can use these commands.
IDE Figure 1-6: The Edit menu holds commands that deal with manipulating text and other objects. ❑ Find and Replace\Quick Find — This command displays a find dialog where you can search the project for specific text. A drop-down lets you indicate whether the search should include only the current document, all open documents, the current project, or the entire solution. Options let you determine such things as whether the text must match case or whole words.
Chapter 1 ❑ Bookmarks — The Bookmarks submenu lets you add, remove, and clear bookmarks, and move to the next or previous bookmark. ❑ Outlining — The Outlining submenu lets you expand or collapse sections of code, and turn outlining on and off. ❑ IntelliSense — The IntelliSense gives access to IntelliSense features. For example, its List Members command makes IntelliSense display the current object’s properties, methods, and events.
IDE Following is a description of commands associated with the View menu: ❑ Code — The Code command opens the selected file in a code editor window. For example, to edit a form’s code, you can click on the form in the Solution Explorer and then select View\Code. ❑ Designer — The Designer command opens the selected file in a graphical editor if one is defined for that type of file. For example, if the file is a form, Visual Studio opens it in a graphical form editor.
Chapter 1 Figure 1-8: The View menu’s Property Pages command displays an application’s property pages. Project The Project menu shown in Figure 1-9 contains commands that let you add and remove items to and from the project. Which commands are available depends on the currently selected item. Following is a description of commands associated with the Project menu: 12 ❑ New items — The first several commands let you add new items to the project. These commands are fairly self-explanatory.
IDE Figure 1-9: The Project menu lets you add files and references to the currently selected project. Figure 1-10: The Project menu’s Add New Item command lets you add a wide variety of items to the project.
Chapter 1 ❑ Show All Files — The Show All Files command makes Solution Explorer list files that are normally hidden. These include resource files corresponding to forms, hidden partial classes such as designer-generated form code, resource files, and files in the obj and bin directories that are automatically created by Visual Studio when it compiles your program. Normally, you don’t need to work with these files, so they are hidden. Select this command to show them. Select the command again to hide them.
IDE MyMathLibrary.dll defines a class named MathTools and that class defines a public function Fibonacci, a project with a reference to this DLL could use the following code. Dim math_tools As New MyMathLibrary.MathTools MsgBox(“Fib(5) = “ & math_tools.Fibonacci(5)) ❑ Add Web Reference — The Add Web Reference command displays the dialog shown in Figure 1-12. You can use this dialog to find Web Services and add references to them so your project can invoke them across the Internet.
Chapter 1 In Figure 1-13, the Toolbox, Solution Explorer, Properties window, Errors List, and other secondary windows have been hidden to make more room for the large Properties page. You can see these other windows’ icons lurking along the left, right, and bottom edges of the figure. Click the tabs on the left to view and modify different types of application settings. You can leave many of the property values alone and many are set in other ways.
IDE Figure 1-14: The Compile tab contains important properties for controlling code generation. The second compiler option is Option Strict. When this option is turned off, Visual Studio allows your code to implicitly convert from one data type to another, even if the types are not always compatible. For example, Visual Basic will allow the following code to try to copy the string s into the integer i. If the value in the string happens to be a number, as in the first case, this works.
Chapter 1 To avoid confusion and long debugging sessions, you should always set Option Explicit On and Option Strict On. You can turn them on for a project using the project page. To make them on by default for new projects, open the Tools menu and select Options. Open the Projects and Solutions folder, select the VB Defaults page, and turn the options on, as shown in Figure 1-15.
IDE Figure 1-16. The Solution Properties Pages dialog lets you set solution properties. Figure 1-17: The Build menu lets you compile projects.
Chapter 1 Following is a description of commands associated with the Build menu: ❑ Build WindowsApplication1 — This command compiles the currently selected project, in this case the project WindowsApplication1. Visual Studio examines the project’s files to see if any have changed since the last time it compiled the project. If any of the files have changed, Visual Studio recompiles those files to update the result.
IDE Figure 1-19: The Configuration Manager lets you manage project builds. Release configurations use more optimizations than Debug configurations, so they provide smaller, faster executable programs. They do not include support for debugging, however, so you cannot debug a program compiled for release. In the “Active solution configuration” drop-down, select the entry to create a new configuration.
Chapter 1 Use the drop-downs and check boxes in the grid to select features for the solution’s projects. For example, if the solution contains several projects, you could flag some to compile using the Debug configuration and others to compile using the Release configuration. If you then rebuilt the solution, you would be able to debug some of the projects but not all of them.
IDE Effectively using these debugging techniques can make finding problems in the code much easier, so you should spend some time learning how to use these tools. They can mean the difference between finding a tricky error in minute, hours, or days. The commands visible in the Debug window change, depending on several conditions such as the type of file you have open, whether the program is running, the line of code that contains the cursor, and whether that line contains a breakpoint.
Chapter 1 Figure 1-22: The Attach to Process dialog lets you attach the debugger to running processes. Figure 1-23: The Exceptions dialog lets you determine how Visual Basic handles uncaught exceptions.
IDE ❑ Step Into — This command makes the debugger execute the current line of code. If that code invokes a function, subroutine, or some other procedure, the point of execution moves into that procedure. It is not always obvious whether a line of code invokes a procedure. For example, a line of code that sets an object’s property may be simply setting a value or invoking a property procedure. ❑ Step Over — This command makes the debugger execute the current line of code.
Chapter 1 Figure 1-25: The Watch window lets you easily track expression values. ❑ Toggle Breakpoint — This command toggles whether the current code line contains a breakpoint. When execution reaches a line with an active breakpoint, execution pauses so you can examine the code and program variables. You can also toggle a line’s breakpoint by clicking on the margin to the left of the line in the code editor.
IDE Figure 1-26: The Debug menu’s Windows submenu contains commands that display debugging-related windows. Figure 1-27: The Breakpoints window helps you manage breakpoints. ❑ Output — This command displays the Output window. This window displays compilation results and output produced by Debug and Trace statements. ❑ Script Explorer — This command displays the Script Explorer, which can help you debug script code written in VBScript or JScript.
Chapter 1 Debug menu’s QuickWatch command described earlier, the new watch is placed in the Watch 1 window (shown in Figure 1-25). You can click and drag watches from one watch window to another to make a copy of the watch in the second window. You can also click on the Name column in the empty line at the bottom of a watch window and enter an expression to watch. One useful IDE trick is to drag watch windows 2, 3, and 4 onto Watch 1 so that they all become tabs on the same window.
IDE execution. In this example, the program is at the line 20 in function SearchDatabase. That function was called by function FindEmployee at line 17, and that function was called by the Form_Load event handler. Double-click on a line to jump to the corresponding code in the program’s call stack. This technique lets you move up the call stack to examine the code that called the routines that are running.
Chapter 1 of Figure 1-32, the window shows each module’s load order (lower-numbered modules are loaded first), the module’s version, timestamp, and the process using the module. Click on a column to sort the modules by that column. Figure 1-32: The Modules window displays information about the modules used by the program. ❑ Processes — This window lists processes that are attached to the Visual Studio session.
IDE stops execution only if the expression (i = j) And (i > 20) is True when the code reaches the breakpoint. Note that specifying a breakpoint condition can slow execution considerably. Figure 1-33: The Breakpoint Condition dialog lets you specify a condition that determines whether Visual Studio stops at the breakpoint. Right-click a breakpoint and select Hit Count to display the Breakpoint Hit Count dialog shown in Figure 1-34.
Chapter 1 Figure 1-35: The When Breakpoint Is Hit Condition dialog lets you determine what actions Visual Basic takes when the breakpoint is activated. The Command and Immediate Windows The Command and Immediate windows both allow you to execute commands while the program is stopped in the debugger. One of the more useful commands in each of these windows is the Debug.Print statement. For example, the command Debug.Print x displays the value of the variable x.
IDE One particularly useful command is Tools.Alias. This command lists command aliases defined by the IDE. For example, it indicates that ? is the alias for Debug.Print and that ?? is the alias for Debug .QuickWatch. The Command window includes some IntelliSense support. If you type the name of a menu, for example Debug or Tools, IntelliSense will display the commands available within that menu. While the Command window issues commands to the IDE, the Immediate window executes Visual Basic statements.
Chapter 1 Figure 1-36: The Data menu holds commands that deal with datasets. The following list describes commands shown in Figure 1-36: ❑ Show Data Sources — This command displays the Data Sources window, where you can work with the program’s data sources. For example, you can drag and drop tables and fields from this window onto a form to create controls bound to the data. ❑ Preview Data — This command displays a dialog that lets you load data into a DataSet and view it at design time.
IDE Figure 1-37: The Format menu contains commands for formatting and arranging controls on a form. ❑ Align — This submenu contains commands that align the controls you have selected in various ways. It contains the commands Lefts, Centers, Rights, Tops, Middles, Bottoms, and “to Grid.” For example, the Lefts command aligns the controls so their left edges line up nicely. The “to Grid” command snaps the controls to the nearest grid position.
Chapter 1 ❑ Center in Form — This submenu contains commands that center the selected controls on the form. It contains the commands Horizontally and Vertically. Note that the selected controls are centered as a group; they are not centered individually on top of each other. ❑ Order — This submenu contains the commands Bring to Front and Send to Back, which move the selected controls to the top or bottom of the stacking order.
IDE The following list describes the Tools menu’s most useful commands: ❑ Attach to Process — This command displays the dialog shown in Figure 1-22 to let you attach the debugger to a running process. Select the process to which you want to attach and click Attach. ❑ Connect to Device — This command lets you connect to a physical device or emulator such as Pocket PC or Smartphone devices or emulators.
Chapter 1 Figure 1-39: The Create GUID dialog generates GUIDs. ❑ Import/Export Settings — This command displays a dialog that you can use to save, restore, or reset your Visual Studio IDE settings. ❑ Customize — This command allows you to customize the Visual Studio IDE. See the “Customize” section later in this chapter for details. ❑ Options — This command allows you to specify options for the Visual Studio IDE. See the “Options” section later in this chapter for details.
IDE Figure 1-40: The Macros submenu contains commands for recording and executing macros. Select the Macro Explorer command to display the window shown in Figure 1-41. If you right-click on a macro, the resulting pop-up menu lets you run, edit, rename, or delete the macro. Notice the Macro Explorer’s predefined Samples section, which contains example macros that you can use or modify for your own use. Figure 1-41: The Macro Explorer lets you edit, run, and delete macros.
Chapter 1 Sometimes when you perform a series of programming tasks many times, there are better ways to approach the problem than writing a macro. For example, you may be able to make your program repeat the steps inside a loop. Or you may be able to extract the common code into a subroutine and then call it repeatedly rather than repeating the code many times. In these cases, your application doesn’t need to contain a long sequence of repetitive code that may be hard to debug and maintain.
IDE Click the Commands tab to see a list of categories as shown in Figure 1-43. Select a category on the left. Then click and drag a command from the list on the right. If you drop the command on a toolbar, the command is added to the toolbar. Hover over a menu to open the menu so that you can drop the command in it. Figure 1-43: The Customize dialog’s Commands tab lets you add commands to toolbars and menus. To create a new menu, select the New Menu item in the list on the left.
Chapter 1 If you right-click a command in a menu or toolbar while the Customize dialog is open, Visual Studio displays the pop-up menu shown in Figure 1-44. Click the Name text box and enter a new name to change the text displayed in the menu or toolbar. Figure 1-44: Right-click menu and toolbar commands to change their appearances. Use the Copy Button Image command to copy the button’s image to the clipboard. Use Paste Button Image to paste a copied image onto a button.
IDE Figure 1-45: You can use Visual Studio’s simple button editor to change a command’s button. The pop-up menu’s Default Style command makes the command use a style that depends on whether it is in a menu or toolbar. In a menu, the command displays a button and text. In a toolbar, the command displays only a button. Ironically, a new button’s default style is not Default Style. When you create a new toolbar or menu command, the button initially displays only text.
Chapter 1 Figure 1-46: The Options dialog’s Keyboard section lets you view and modify keyboard shortcuts. Options The Tools menu’s Options command displays the dialog shown in Figure 1-47. This dialog contains a huge number of pages of options that configure the Visual Studio IDE. The Customize dialog’s Keyboard button described in the previous section uses the same dialog with the Keyboard item selected in the list on the left. Figure 1-47: The Options dialog lets you specify IDE options.
IDE The following list describes the Options dialog’s most important categories. ❑ Environment — Contains general IDE settings such as whether the IDE uses an Multiple Document Interface (MDI) or Single Document Interface (SDI) interface, the number of items listed in the MRU lists, and how often the IDE saves AutoRecover information. The Fonts and Colors subsection lets you determine the colors used by the editors for different types of text.
Chapter 1 Figure 1-48: The Window menu displays commands that control Visual Studio’s windows. The following list briefly describes these commands. 46 ❑ New Window — Creates a new window displaying the contents of the current code window. ❑ Split — Splits a code window into two panes that can display different parts of the code at the same time. This command changes to Remove Split when you use it.
IDE ❑ New Horizontal Tab Group — Splits the main document window horizontally so that you can view two different documents at the same time. ❑ New Vertical Tab Group — Splits the main document window vertically so that you can view two different documents at the same time. ❑ Close All Documents — Closes all documents. ❑ Reset Window Layout — Resets the window layout to a default configuration. ❑ Form1.vb — The bottom part of the Window menu lists the open documents. In Figure 1-48, it lists Form1.
Chapter 1 For other Visual Basic community resources, see the “community support” topic in the MSDN help or visit the Web page http://msdn.microsoft.com/library/en-us/vsintro7/html/vxoriAdditionalResources ForVisualStudioDevelopers.asp. Also see the Visual Studio 2005 Home Page at http://msdn.microsoft .com/vs2005/default.aspx. Microsoft may move these pages but you should be able to find them if you search Microsoft’s Web site for “Additional Resources for Visual Studio Developers” and “Visual Studio 2005.
IDE Toolbars Visual Studio’s toolbars are easy to rearrange. Grab the four gray dots on a toolbar’s left or upper edge and drag the toolbar to its new position. If you drag a toolbar to one of Visual Studio’s edges, it will dock there either horizontally (on the IDE’s top or bottom edge) or vertically (on the IDE’s left or right edge). If you drop a toolbar away from the IDE’s edges, it becomes a floating window not docked to the IDE.
Chapter 1 Figure 1-51: Use the IDE’s docking icons to help you dock windows. Sometimes the IDE is so cluttered with windows that it’s hard to figure out exactly where the window will be dropped. It’s usually fairly easy to just move the mouse around a bit and watch the grayed rectangle to see what’s happening.
IDE Figure 1-52: The Toolbox window can display tools by name or icon. You can customize the Toolbox by right-clicking on it and selecting one of the commands in the context menu. The following list briefly describes these commands. ❑ List View — Toggles the current tab to display tools either as a list of names or a series of icons. ❑ Show All — Shows or hides less commonly used tool tabs such as XML Schema, Dialog Editor, DataSet, Login, WebParts, Report Items, Device Controls, and many others.
Chapter 1 ❑ Delete Tab — Deletes a tab. ❑ Rename Tab — Lets you rename a tab. ❑ Move Up, Move Down — Moves a tab up or down in the Toolbox. You can also click and drag the tabs to new positions. Figure 1-53: Use the Choose Toolbox Items dialog to select the tools in the Toolbox. The Visual Basic Code Editor Visual Studio includes editors for many different kinds of documents, including several different kinds of code.
IDE Figure 1-54: The Visual Basic code editor provides many features, including line numbers and icons that indicate breakpoints and bookmarks. Margin Icons The gray margin to the left of the line numbers contains icons giving information about the corresponding lines of code. The following table describes the icons on lines 4 through 11.
Chapter 1 These icons can combine to indicate more than one condition. For example, line 12 shows a blue and white rectangle to indicate a bookmark, a hollow red diamond to indicate a disabled breakpoint that performs an action, and a plus sign to indicate that the breakpoint has a condition or hit count test. Note that the editor marks some of these lines in other ways than just an icon. It highlights the currently executing line with a yellow background.
IDE The editor automatically creates outlining entries for namespaces, classes and their methods, and modules and their methods. You can also use the Region statement to group a section of code for outlining. For example, you can place several related subroutines in a region so you can collapse and expand the routines as a group. Figure 1-55 shows more examples of outlining. Line 37 begins a region named Randomization Functions that contains three collapsed subroutines.
Chapter 1 Also notice that comments before a subroutine are not collapsed with the subroutine. You can make reading collapsed code easier by placing a short descriptive comment before each routine. Tooltips If you hover the mouse over a variable at design time, the editor displays a tooltip describing the variable. For example, if you hover over an integer variable named num_actions, the tooltip would display “Dim num_actions As Integer.
IDE If a variable has properties that are references to other objects, you can hover over their plus signs to expand those objects. You can continue following the plus signs to drill into the variable’s object hierarchy as deeply as you like. IntelliSense If you start typing a line of code, the editor tries to anticipate what you will type. For example, if you type “Me.” then the editor knows that you are about to use one of the current object’s properties or methods.
Chapter 1 After you finish typing a method and its opening parenthesis, IntelliSense displays information about the method’s parameters. Figure 1-58 shows parameter information for a form object’s SetBounds method. This method takes four parameters: x, y, width, and height. IntelliSense shows a brief description of the current parameter x. As you enter parameter values, IntelliSense moves on to describe the other parameters. IntelliSense also indicates whether there are overloaded versions of the method.
IDE The following table describes some of the default colors that the code editor uses to highlight different code elements.
Chapter 1 Figure 1-59: If the code editor can figure out what’s wrong, it displays a suggestion indicator. Code Snippets A code snippet is a piece of code that you might find useful in many applications. It is stored in a snippet library so that you can quickly insert it into a new application. Visual Studio comes with hundreds of snippets for performing standard tasks. Before you start working on a complicated piece of code, you should take a look at the snippets that are already available to you.
IDE Figure 1-60: The error suggestion dialog proposes likely solutions to an error. The following sections explain how to use snippets in your applications and how to create new snippets. Using Snippets To insert a snippet, right-click where you want to insert the code and select Insert Snippet to make the editor display a list of snippet categories. Double-click a category to find the kinds of snippets that you want. If you select a snippet, a tooltip pops up to describe it.
Chapter 1 Figure 1-61: When you select a code snippet, a pop-up describes it. Double-click on the snippet to insert it into your code. The snippet may include values that you should replace in your code. These replacement values are highlighted with a light green background, and the first value is initially selected. If you hover the mouse over one of these values, a tooltip appears to describe the value. You can use the Tab key to jump between replacement values.
IDE Figure 1-62: Values that you should replace in a snippet are highlighted. Creating Snippets To create a new snippet, you need to build an XML file containing the property tags to define the snippet and any replacements that the user should make. The following code shows the “Create a public property” snippet used in the previous section. The outer CodeSnippets and CodeSnippet tags are standard and you should not change them. Use the Title tag in the Header section to describe the snippet.
Chapter 1 DataType The property’s data type. Integer Description The property’s description. An Integer property.
IDE Figure 1-63: The Code Snippets Manager lets you add and remove snippet directories. The Code Editor at Run Time The code editor behaves slightly differently at run time and design time. Many of its design time features still work. Breakpoints, bookmarks, IntelliSense, and snippets still work. At run time, the editor adds new tools for controlling the program’s execution. Right-click on a value and select Add Watch or QuickWatch to examine and monitor the value.
Chapter 1 Summar y The Visual Studio integrated development environment provides many tools for writing and debugging applications. It provides code snippets that make saving and reusing code easy. It lets you add, remove, and disable complex breakpoints that check conditions and hit counts, and that can perform customized actions. You can use regions and bookmarks to organize and find pieces of code, and you can step through the code line by line at execution time. The IDE is extremely flexible.