The book of visual basic 2005 net insight for classic vb developers 2006 - phần 2 ppt

51 268 0
The book of visual basic 2005 net insight for classic vb developers 2006 - phần 2 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

34 Chapter 2 The Task List The Task List (see Figure 2-14) is another convenience that helps you manage programming tasks while working on a project. The Task List acts like a developer’s to-do list. To show the Task List, select View Other Windows Task List. To add an item to the Task List, click the clipboard icon, and type a description. You can also give each task a priority (Low, Medium, or High) by choosing an option in the ! column. When you’re finished with the task, you can mark it completed by adding a check mark, or you can right-click and choose Delete to remove it altogether. Figure 2-14: User tasks So far, you’ve seen how to add user task items to the Task List. However, the most interesting part of the Task List is the way you can link it to your code using predefined comment items. For example, any time you add a com- ment that starts with 'TODO, it will be automatically added to the list. You can then double-click the item to jump directly to the relevant place in code. This allows you to keep track of locations in code where further work or revision is required. Figure 2-15 shows how comment items work. The 'TODO comment in the upper pane (the code view) is linked to the task item in the bottom pane (the Task List). Notice that comment items appear only when you select Comment in the drop-down list at the top of the Task List, instead of User Tasks. Figure 2-15: Comment tasks bvb_02.book Page 34 Thursday, March 30, 2006 12:39 PM The Design Environment 35 You don’t need to add 'TODO in front of all the comments you want to track. Instead, you can set the predetermined comment types that will be automatically added to the Task List. To do so, select Tools Options, and then choose the Environment Task List section. You can add a new type of comment (called a “comment token”) by typing the prefix the comment must start with (leave out the apostrophe), setting the default priority, and clicking Add (Figure 2-16). Figure 2-16: Adding custom comment tokens Code Snippets Every day, developers write similar code to solve similar problems. The creators of VB wanted to take some of the pain out of hunting for code examples and to put the syntax for basic tasks right at your fingertips. Visual Basic 2005 addresses this challenge with a new feature called code snippets. The idea behind the code snippets feature is that you can quickly dig up a few elusive lines of code for a common task and then customize them to suit your exact needs. Visual Studio helps you out by organizing snippets into groups and using some innovative highlighting. Inserting a Snippet To try this out, move to the appropriate location in your code, right-click the mouse, and select Insert Snippet. A pop-up menu will appear with a list of snippet categories, such as Common Code Patterns, Data, Security, and Windows Forms Applications. Once you select a category, you’ll see a full list of all the snippets that are available. You can then select one to insert it at the current position. For example, Figure 2-17 shows the result of inserting the code snippet named Get a Random Number using the Random class from the Math category. bvb_02.book Page 35 Thursday, March 30, 2006 12:39 PM 36 Chapter 2 Figure 2-17: Inserting a snippet The best feature snippets provide is that they highlight any hard-coded values in green. For example, the snippet shown in Figure 2-17 includes two hard-coded values (the numbers 10 and 100, which represent the lowest and highest random numbers you’re willing to consider). When you hover over either of these values, a tooltip pops up with an explanation about what value you should type in to replace the hard-coded number (Figure 2-18). And for a real shortcut, you can jump from one highlighted region to the next, just by pressing TAB. Figure 2-18: Replacing the hard-coded values in a snippet Managing Snippets Snippets are only as useful as the code they contain, and Visual Basic 2005 ships with a relatively small collection of snippets that range from genuinely useful (like “Compare Two Files”) to absurdly trivial (like “Define a Function”). Many useful topics aren’t dealt with at all. However, there’s still hope, because the snippets system is extensible. That means you can hunt down .vbsnippet files on the Internet and add them to your collection. bvb_02.book Page 36 Thursday, March 30, 2006 12:39 PM The Design Environment 37 To get an overview of all the snippets that are currently on your computer or to add new ones, you need to use the Snippet Manager. Select Tools Code Snippets Manager (Figure 2-19). Select a snippet, and you’ll get a brief description that indicates who created it. Figure 2-19: The Code Snippets Manager The hidden gem in the Code Snippets Manager window is the Search Online button. Click this to launch a search that goes beyond your local computer and into the CodeZone community. (You may find that this search isn’t quite as refined as should be. If you’re digging up irrelevant links, try adding the word “snippet” to the search to home in on the code.) TIP Want to take your snippets to the next level? Microsoft offers a surprisingly powerful free tool for managing and customizing code snippets—you can download it at http:// msdn.microsoft.com/vbasic/downloads/tools/snippeteditor. Using this tool you can edit and test existing snippets, set author, title, and keyword information for your own snippets, and even convert your snippet into a .vsi (Visual Studio Content Installer) file for easy sharing with other programmers. Macros Macros are a new and welcome feature for Visual Basic 2005 users. At their simplest, macros are little pieces of functionality that help you automate repetitive coding tasks. For example, consider the following code example. (I’ve abbreviated it considerably to save space, but you get the idea.) ' Assigning to oddly named controls. FirstNamelbl.Text = FirstName LastNamelbl.Text = LastName Streettxt.Text = Street Countrycbo.Text = Country bvb_02.book Page 37 Thursday, March 30, 2006 12:39 PM 38 Chapter 2 The programmer who wrote these lines made a common naming mistake and put the control identifier (for example, txt for text box) at the end of the name instead of the beginning. In this case, using Visual Studio’s Find and Replace feature isn’t much help, because even though the mistake is repeated, many different variables are incorrectly named. If you’re a seasoned problem-solver, you may already realize that this mistake can be fixed by repeating a set of steps like this: 1. Start at the beginning of the line. 2. Press CTRL and the right arrow to jump to the position right before the period. 3. Highlight the last three letters (hold down SHIFT and press the left arrow three times). 4. Use CTRL+X to cut the text. 5. Press HOME to return to the front of the line. 6. Press CTRL+V to paste the variable prefix in the right position. 7. Move one line down by pressing the down arrow. Easy, right? Just repeat these steps for each of the next dozen lines, and the problem is solved. Of course, now that we’ve realized that the process of editing a line is just a sequence of clearly defined steps, we can automate the whole process with a macro. To do this, select Tools Macros Record TemporaryMacro (or press CTRL+SHIFT+R). Follow the steps, enter the appropriate keystrokes, and then click the Stop button on the macro toolbar. Now you can play the temporary macro ( CTRL+SHIFT+P) to fix up the remaining lines. The Macro IDE When you record a macro, Visual Studio stores a series of instructions that correspond to your actions. If you’ve created macros in other Microsoft applications such as Microsoft Word or Microsoft Access, you’ll already be familiar with this system. The interesting thing in Visual Studio is that the macro language used to record your actions is exactly the same as ordinary Visual Basic 2005 code. The only difference is that it has built-in objects that allow you to interact with the IDE to do things like insert text, open windows, and manage projects. In fact, an entire book could be written about the object model used in Visual Studio’s macro facility. To view the code you created with your temporary macro, select Tools Macros Macro Explorer. In the Macro Explorer window (which is paired with the Solution Explorer by default), find the TemporaryMacro routine in the RecordingModule, as shown in Figure 2-20. Right-click the TemporaryMacro routine, and select Edit to view the code, which looks like this: Sub TemporaryMacro() DTE.ActiveDocument.Selection.StartOfLine(VsStartOfLineOptionsFirstText) bvb_02.book Page 38 Thursday, March 30, 2006 12:39 PM The Design Environment 39 DTE.ActiveDocument.Selection.WordRight() DTE.ActiveDocument.Selection.CharLeft(True, 3) DTE.ActiveDocument.Selection.Cut() DTE.ActiveDocument.Selection.StartOfLine(VsStartOfLineOptionsFirstText) DTE.ActiveDocument.Selection.Paste() DTE.ActiveDocument.Selection.LineDown() End Sub Figure 2-20: The Macro Explorer window This code is bound to look a little unfamiliar, as it uses the DTE object model, which allows you to interact with the IDE. The important thing to understand is that every macro corresponds to a subroutine, and all recorded actions are defined in code. To interact with the IDE, you use DTE commands. TIP When you open a macro project, you’ll end up with what looks like two design environ- ments. The easiest way to switch from your regular project to your macro project is using the taskbar, which shows both. The Temporary Macro Macros help you while you are writing a program. In fact, most macros have a limited usefulness: They are created to solve a specific problem and are not used once that problem is solved. For that reason, a Visual Studio macro is recorded as a “temporary” macro. There can only be one temporary macro at a time, and when you create a new temporary macro, the old one is replaced. If you want to create a permanent macro, you’ll have to open the macro editor and move the code in the TemporaryMacro subroutine into a new sub- routine that you’ve just created for that purpose. To run this new macro, double-click its name in the Macro Explorer window. Macros with Intelligence In practice, macros often take over where more mundane find-and-replace or cut-and-paste operations leave off. For example, you might want to make a macro that could intelligently examine the currently selected text and decide what correction or insertion to make based on some test. You could even build an entire wizard, complete with Windows forms and file access. Some examples of advanced macros are included in the sample code for this chapter. bvb_02.book Page 39 Thursday, March 30, 2006 12:39 PM 40 Chapter 2 Following is a straightforward example that swaps the code on either side of an equal (=) sign in the selected range. For example, it could convert the line of code StringA = StringB to StringB = StringA. Public Sub InvertAssignmentLine() ' Retrieve the text. Dim str As String Dim i As Integer DTE.ActiveDocument.Selection.SelectLine() str = DTE.ActiveDocument.Selection.Text ' Trim the final hard return. str = Left(str, Len(str) - 2) ' Find the equal sign. i = InStr(str, "=") ' Reverse the text if it had an equal sign. If i > 0 Then str = Mid(str, i + 1) & "=" & Left(str, i - 1) DTE.ActiveDocument.Selection.Text = str & vbNewLine End If ' "De-select" the current line. DTE.ActiveDocument.Selection.Collapse() End Sub The structure of this code should be clear, but the DTE commands will be new. A good way to start learning about DTE commands is to record a task in the IDE, and then look at the automatically generated code. For compre- hensive information about the DTE, check out the Visual Studio Help. Incidentally, this code also uses traditional Visual Basic 6 string manipu- lation functions such as Len() and Left(), which are still supported but whose use is discouraged in favor of VB 2005’s new object-oriented equivalents. The online samples for this chapter include a rewritten version of this macro that uses .NET-style string manipulation. After you’ve read the next chapter and learned the basics of .NET, you might want to take a look at that sample. Macros and Events Visual Studio also provides a special EnvironmentEvents macro module, which contains macros that react to IDE events for windows, documents, and build and debugging operations. Once again, you need to know some non-VB features to perfect this type of macro—namely, the object model for the IDE. The next macro example uses the WindowActivated event. Whenever you change focus to a new window, this macro closes all the other windows that are part of your project (the dockable Visual Studio windows and the Start Page won’t be closed) in an attempt to reduce screen clutter. It may seem a bvb_02.book Page 40 Thursday, March 30, 2006 12:39 PM The Design Environment 41 little foreign because we haven’t yet explained how .NET handles events, but it gives you an interesting idea of what is possible with the IDE. For example, you could create a macro that executes every time a user starts a new project and preconfigures the toolbox or initial code files. Public Sub WindowEvents_WindowActivated(GotFocus As EnvDTE.Window, _ LostFocus As EnvDTE.Window) Handles WindowEvents.WindowActivated ' Exit if the current window doesn't correspond to a document. If GotFocus.Document Is Nothing Then Exit Sub Dim Doc As Document Dim Win As EnvDTE.Window ' Scan through all the windows. For Each Win In DTE.Windows ' Ignore the window if it doesn't correspond to a document ' or is the currently active window. If Not Win.Document Is Nothing And Not Win Is GotFocus Then Win.Close() End If Next End Sub Remember, this macro will work only if it’s placed in the EnvironmentEvents module. Only that module has the automatically generated code that makes all the Visual Studio events available. NOTE These macro examples are by no means comprehensive. Visual Studio allows you to write and integrate all sorts of advanced add-ins, control designers, and macros. Macros can even use the .NET class library, display a Windows interface, and examine your code. For a rich set of macro samples, check out the macro project named Samples, which is installed automatically with Visual Studio and will appear in the Macro Explorer. The Simplest Possible .NET Program Now that you’ve made your way around the Visual Studio environment, it’s time to take a closer look at the project and solution system by generating your first real program. The simplest possible .NET program doesn’t use any Windows forms. Instead, it is a Console or command-line application. This type of application takes place in something that looks like an old-fashioned DOS window, but it is really just a text-based Windows program. Console applications are some- times used for batch scripts and other extremely simple utilities. Mainly, though, Console applications are used to create every computer writer’s traditional favorite: the “Hello, World!” program. To create a Console application, just start a new project and select Visual Basic Projects Console Application (Figure 2-21). bvb_02.book Page 41 Thursday, March 30, 2006 12:39 PM 42 Chapter 2 Figure 2-21: Creating a Console application The following sample program, called MyFirstConsoleApplication, uses the Console object (found in the System namespace) to display some basic information on the screen. The project is configured to run the Main sub- routine at startup. Module Module1 Sub Main() Console.WriteLine("What is your name?") Dim Name As String = Console.ReadLine() Console.WriteLine() Console.WriteLine("Hi " & Name & ". I feel like I know you already.") ' This stops the application from ending (and the debug window ' from closing) until the user presses Enter. ' If you were interested, you could examine the return value ' of ReadLine() to find out what the user typed in. Console.ReadLine() End Sub End Module To launch this program, choose Debug Start Debugging (or just press the F5 key). The result is shown in Figure 2-22. This book won’t feature anything more on Console applications, because Windows and web applications provide much richer interface options. The only significance of MyFirstConsoleApplication (and the only reason it’s included in this book) is the fact that it shows you a complete Visual Basic 2005 application in its simplest form. That application is a module (although a class could be substituted) and a single subroutine that contains all of the bvb_02.book Page 42 Thursday, March 30, 2006 12:39 PM The Design Environment 43 program’s operations. Nothing is hidden in this example. You could write this program in a text file, give it the .vb extension, and compile it the old- fashioned way using the vbc.exe command-line compiler. Figure 2-22: An unimpressive .NET application MyFirstConsoleApplication Files The essential logic for this application is contained in a single .vb file, named Module1.vb. However, Visual Studio uses a few extra files to keep track of additional information about your solution. MyFirstConsoleApplication.sln contains information about the projects in the solution and their build settings (which determine whether the project is compiled or ignored when you click the Start button). The MyFirstConsoleApplication.suo file contains binary data that pre- serves your view settings for the solution, making sure that Task List items, breakpoints, and window settings are retained between sessions. This is a major improvement over Visual Basic 6. The MyFirstConsoleApplication.vbproj file is the most interesting. It uses an XML format to store information about your application, including the assemblies it needs, the configuration settings it uses, and the files it contains. This information is required only when programming and test- ing your application—as you’ll see in Chapter 7, all the necessary details are embedded into the final executable when you compile it. MyFirstConsoleApplication Directories There are also three extra subdirectories created inside every VB project: The My Project directory includes some resource files that support various Visual Basic features that you may or may not use, such as the My object, application settings, and embedded resources. You’ll learn about all of these details later in this book. But for now, don’t worry about this directory. It doesn’t include any nondefault content for the MyFirstConsoleApplication project. bvb_02.book Page 43 Thursday, March 30, 2006 12:39 PM [...]... with the Visual Basic 20 05 language NOTE Although a handful of language refinements are new in Visual Basic 20 05, almost all of the changes you’ll learn about in this chapter apply to all NET versions of Visual Basic The most notable exception is the My object, which makes its debut in Visual Basic 20 05 New in NET You might wonder what a chapter on basics is doing in a book designed for developers who... combination of dozens of separately developed components and the Windows API Throughout this book, I’ll introduce you to the most exciting parts of the class library, the Visual Basic 20 05 programming language, and the Visual Studio interface where you’ll do your work, all at once V B 2 0 0 5 Ba s ic s 59 bvb_ 02 .book Page 60 Thursday, March 30, 20 06 12: 39 PM The My Object After releasing VB NET 1.0, Microsoft... project files (.vbproj) Code Form, Class, or Module ( .vb) Code Form, Class, or Module ( .vb) Code Form, Class, or Module ( .vb) Code Form, Class, or Module ( .vb) Project (.vbproj) Solution (.sln) Figure 3-8 : A solution file 62 C ha pt er 3 Project (.vbproj) Project (.vbproj) Project (.vbproj) bvb_ 02 .book Page 63 Thursday, March 30, 20 06 12: 39 PM All the other files are code files ( .vb) These code files... the remodeled Visual Studio programming environment But before you can start creating real VB 20 05 programs, you need a basic understanding of a few NET concepts These concepts the basics of Visual Basic 20 05 range over every aspect of the language They include everything from small details, such as changes to assignment syntax and variable scoping, to the namespace feature, which is the basis of NET s... this integration to work, the built-in variable types in Visual Basic 6 had to be harmonized with those from other programs to create the best possible common type system This is the reason for most of the data type changes that you’ll find in VB 20 05 For example, Integer now maps to V B 2 0 0 5 Ba s ic s 65 bvb_ 02 .book Page 66 Thursday, March 30, 20 06 12: 39 PM System.Int 32 a 3 2- bit integer that can store... accessing the common class library the all-in-one repository of functionality used for everything from downloading a file from the Internet to printing a document We’ll begin this chapter with an introduction to namespaces, the common class library, and Visual Basic 20 05 s file format These are the aspects of Visual Basic programming that have changed the most in the NET world Next, you’ll learn about how the. .. they can provide If these features have captured your interest, go ahead and start experimenting! One great starting point is the Visual Studio Extensibility website provided by Microsoft at http://msdn.microsoft.com/ vstudio/extend 46 C ha pt er 2 bvb_ 02 .book Page 47 Thursday, March 30, 20 06 12: 39 PM 3 VB 20 05 BASICS So far you’ve read about the fundamental goals of the NET platform, and you’ve learned... that you can keep track of important resources Data Types Most Visual Basic 6 data types remain the same in VB 20 05, at least on the surface For example, all of the following statements are still valid: Dim MyInteger As Integer Dim MyString As String Dim MyDate As Date These may look the same as the built-in data types in Visual Basic 6, but in fact they are not Each one of these variable types is... TIP If you want a detailed look at the entire VB 20 05 language, from the ground up, why not take a look at the Visual Basic Language Specification 8.0 (VB 20 05 is also known by the version number 8.) You can search for this document at www.microsoft.com/ downloads, or just use http://tinyurl.com/8rne5 to get there Introducing the Class Library The cornerstone of NET is the common class library, which... details such as functions, variables, and events The answer? VB 20 05 and its predecessor, VB .NET, represent a complete overhaul of the Visual Basic language The changes from classic Visual Basic range from minor tweaks all the way to a radical new programming model based on the class library Some of the features you’ll read about in this chapter include: The common class library Java has one Windows programmers . and select Visual Basic Projects Console Application (Figure 2- 2 1). bvb_ 02 .book Page 41 Thursday, March 30, 20 06 12: 39 PM 42 Chapter 2 Figure 2- 2 1: Creating a Console application The following. select Comment in the drop-down list at the top of the Task List, instead of User Tasks. Figure 2- 1 5: Comment tasks bvb_ 02 .book Page 34 Thursday, March 30, 20 06 12: 39 PM The Design Environment. events. The answer? VB 20 05 and its predecessor, VB .NET, represent a complete overhaul of the Visual Basic language. The changes from classic Visual Basic range from minor tweaks all the way

Ngày đăng: 13/08/2014, 08:21

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan