Visual Basic .NET Programming
Total Page:16
File Type:pdf, Size:1020Kb
Visual Basic .NET Programming Harold Davis Associate Publisher: Richard Mills Acquisitions and Developmental Editor: Denise Santoro Lincoln Editor: Marilyn Smith Production Editor: Mae Lum Technical Editor: Scott Swigart Graphic Illustrator: Tony Jonick Electronic Publishing Specialist: Judy Fung Proofreaders: Nancy Riddiough, Nelson Kim, David Nash, Nanette Duffy Indexer: Jerilyn Sproston Cover Designer: Caryl Gorska, Gorska Design Cover Photographer: Jon Morgan/Photo Japan Copyright © 2002 Harold Davis. World rights reserved. No part of this publication may be stored in a retrieval system, transmitted, or reproduced in any way, including but not limited to photocopy, photograph, magnetic, or other record, without the prior agreement and written permission of the publisher. Figure 1.1 in Chapter 1, "Understanding Visual Basic .NET" Copyright © 2002, Phyllis Davis. All rights reserved. Library of Congress Card Number: 2002101977 ISBN: 0-7821-4038-6 SYBEX and the SYBEX logo are either registered trademarks or trademarks of SYBEX Inc. in the United States and/or other countries. Screen reproductions produced with FullShot 99 and Collage Complete. FullShot 99 © 1991- 1999 Inbit Incorporated. All rights reserved.FullShot is a trademark of Inbit Incorporated.Collage Complete is a trademark of Inner Media Inc. Internet screen shot(s) using Microsoft Internet Explorer 6 reprinted by permission from Microsoft Corporation. TRADEMARKS: SYBEX has attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following the style used by the trademark holder wherever possible. The author and publisher have made their best efforts to prepare this book, and the content is based upon final release software whenever possible. Portions of the manuscript may be based upon pre-release versions supplied by software manufacturer(s). The author and the publisher make no representation or warranties of any kind with regard to the completeness or accuracy of the contents herein and accept no liability of any kind including but not limited to performance, merchantability, fitness for any particular purpose, or any losses or damages of any kind caused or alleged to be caused directly or indirectly from this book. Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 For my parents, Martin and Virginia Acknowledgements It takes a village, they say, to raise a child. Undoubtedly, it takes a team to create a book like this. Without the wonderful people at Sybex, this book wouldn't exist. Richard Mills and Denise Santoro Lincoln originated this project and brought me into it. Denise got the book off the ground and moved it from conception through toddlerhood. Mae Lum masterfully handled the logistics, as the book became a full-fledged project. Scott Swigart technically reviewed my manuscript with a great and rare attention to detail. His technical understanding of .NET has greatly enriched this book. Marilyn Smith copyedited this book and has substantially helped to make it something we can all be proud of. I would also like to thank the Sybex production team, including graphic illustrator Tony Jonick; electronic publishing specialist Judy Fung; proofreaders Nancy Riddiough, Nelson Kim, David Nash, and Nanette Duffy; and indexer Jerilyn Sproston. In addition to team Sybex, I would like to thank my friend and agent, Matt Wagner, and Bill Gladstone, both of Waterside Productions. Without them, this book wouldn't exist. John Kilcullen and Stephen Compagni Portis also indirectly contributed to this book's genesis. Finally, I need to thank my wife, Phyllis Davis, who is once again a major contributor to a book of mine. Phyllis and my children, Julian and Nicholas, provided the inspiration, space, and motivation that made this book possible. Introduction At first, .NET was no more than a distant echo in my consciousness. For a long time—it seems like years, and actually, it was years—I heard the distant beating of publicity drums regarding .NET, and I couldn't care less. My concern has always been to work with high- quality programming tools that are solid, rather than playing with the latest technology toy, and I am ever suspicious of publicity campaigns. Visual Basic 6 worked well enough for me, my consulting clients, and the readers of my books. Why should I bother taking the time to understand this .NET thing until it was tried-and-true technology? What's the .NET Strategy? Before we get to my "Aha!" moment—and I've had several epiphanies related to .NET and Visual Basic .NET—I would like to step back for a second and consider exactly what .NET is and what it is not. This is particularly confusing because some things that Microsoft calls .NET are a matter of "vision," some are marketing terms, and others—such as Visual Basic .NET—are grounded technologies that are here today. (I mean, what could be more grounded as a technology than a programming language?) Pushing aside the clouds of confusion that swirl around the terminology, Microsoft's .NET strategy involves three related offerings: • .NET Framework and Visual Studio .NET, a runtime platform and development environment that works with languages including Visual Basic .NET • .NET Enterprise Servers, a set of enterprise server products such as Biztalk Server, Exchange Server, Mobile Information Server, and SQL Server, which happen to have been given the .NET moniker for marketing purposes • .NET MyServices, also sometimes called Hailstorm, which is a vision for creating services, such as the Passport user authentication service, that can be drawn upon as utilities by myriad distributed applications The domain of this book is the first of these: applications written in the Visual Basic .NET language, using the Visual Studio development environment, targeting the .NET Framework as a runtime platform. You can think of the runtime platform that .NET applications are written for as being analogous to the Java runtime "sandbox" that Java applications target, except that, at this point, the .NET Framework is primarily deployed on Microsoft Windows platforms. What's Special about Visual Basic .NET? So, what is so exciting about another programming language and another development environment, targeting another runtime? In some ways, the proof of this is "in the pudding"— you really need to take it for a test drive and find out for yourself. In the meantime, I would like to share with you a few of the features that made me sit up and take notice. The first thing that I found extraordinary was that Windows applications could be run within Internet Explorer. (There are some caveats here, of course: The target system needs to have the .NET Framework and a recent version of Internet Explorer installed.) Every time you successfully run a program, such as a Windows application, within the Visual Studio .NET development environment, a compiled executable is created. If you open this executable file in Internet Explorer, the application will run, looking and behaving for all the world like a normal Windows application. This creates the possibility of easily producing distributed applications with an extraordinarily rich user interface, and the user doesn't need to do anything more than click a hyperlink (Microsoft terms this no-touch deployment). My next epiphany was related to a slightly silly programming wish. It has always bugged me that application windows had to be rectangular. No more! In Chapter 6, "Working with Form Methods and Modules," I'll show you how to easily create an ovoid window. You may never create an application that uses a curvilinear interface, but what a relief to know that windows don't have to be square! I also found it very exciting that Visual Basic .NET allows you to create web applications using an event-driven programming model (rather than a top-to-bottom linear HTML model). And rolling your own web services, complete with XML definition and discovery files, is a matter of clicking a few buttons. In Chapter 19, "Building ASP.NET Web Applications," I'll explain the mechanics of creating web applications and web services. What Kind of Book Is This? The features I've mentioned, and many more features that are explained in this book, sound glitzy. But this book has its two, solid, peasant feet firmly planted on the ground. It is not a "gee-golly-whizz, isn't this technology wonderful" book. It is a matter-of-fact, here's how you do it kind of book. As author to reader, my pledge to you is that I do not tell you to do anything I haven't tried to do (and got to work). Every example, and every line of code, in this book is the result of my own (sometimes fluid and easy, sometimes desperate) attempts to get things to work. I believe this makes me a better guide for you than if I were some kind of programming superhero. It's not my job to show you that I know more than you do. It is my job to help you find your way. I am there with you, swerving to avoid the pitfalls, one step ahead, shining a flashlight in the dark. I know what you are likely to want to know, because I have been there, done that, and have been as blind as you. I know that everything in this book works, because I have made it work myself. This book is not a book about how to transition from Visual Basic 6 to Visual Basic .NET, and it is not a book about the future. It is a book about the present. Visual Basic .NET is now, and this book is a pragmatic guide to working with it. Who Should Read This Book? This book does not assume any formal programming knowledge on the part of readers.