Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning Visual Basic 2005 Express Edition - From Novice To Professional (2006)

.pdf
Скачиваний:
386
Добавлен:
17.08.2013
Размер:
21.25 Mб
Скачать

180

C H A P T E R 7 H O W V I S U A L B A S I C 2 0 0 5 E X P R E S S H E L P S Y O U C O D E

Figure 7-11. Visual Basic 2005 Express’s View menu

If you select the Tab Order item from the View menu, the form you are working on changes to Tab Order mode, just like mine in Figure 7-12.

Figure 7-12. Selecting Tab Order from the View menu shows you the current tab order of the controls on the form.

C H A P T E R 7 H O W V I S U A L B A S I C 2 0 0 5 E X P R E S S H E L P S Y O U C O D E

181

In Tab Order mode, all you need to do to set up the tab order of the controls on the form is to simply click on them one by one. Don’t forget to click on the Label controls as well, because you can set a hot key for a label by using & in the label’s Text property (for example, &Next to display Next); when you select a label by pressing its hot key, focus moves to the next control in sequence.

When you are finished clicking away, just press the Esc key on the keyboard to leave Tab Order mode. What could be easier?

Using IntelliSense

We’ve touched on IntelliSense in some of the examples in earlier chapters. It’s the part of Visual Basic 2005 Express that pops up tip boxes in the code window to help you remember the syntax of methods to call and so on. You can even bring it up at any time in the code editor by pressing Ctrl+spacebar. It’s a lot more than just a neat pop-up window, though.

IntelliSense can help you spot bugs and problems with your code before you run the program. It can also automatically resolve some problems for you. In fact VB Express’s IntelliSense features can even be used to write code for you in some instances, thanks to a great feature called code snippets.

Automatically Fixing Namespace Problems

Methods and properties live in classes, and classes live in namespaces. The .NET Framework alone includes a huge number of namespaces, and an even larger number of classes. Remembering what lives where can quickly become a real problem. IntelliSense can help.

Fire up a new console application and I’ll demonstrate.

There’s a class in one of the framework namespaces called WebClient, a great class that lets you download stuff from the Internet (web pages, files, and so on). The only problem I ever have with it, and indeed with a great many other classes in .NET, is remembering where it lives; is it System.Web.WebClient or System.Net.WebClient?

Add some code to the Main() function to create a WebClient object:

static void Main(string[] args)

{

WebClient myClient = new WebClient();

}

182

C H A P T E R 7 H O W V I S U A L B A S I C 2 0 0 5 E X P R E S S H E L P S Y O U C O D E

If you try to run this program now, you’ll get an error because you haven’t told VB Express where to find the WebClient class (either by fully naming it or by adding an Imports clause to the top of the source file).

Take a look at the code in the editor and you’ll notice that IntelliSense has drawn a blue squiggle underneath WebClient. You should also notice a small red box at the end of the squiggle. Hover the mouse over the red box and a drop-down box will appear. Click it, and you’ll see an option to fix your code, as shown in Figure 7-13.

Figure 7-13. IntelliSense can automatically spot namespace problems and offer you solutions.

To fix the code, simply click on Change WebClient to Net.WebClient. IntelliSense will change the name to the correct fully qualified name for you.

Using Code Snippets

Just as it’s easy to forget which namespaces to use for which classes, new programmers in Visual Basic 2005 (or indeed any programming language) frequently have a hard time remembering how to structure blocks in the language. For example, when I learned C years ago, I used to have real issues remembering whether the condition or the increment came first in a for loop. (It’s the condition, right?) IntelliSense code snippets can help you out here.

If you don’t still have the console application open, create a new one just to get into the code editor so you can try this out.

Right-click on a blank line in the Main() method and choose the Insert Snippet option from the context menu. A list of code categories will appear, as shown in Figure 7-14.

C H A P T E R 7 H O W V I S U A L B A S I C 2 0 0 5 E X P R E S S H E L P S Y O U C O D E

183

Figure 7-14. Snippets are prewritten blocks of code that IntelliSense can add to your programs for you.

Each item in the list is a category of code snippet. If you wanted to drop a loop into your code, you would click the Common Code Patterns category. If you needed to do something on the Internet, Connectivity and Networking would be a great place to start looking. For now, just click the Common Code Patterns category. The list will change to show you some more categories, and also display a web-style “breadcrumb” to let you quickly jump back to the list view you were just looking at. You can see this in Figure 7-15.

Figure 7-15. Clicking a category shows you more categories and also displays a breadcrumb trail above the list that you can use to quickly jump back to a previous place in your snippet search.

Click the Conditionals and Loops category and you’ll see a list of actual code snippets, just like those in Figure 7-16.

184

C H A P T E R 7 H O W V I S U A L B A S I C 2 0 0 5 E X P R E S S H E L P S Y O U C O D E

Figure 7-16. Finally, the snippet window shows you a list of actual code snippets.

Click the For…Next entry. The snippet will be inserted into your code, and the snippet window will vanish. Notice that the resulting code has highlighted areas inside it, as in Figure 7-17.

Figure 7-17. When you click an actual snippet, it gets inserted into your code. The areas you need to customize are highlighted for you.

The highlights show you the parts of the code snippet that you need to change. For example, in our For loop here, the name of the variable in the loop is highlighted, as is its data type. Simply type in a new variable name and hit Tab and you’ll move to the next highlight. When you’ve changed the data type (if you need to), hit Tab again to move to the value that the For loop will start at.

As you can hopefully see, snippets can save you a lot of time when you are getting up to speed with the language and features of the .NET Framework. Feel free to spend some time exploring what other snippets are included before we move on.

C H A P T E R 7 H O W V I S U A L B A S I C 2 0 0 5 E X P R E S S H E L P S Y O U C O D E

185

Summary

Visual Studio has always been the IDE of choice for millions of developers around the globe simply because it makes programming easier. With Visual Studio 2005 and the Express tools, it just got even better.

In this chapter you took a look at some of the GUI aids for Windows user-interface design, as well IntelliSense and code snippets. I picked these items for a full explanation because they warrant it, especially if you’ve never used Visual Studio before. There are still a lot of other neat features to explore on the menus, so feel free to click away to your heart’s content before moving on. Don’t forget, online help can get you out of a bind if you find something particularly strange.

You’re all set now to start learning how to write Windows applications. See you in the next chapter.

C H A P T E R 8

■ ■ ■

Building Windows Applications

Now we get to the good stuff: using Visual Basic 2005 Express to create beautiful Microsoft Windows applications, complete with windows, dialog boxes, buttons, text boxes, lists—in fact, everything you see in the professionally packaged Windows applications out there. The bad news, of course, is that this isn’t something that you can just pick up (or I can write about) in one chapter. There’s a lot of ground to cover. It’s fun ground, though.

In this chapter we’ll cover the basics of building a user interface with VB Express, before diving into the details of just what a Windows application is and what it means to you. Along the way, you’ll explore some of the most common elements you’ll see in any Windows application user interface, namely buttons, text boxes, and labels.

That may not sound like a lot, but you have to bear in mind that you’ll also be looking at the fundamental concepts behind how a Windows app works, and these are concepts that you’ll need to know and be aware of no matter what elements you stick in your application’s user interface.

So, without further ado…

How Windows Programs Work

It’s handy to have the wonderful world of .NET application building put into context. Just how do Windows programs really work? If you think about it hard enough, Windows programs should be impossibly complex to create. A program consists of a number of windows, each containing a number of controls ranging from menus and toolbar items, to buttons, drop-down lists, text boxes, and much more. At any point, users could click on and interact with any control they can see. The golden rule with a graphical user interface is typically, “If they can see it, they can use it.”

Just think about that for a second. Your programs need to be able to handle absolutely any permutation of controls being used, in any order, and for pretty much any purpose (just because you want a user to enter a number for his age in a text box rarely means he will). Thankfully, Visual Basic 2005 Express simplifies this a great deal, and in the process simplifies just how Windows programs really work (in effect, it hides a lot of the underlying complexity of Windows away from you).

187

188

C H A P T E R 8 B U I L D I N G W I N D O W S A P P L I C A T I O N S

Hidden away, out of sight from you, is something called the message loop. Whenever something happens to an application (even something as simple as the user moving the mouse), the Windows operating system catches that and fires off a message, a data structure that holds lots of information about what just happened. The message loop of a program just sits there checking and rechecking to see whether there are any new messages, and if there are, it grabs them so that no one else can, and fires them off to the appropriate window. Historically, you’d have to write the code for the message loop and add code to each and every window you create to catch the messages and respond accordingly.

In Visual Basic 2005 Express, all of this complexity is taken care of for you through events. Each and every control and window in your application is capable of generating a number of different events. For example, when the user presses the Tab key to move the cursor from one text box to another, the first text box generates a LostFocus event, while the second generates a GotFocus event. When a user clicks a button, the button generates a Click event.

In VB Express, your part in all this is simple: you write code to “handle” these events. For example, if you want something to happen when the user clicks a button, you write code to handle the button’s Click event.

So, how do you do that? It’s very easy. You already know that when you drag a control from the Toolbox onto a form, you can use the Properties window to set various properties that define how the control should look and behave. Well, the Properties window is also an event browser. If you select a control and then click the lightning bolt button at the top of the Properties window, the list changes to show you all the possible events that the selected control can generate (see Figure 8-1).

All you need to do now is find the event you are interested in, and then do one of three things to start writing code:

Double-click the event in the list to generate a brand new method in your code for the event.

Type in a method name next to the name of the event to have VB Express generate a method with the name you specify.

Click the drop-down arrow in the entry area next to the event name and choose some event code that’s already been written to reuse that code for this event.

C H A P T E R 8 B U I L D I N G W I N D O W S A P P L I C A T I O N S

189

Figure 8-1. Clicking the lightning bolt at the top of the Properties window changes it to show all the events that a control can generate.

The last item in that list is quite important. Event handlers (the code you write to respond to an event) are really little more than methods with a special signature. It is totally possible and acceptable to have one event handler respond to the same event on many different controls. For example, if you have a bunch of text boxes in a window and you want them all to be validated the same way, you’d just hook up the Validating event of all the text boxes to the same event handler.

The question then arises, if you do that, how do you figure out just which control fired the event in the first place? After all, if you have 20 text boxes in a window all using the same Validating event, you’ll probably want the code in that event to be able to figure out just which one of the 20 fired the event in the first place. Well, all event handlers look pretty much the same. For example, here’s the Click event for a button:

Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

End Sub