Sams Teach Yourself Visual Basic.NET in 24 Hoursby James Foxall, James Foxall
Teach Yourself Visual Basic.NET in 24 Hours provides readers with 24 structured lessons with step-by-step guidance to real-world tasks. Each chapter also contains exercises that reinforce the lessons learned in each chapter. Tips, Notes, and Cautions provide additional advice from the authors on how to get up-to-speed and programming quickly with/i>
Teach Yourself Visual Basic.NET in 24 Hours provides readers with 24 structured lessons with step-by-step guidance to real-world tasks. Each chapter also contains exercises that reinforce the lessons learned in each chapter. Tips, Notes, and Cautions provide additional advice from the authors on how to get up-to-speed and programming quickly with Visual Basic.NET.
Read an Excerpt
Chapter 1: A Visual Basic.NET Programming TourLearning a new programming language can be intimidating. If you've never programmed before, the act of typing seemingly cryptic text to produce sleek and powerful applications probably seems like a black art, and you may wonder how you'll ever learn everything you need to know. The answer is, of course, one step at a time. The first step to learning a language is the same as that of any other activitybuilding confidence. Programming is part art and part science. Although it may seem like magic, it's more akin to illusion; after you know how things work, a lot of the mysticism goes away, freeing you to focus on the mechanics necessary to produce the desired result.
In this hour, you'll complete a quick tour that takes you step-by-step through creating a complete, albeit small, Visual Basic program. I've yet to see a "Hello World" program that's the least bit helpful (they usually do nothing more than print "hello world" to the screenoh, fun). So instead, you'll create a picture-viewer application that lets you view Windows bitmaps and icons on your computer. You'll learn how to let a user browse for a file and how to display a selected picture file on the screen, both of which are skills that will come in handy in later applications that you create. Creating large, commercial solutions is accomplished by way of a series of small steps. After you've finished creating this small project, you'll have an overall feel for the development process.
The highlights of this hour include the following:
Building a simple, yet functional, Visual Basic application
Letting a user browse a hard drive
Displaying a picture from a file on disk
Getting familiar with some programming lingo
Learning about the Visual Basic IDE
I hope that by the end of this hour, you'll realize just how much fun it is to program using Visual Basic.
Starting Visual Basic.NET
You must be familiar with a few terms before you begin to create programs in Visual Basic:
Distributable Component The final, compiled version of a project. Components can be distributed to other people and other computers and do not require Visual Basic to run. Distributable components are also called programs.
Project A collection of files that can be compiled to create a distributable component (program). There are many types of projects, and complex applications may consist of many projects, such as a Windows Application project and support DLL projects.
Solution A collection of projects and files that compose an application or component.
Visual Basic.NET is a complete development environment; every tool you'll need to create your Visual Basic projects is accessed from within Visual Basic. The Visual Basic environment is called the IDE, short for Integrated Development Environment, and it's the design framework in which you build applications. To work with Visual Basic projects, you must first start the Visual Basic IDE.
Start Visual Basic.NET now by choosing Microsoft Visual Studio.NET 7.0 from within the Microsoft Visual Studio.NET 7.0 folder on your Start menu.
Creating a New Project
When you first start Visual Basic, you're shown the Visual Studio Home Page tab within the IDE. Using this home page, you can open projects created previously or create new ones (see Figure 1.1). For this quick tour, you're going to create a new Windows application, so click New Project to display the New Project dialog box shown in Figure 1.2.
You can open existing projects or create new projects from the Visual Studio home page.
If you don't see the Visual Studio home page, chances are that you've changed the default settings. Hour 2, "Navigating Visual Basic," shows you how to change them back. For now, be aware that you can create a new project from the File menu in addition to using the techniques described in this hour.
You can create many types of projects with Visual Basic, as well as with the other supported languages of the .NET platform. The New Project dialog box is used to specify the type of Visual Basic project you want to create. If the Visual Basic Projects folder isn't selected, click it to display the Visual Basic project types, and then make sure the Windows Application icon is selected (if it's not, click it once to select it). At the bottom of the New Project dialog box is a Name text box, in which you specify the name of the project you're creating; in the Location text box, you can enter the location in which to save the project files.You should always set these values to something meaningful before creating a project, or you'll have more work to do later if you want to move or rename the project. The New Project dialog box allows you to create many types of .NET projects.
Type Picture Viewer into the Name text box to name your project. There's no need to change the location where the project files are to be saved at this time, so go ahead and create the new Windows Application project by clicking OK. Visual Basic creates the new project, complete with one form (design window) for you to begin building the interface for your application (see Figure 1.3).New Windows applications start with a blank form; the fun is just beginning!
Your Visual Basic environment may look different from that shown in the figures of this hour, depending on the edition of Visual Basic you're using, whether you've already played with Visual Basic, and other factors such as the resolution of your monitor. All the elements discussed in this hour, however, exist in all editions of Visual Basic.NET. (If your IDE doesn't have a window displayed that is shown in a figure, use the View menu to display the window.)
To create a program that can be run on another computer, you start by creating a project, and then you compile the project into a component, such as an executable (a program a user can run) or a DLL (a component that can be used by other programs and components). The compilation process is discussed in detail in Hour 23, "Packaging and Deploying a Solution." The important thing to note at this time is that when you hear someone refer to creating or writing a program, just as you are creating the Picture Viewer program now, they're referring to the completion of all steps up to and including compiling the project to a distributable file.
Understanding the Visual Basic Environment
The first time you run Visual Basic, you'll notice that the IDE contains a lot of windows, such as the Properties window on the right, which is used to view and set properties of objects. In addition to these windows, the IDE contains a lot of tabs, such as the Toolbox tab on the left edge of the IDE (refer to Figure 1.3). Clicking a tab displays an associated window. Try this now: click the Toolbox tab to display the Toolbox window. You can also hover the mouse over a tab for a few seconds to display the window. To hide the window, simply move the mouse off the window. To close the window completely, click the Close (X) button in the window's title bar.
You can adjust the size and position of any of these windows, and you can even hide and show them at will. You'll learn how to customize your design environment in Hour 2.
Unless specifically instructed to do so, do not double-click anything in the Visual Basic design environment. Double-clicking most objects produces an entirely different outcome than single-clicking does. If you mistakenly double-click an object on a form, a code window is displayed. At the top of the code window is a set of tabs: one for the form design and one for the code. Click the tab for the form design to hide the code window and return to the form.
The Properties window at the right side of the design environment is perhaps the most important window, and it's the one you'll use most often. If your computer's display is set for 640[ts]480, you can probably see only a few properties at this time. This makes it difficult to view and set properties as you create projects. I highly recommend that you don't attempt development with Visual Basic at a resolution below 800[ts]600. Personally, I prefer 1024[ts]768 because it offers plenty of work space. To change your display settings, right-click your desktop and select Properties.
Changing the Characteristics of Objects
Almost everything you work with in Visual Basic is an object. Forms, for instance, are objects, as are all the items you can put on a form to build an interface, such as list boxes and buttons. There are many types of objects (Hour 3, "Understanding Objects and Collections," discusses objects in detail). Objects, in turn, are classified by type. For instance, a form is a Form object, whereas items you can place on a form are called Control objects, or controls. Some objects don't have a physical appearance, but exist only in code. You'll learn about these kinds of objects in later hours.
Every object, regardless of whether it has a physical appearance, has a distinct set of attributes known as properties. You have certain properties about you, such as your height and hair color, and Visual Basic objects have properties as well, such as Height and BackColor. Properties define the characteristics of an object. When you create a new object, the first thing you need to do is set its properties so that the object appears and behaves in the way you desire. To display the properties of an object, click the object in its designer. Because the only object that currently exists in your new project is the default form, the form is already selected and its properties are displayed in the Properties window.
The property you should set first for any new object is the Name property. Press F4 to display the Properties window (if it's not already visible), and notice the Name given to your default form (the first property listed in the Properties window)Form1. When you first create an object, Visual Basic gives the object a unique, generic name based on the object's type. Although these names are functional, they aren't very descriptive. For instance, Visual Basic named your form Form1, but it's common to have dozens of forms in a project, and it would be extremely difficult to manage a complicated project if all forms were distinguishable only by a number (Form2, Form3, and so forth).
In actuality, what you're creating is a form class, or template, that will be used to create and show forms at runtime. For the purpose of this quick tour, I simply refer to it as a form. See Hour 5, "Managing Projects," for more information.
To better manage your forms, you should give each one a descriptive name. Visual Basic gives you the chance to name new forms as they're created. Because Visual Basic created this default form for you, you didn't get a chance to name it, so you must change the filename of the form. Change the name of the form now by clicking the Name property and changing the text from Form1 to fclsViewer.
I use the fcls prefix here to denote that the file is a form class. There are different types of classes, so using a prefix helps differentiate the classes in code. You're not required by Visual Basic to use object prefixes, but I highly recommended that you do so. In Hour 12, "Using Constants, Data Types, Variables, and Arrays," you'll learn the benefits of using a naming convention as well as the standard prefixes for many .NET objects.
Setting the Text Property of the Form
Notice that the text that appears in the form's title bar says Form1. This is because Visual Basic sets the form's title bar text to the name of the form when it is first created, but doesn't change it when you change the name of the form. The text in the title bar is determined by the value of the Text property of the form. Use the scrollbar to locate the Text property in the Properties window and then change the text to Picture Viewer.
The Text property was titled Caption in earlier versions of Visual Basic.
Giving the Form an Icon
Everyone who has used Windows is familiar with icons, which are the little pictures used to represent programs. Icons most commonly appear in the Start menu next to the name of their respective programs. In Visual Basic, you not only have control over the icon of your program file, you can also give every form in your program a unique icon if you want to.
The instructions that follow assume you have access to the source files for the examples in this book. They are available at http://www.samspublishing.com/detail_sams.cfm?item=0672320800. You don't have to use the icon I've provided for this example; you can use any icon of your choice. If you don't have an icon available, you can skip this section without affecting the outcome of the example.
To give the form an icon, follow these steps:
In the Properties window, click the Icon property to select it.
When you click the Icon property, a small button with three dots appears to the right of the property. Click this button.
To locate the HourOne.ico file or another icon file of your choice, use the Open dialog box that appears. When you've found the icon, double-click it, or click it once to select it and then click Open.
After you've selected the icon, it appears in the Icon property along with the word (Icon). A small version of the icon appears in the upper-left corner of the form, as well. Whenever this form is minimized, this is the icon that's displayed on the Windows taskbar.
Changing the Size of the Form
The Properties window currently shows all properties for the form in alphabetical order. Although this is useful, you may prefer to view the properties by category, particularly when you're first learning Visual Basic. Right above the list of properties in the Properties window is a group of tool buttons. Clicking the first button on the left changes the property display to categorical. The button next to this changes the display back to alphabetical....
Meet the Author
James Foxall is vice president of Tigerpaw Software, Inc. and is a Microsoft Certified Solution Provider (MCSP) specializing in commercial database applications. James is responsible for management of all Windows application development at Tigerpaw, and he is an authority on application interface and behavior standards of applications for the Microsoft Windows and Office environments. James, a MCSD, has written more than 100,000 lines of commercial production Visual Basic code in both single-programmer and multiple-programmer environments.
James is the author of several books on Visual Basic, including the a definitive work on standards, entitled Practical Standards for Microsoft Visual Basic (Microsoft Press), MCSD in a Nutshell: The Visual Basic Exams (O'Reilly), Discover Visual Basic, Access 97 Secrets (Hungry Minds), Access for Windows 95 Secrets (Hungry Minds), and the Access 97 Bible Gold Edition (Hungry Minds). He also contributes to Visual Basic Programmer's Journal and Access/Office/VBA Advisor magazine, and he is an international speaker on Visual Basic and Access. In addition to speaking and writing, James has taught Visual Basic at the college level and has been featured on numerous television news shows, in trade publications, and in newspaper articles for his technology expertise.
Most Helpful Customer Reviews
See all customer reviews