23,99 €
Visual Basic is a favorite programming language, so if you're new to programming, it's a great place to start. Visual Basic 2008 For Dummies is the fun and easy way to begin creating applications right away while you get the hang of using the Visual Studio environment. Soon you'll be building all sorts of useful stuff with VB 2008! This step-by-step guide walks you through a logical series of tasks that build your skills as you get comfortable with .Net terminology, theory, tools, and design principles. You'll learn how to build an application in four different architectural styles, and you'll find out how to make your programs validate input and output, make decisions, and protect themselves from security threats. Discover how to: * Install the Visual Studio environment * Write a VB program * Use Web forms, Windows forms, and Web services * Establish good programming practices * Create class libraries * Write secure applications * Debug your applications * Work with strings and "if-then" statements * Iterate with counted and nested loops * Pass arguments and get return values * Access data with VB.NET * Work with the file system using VB You'll also find great tips for working with the VB user interface, using VB.NET in C# programming, troubleshooting your VB programs, taking your programming to the next level, and more! Once you get your hands on Visual Basic 2008 For Dummies, you'll be programming like a genius in no time!
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 437
by Bill Sempf
Visual Basic® 2008 For Dummies®
Published by Wiley Publishing, Inc. 111 River Street Hoboken, NJ 07030-5774
www.wiley.com
Copyright © 2008 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions.
Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries, and may not be used without written permission. Visual Basic and Visual Studio are registered trademarks of Microsoft Corporation in the United States and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Website may provide or recommendations it may make. Further, readers should be aware that Internet Websites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services, please contact our Customer Care Department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.
For technical support, please visit www.wiley.com/techsupport.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.
Library of Congress Control Number: 2008923124
ISBN: 978-0-470-18238-3
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
I am Bill Sempf, and you’ll notice that I don’t write in third person. I have spent an inordinate amount of time in the last several years writing about, thinking about, and coding in VB.NET. I am a coauthor of Professional Visual Studio.NET, Effective Visual Studio.NET, Professional ASP.NET Web Services and Professional VB.NET (among others), and a frequent contributor to the Microsoft Developer Network, Builder.com, Hardcore Web Services, Inside Web Development Journal, and Intranet Journal. I have recently been an invited speaker for DevEssentials, the International XML Web Services Expo, and the Association of Information Technology Professionals. As a graduate of Ohio State University with a Bachelor of Science in Business Administration, Microsoft Certified Professional, Certified Internet Business Strategist, and Certified Internet Webmaster, I have developed over one hundred Web applications for startups and Fortune 500 companies alike.
I began my career in 1985 by helping my father (also named Bill) manage Apple IIe systems for the local library. Since then, I have built applications for the likes of Lucent Technologies, Bank One, the State of Ohio, Nationwide Insurance, and Sears, Roebuck and Co. I specialized in data-driven Web applications of all types, both public and private. Currently, I am the Senior Technology Consultant at Products of Innovative New Technology in Grove City, Ohio, a Microsoft Certified Partner, working on a Learning Management System for OmniPath, inc. (www.omnipath.com). I can be reached at [email protected].
This edition of the book was heavily influenced by the thousands of readers all over the world who took the time to e-mail with thoughts, questions, complaints, criticism, praise and ideas. To be honest, they are why I write. The majority of my good projects have come from people who read my books and call to collaborate.
This book is dedicated to those readers. If you have taken the time to e-mail me or review the book or contact Wiley — good or bad — about this book series, thank you. Even if you are peeved because of a problem or error, you are helping make this a better product and that helps everyone.
Despite what people often think, even with the best of editing and authoring, these books aren’t perfect — they are a community effort. Without that community, the books wouldn’t be good or necessary. It’s a self-replicating cycle, and I love it.
I cannot begin to thank the amazing team at Wiley who led me ever so carefully through the process of updating this book. Katie Feltman kept on me to revise the ideas I presented, and Mark Enochs saw to it that I stuck to schedule. The entire Wiley team, in fact, is the best an author could ever hope to have. I have gotten to know them well over the last several years, and I love them all.
A project at Wendy’s International led me to ask the most detail-oriented person I have ever met — Mike Spivey — to review the technical details of this edition. Jeff Simmons reviewed the original code, and did a good job, but this book is all the better for multiple sets of eyes.
My army of peer reviewers from the original edition was fantastic: Theresa Alexander, Jim Andrews, David Deloveh, Rex Mahel, Greg McNamara, Rob Morgan, Blake Sparkes, and Gary Spencer. Here’s a special note about my father, William E. Sempf, whose education background was of inestimable help in reviewing the early concepts for the book. Then, he let me use him as a guinea pig for Part I! What a trooper!
Finally, a shout to the many Microsoft people who gave me a hand with specific questions about VB, Visual Studio, and the framework in general: Jan Shanahan and Susann Ragsdale in the Author Support Group, and Brad McCabe, Daniel Roth, Jay Roxe, and Steve Lasker, among many others, on the development teams.
We’re proud of this book; please send us your comments through our online registration form located at www.dummies.com/register/.
Some of the people who helped bring this book to market include the following:
Acquisitions, Editorial, and Media Development
Senior Project Editor: Mark Enochs
Senior Acquisitions Editor: Katie Feltman
Copy Editor: John Edwards
Technical Editor: Mike Spivey
Editorial Manager: Leah Cameron
Media Development Project Manager: Laura Atkinson
Editorial Assistant: Amanda Foxworth
Sr. Editorial Assistant: Cherie Case
Cartoons: Rich Tennant (www.the5thwave.com)
Composition Services
Project Coordinator: Kristie Rees
Layout and Graphics: Alissa D. Ellet, Shane Johnson, Christine Williams, Erin Zeltner
Proofreaders: John Greenough, Caitie Kelly
Indexer: Potomac Indexing, LLC
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary Bednarek, Executive Acquisitions Director
Mary C. Corder, Editorial Director
Publishing for Consumer Dummies
Diane Graves Steele, Vice President and Publisher
Joyce Pepple, Acquisitions Director
Composition Services
Gerry Fahey, Vice President of Production Services
Debbie Stailey, Director of Composition Services
Title
Introduction
About This Book
Conventions Used in This Book
What You Don’t Have to Read
Foolish Assumptions
How This Book Is Organized
Icons Used in This Book
Where to Go from Here
Part I : Getting to Know .NET Using Visual Basic
Chapter 1: Wading into Visual Basic
Visual Basic’s Role in the Framework
Saying Hello to VB 2008!
Finding More Power in Visual Studio
Chapter 2: Using Visual Studio 2008
Understanding Visual Studio Tools
Moving a Tool Window
Working with Code
Customizing with Options
Increasing Efficiency with Third-Party Tools
Chapter 3: Designing Applications in VB 2008
Making Software Simple Using the .NET Framework
Comparing Abstract Concepts with the Real World
Planning for a Project Using the Project Lifecycle
Designing the Date Calculator
Part II : Building Applications with VB 2008
Chapter 4: Building Windows Applications
A Quick Look Back at Visual Basic
Discovering Windows Controls
Making a Windows Application
Adding Functionality to a Windows Form
Adding Features to Windows Forms
Chapter 5: Building Web Applications
Seeing How ASP.NET Works with Your Web App
Discovering the Web Controls
Building Your First Web Application
Looking Below the Surface of Web Forms
Checking Out Some Cool Web Tricks
Chapter 6: Building Class Libraries
Past to Present: DLLs Defined
Designing a Library
Creating a Class Library
Delving Deeper into DLLs
Chapter 7: Building Web Services
Getting to Know XML Web Services
Designing for Web Services
Building a Web Service
Consuming a Web Service
Web Services in More Depth
Chapter 8: Debugging in VB 2008
Cool Visual Tools for Debugging
Using the Debugging Tools in the .NET Framework
Debugging the Projects
Part III : Making Your Programs Work
Chapter 9: Interpreting Strings and Things
Types of Information in Visual Basic
Making Words Work with the String Type
Finding Tools for Managing User Input
Things That Aren’t Strings — Numbers and Dates
Changing Types with Parse and TryParse
Chapter 10: Making Decisions in Code
Designing Business Logic
Depicting Logic with Flowchart Components
Implementing These Processes in Visual Basic
Chapter 11: Getting Loopy
Dealing with Zero
Writing Loops with For-Next
Using the For-Each Listing with Collections
Writing Indefinite Loops with Do-Loop
Checking at the Beginning with While
Chapter 12: Reusing Code
Reusing Code to Build Software
Avoiding the Code-Complexity Trap
Finding Other Ways to Reuse Code
Reusing Programs Outside of the Framework
Accessing DOS: But Only as a Last Resort
Chapter 13: Making Arguments, Earning Returns
Using Classes Effectively
Using Event Handlers
Making Sensible Procedures with Overloading
Flexible Objects with Generics
Controlling Objects with Threading
Part IV : Digging into the Framework
Chapter 14: Writing Secure Code
Designing Secure Software
Building Secure Windows Forms Applications
Building Secure Web Forms Applications
Using System.Security
Chapter 15: Accessing Data
Getting to Know System.Data
How the Data Classes Fit into the Framework
Getting to Your Data
Using the System.Data Namespace
Using LINQ
Chapter 16: Working with the File System
Getting to Know System.IO
Using the System.IO Namespace
Chapter 17: Accessing the Internet
Getting to Know System.Net
How the Net Classes Fit into the Framework
Using the System.Net Namespace
Chapter 18: Creating Images
Getting to Know System.Drawing
How the Drawing Classes Fit into the Framework
Using the System.Drawing Namespace
Part V : The Part of Tens
Chapter 19: Ten Tips for Using the VB User Interface
Generating Event Handlers from the Properties Window
Generating Event Handlers from the Code View
Pasting Text as HTML
Customizing Your Toolbars for Every File Type
Adding Extender Providers
Using Visual Components That Are Not So Visual
Recording Macros
Using the Task List
Inserting Snippets in Your Code
Chapter 20: Ten Ideas for Taking Your Next Programming Step
Get Visual Basic 2008 for Home Use
Update Code to Use New Tools
Join an Online Competition at TopCoder
Participate in an Open Source Project
Use Third-Party Tools in Your Projects
Integrate a Public Web Service
Try Out CodeRush and Other Code Generators
Write a Web Part
Use the DTE
Write an Article about What You Have Discovered
Chapter 21: Ten Resources on the Internet
MSDN Library
VBForDummies.net
Channel 9
ASP.NET Web
The Microsoft Public Newsgroups
.NET 247
Google CodeSearch
kbAlertz
CodePlex
<Microsoft> Google for Searching
: Further Reading
In this part . . .
Everyone must start somewhere, and I start at the beginning in this part. You write your first Visual Basic program, and, in doing so, you discover some of the ideas behind the .NET Framework (the backbone of this version of the language). You then get to do the only required reading in this entire book. First, you go over the use of the tool, Visual Studio. Second, you design the example application that you write in the next part.
Going over Visual Studio tools
Discovering how the code ties it all together
Customizing with options
Using third-party tools
Before you can effectively work with Visual Basic, you must know its tools inside and out. For the purpose of this chapter and this book, I will focus on just one — Visual Studio 2008. Visual Studio gives you access to the drag-and-drop controls that were introduced in earlier versions of Visual Basic.
Although I don’t cover the specifics of code in this chapter, I do cover all the code-generating tools that Visual Studio 2008 provides for Visual Basic. For example, I discuss the new, improved IntelliSense, which can help you remember the 288,000 methods, properties, and events in the .NET Framework, but I don’t cover the framework itself.
Part of the joy of programming in Visual Basic 2008 is using the tools that are provided by Visual Studio. Rapid Application Development (RAD) is a buzzword now, but when Visual Basic was first developed, it described (among other things) the ability to code faster by reusing bits of code built into the development tools.
This ability has never been more apparent than it is with Visual Basic 2008. Even though Visual Basic is a language, and it depends on Visual Studio for its environment, many tools make RAD real. In the following sections, I cover these tools. These tools are language independent, project independent, and indispensable.
Keep in mind that Visual Studio isn’t necessary to make Visual Basic programs. You can, in fact, make complete applications in the old-school style by using a command-line compiler.
Additionally, much of the documentation provided by the Microsoft Developer Network (MSDN) Library assumes an understanding of the tools. The documentation refers to the tools by name and often doesn’t clearly describe them. You must know where you are working before you can work, so the following sections take you on a tour of the Visual Studio tools.
When you installed Visual Studio, you were probably asked to install the MSDN Library. You will find it an indispensable tool (it’s what you get when you go to the Help menu, in fact). Additionally, you can find the library online at http://msdn.microsoft.com/library.
When you launch Visual Studio (usually by selecting its icon on your Start menu) and begin any visual project, you see the Design View. The Design View is where the Graphical User Interface (GUI) work takes place. Generally speaking, anytime you are working with pictures of forms, not code, you are working with the Design View. When I use the term designer window, I am referring to the actual place you do the work. The term Design View refers to the state the application is in.
In the Design View, you can accomplish the following:
Manufacture windows, Web, and smart device forms by dragging controls directly to the form in a What-You-See-Is-What-You-Get (WYSIWYG)–type environment
Work with databases and XML files visually
Create software components by visually managing the parts
In general, Design View is the core part of Visual Studio. Many of the other tools in Visual Studio depend on the Design View, in fact, and are disabled when you use another view, such as Code View, to work on your project.
The designer tabs have the word [Design] in the tab name, as shown in Figure 2-1, to indicate that you are using the Design View. Tabs are used in the Design and Code Views. The gray tab represents files that are open but not active. An asterisk (*) next to the filename means that you’ve made changes, but not yet saved the file.
The white tab is active and contains the editable form. When you have more than one document open, you can edit only the active form. You can drag the tabs to the left and right to change their order. Right-clicking a tab gives you a menu from which you can choose several screen management options, as shown in Figure 2-2.
Figure 2-1: A form in Design View.
Figure 2-2: Managing tab groups.
Tab groups make it easier to copy information out of one form and into another. For example, you can have one set of pages on the top half of the screen and another on the bottom half, and copy from one and paste into the other without changing screens. You can also save and close from this menu, or get information, such as the current path or the containing folder.
To add form components, such as buttons and text, to the form in the Designer window, you simply drag them from the Toolbox. The Toolbox, usually seen on the left side of the Visual Studio environment and to the left of the Designer window, is where the RAD components of various project types are held for use until you need them. The Toolbox is shown in Figure 2-3.
Figure 2-3: The Toolbox.
The Toolbox is project-sensitive, meaning that controls for Web pages and smart devices don’t show up when you are writing a Windows Forms project. Because those controls can’t be used in those project types, the Toolbox doesn’t even let you see them.
You can access the controls in the following ways:
Click a control and drag it to a form, dropping it exactly where you want it.
Double-click the control, and it appears in the upper-left corner of the active form.
If you lose the Toolbox, or if it isn’t showing on your screen, you can open it by choosing View⇒Toolbox or by pressing Ctrl+Alt+X. Right-clicking the Toolbox gives you layout options, including ordering and movement. One of the options is Reset Toolbox, which puts it back the way Microsoft had it — a great feature.
The gray dividers, labeled Data or Windows Forms among other things, divide the tools in the Toolbox by category. To open a closed category, click the divider.
Visual Studio is extendable because of the Toolbox. You can use third-party components with your programs. For example, a clock control that enables the user to set the time would show up in the Toolbox. You can also use the Toolbox to store often-used pieces of text, such as comment blocks.
You can actually drag a little block of code onto the Toolbox for reuse, such as a common comment block or a sample. I use this all the time when I am presenting. I just make a new section by right-clicking the Toolbox and selecting Add Tab. Then I can just select code in the Code View (which I cover in a few pages) and drag it right under the new section header.