28,99 €
Visual Basic 2010 offers a great deal of functionality in both tools and language. No one book could ever cover Visual Basic 2010 in its entirety—you would need a library of books. What this book aims to do is to get you started as quickly and easily as possible. It shows you the roadmap, so to speak, of what there is and where to go. Once we've taught you the basics of creating working applications (creating the windows and controls, how your code should handle unexpected events, what object-oriented programming is, how to use it in your applications, and so on) we’ll show you some of the areas you might want to try your hand at next.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Veröffentlichungsjahr: 2011
Table of Contents
Title Page
Copyright
Dedication
About the Authors
About the Technical Editor
Credits
Acknowledgments
Introduction
Whom This Book Is For
What This Book Covers
What You Need to Use This Book
Conventions
Source Code
Errata
p2p.wrox.com
Chapter 1: Welcome to Visual Basic 2010
Event-Driven Programming
Installing Visual Basic 2010
The Visual Studio 2010 IDE
Creating a Simple Application
Using the Help System
Summary
Chapter 2: The Microsoft .NET Framework
Microsoft's Reliance on Windows
Writing Software for Windows
Common Language Runtime
The Common Type System and Common Language Specification
Summary
Chapter 3: Writing Software
Information and Data
Working with Variables
Comments and Whitespace
Data Types
Storing Variables
Methods
Summary
Chapter 4: Controlling the Flow
Making Decisions
The If Statement
Select Case
Loops
Summary
Chapter 5: Working with Data Structures
Understanding Arrays
Understanding Enumerations
Understanding Constants
Structures
Working with ArrayLists
Working with Collections
Building Lookup Tables with Hashtable
Advanced Array Manipulation
Summary
Chapter 6: Extensible Application Markup Language (XAML)
What Is XAML?
XAML Syntax
Windows Presentation Foundation
Summary
Chapter 7: Building Windows Applications
Responding to Events
Building a Simple Application
Counting Characters
Counting Words
Creating More Complex Applications
Creating the Toolbar
Creating the Status Bar
Creating an Edit Box
Clearing the Edit Box
Responding to Toolbar Buttons
Using Multiple Forms
Summary
Chapter 8: Displaying Dialog Boxes
The MessageBox
The OpenFileDialog Control
The SaveDialog Control
The FontDialog Control
The ColorDialog Control
The PrintDialog Control
The FolderBrowserDialog Control
Summary
Chapter 9: Creating Menus
Understanding Menu Features
Creating Menus
Context Menus
Summary
Chapter 10: Debugging and Error Handling
Major Error Types
Debugging
Error Handling
Summary
Chapter 11: Building Objects
Understanding Objects
Building Classes
Reusability
Designing an Object
Constructors
Inheritance
Objects and Structures
The Framework Classes
Summary
Chapter 12: Advanced Object-Oriented Techniques
Building a Favorites Viewer
An Alternative Favorite Viewer
Using Shared Properties and Methods
Understanding Object-Oriented Programming and Memory Management
Summary
Chapter 13: Building Class Libraries
Understanding Class Libraries
Using Strong Names
Registering Assemblies
Designing Class Libraries
Using Third-Party Class Libraries
Viewing Classes with the Object Browser
Summary
Chapter 14: Creating Windows Forms User Controls
Windows Forms Controls
Creating and Testing a User Control
Exposing Properties from User Controls
Design Time or Runtime
Creating a Command Link Control
Summary
Chapter 15: Accessing Databases
What Is a Database?
The SQL SELECT Statement
Queries in Access
Data Access Components and Controls
Data Binding
Summary
Chapter 16: Database Programming with SQL Server and ADO.NET
ADO.NET
The ADO.NET Classes in Action
Data Binding
Summary
Chapter 17: Dynamic Data Web Site
Creating a Dynamic Data Linq to SQL Web Site
Summary
Chapter 18: ASP.NET
Thin-Client Architecture
Web Forms versus Windows Forms
Web Applications: The Basic Pieces
Active Server Pages
Building Web Sites
Summary
Chapter 19: Visual Basic 2010 and XML
Understanding XML
The Address Book Project
Integrating with the Address Book Application
Summary
Chapter 20: Deploying Your Application
What Is Deployment?
Creating a Visual Studio 2010 Setup Application
User Interface Editor
Deploying Different Solutions
Summary
Appendix A: Exercise Solutions
Chapter 1
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Chapter 18
Chapter 19
Chapter 20
Appendix B: Where to Now?
Online Resources
Offline Resources (Books)
Index
Advertisement
Beginning Microsoft® Visual Basic 2010
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-50222-8
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 Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
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 Web site 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 Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites 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 United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
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: 2009943647
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, 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. Microsoft and Visual Basic 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.
For my daughter, Stephanie, my most precious gift from God.
For Wendy, my wife and friend in Christ
—Thearon
For my wife Jennifer and daughter Katelyn.
—Bryan
About the Authors
THEARON WILLIS currently works as a senior developer and develops Windows applications and add-ins for Microsoft Office products using Microsoft Visual Basic .NET. Over the years, Thearon has worked on a variety of systems from mainframe to client-server development.
BRYAN NEWSOME leads a team of lead developers specializing in Microsoft solutions. Since starting his career building Visual Basic 5 solutions, he has embraced each new version Visual Basic and now creates all new solutions leveraging the .NET platform and VB.NET. He provides clients with solutions and mentoring on leading edge Microsoft technologies. For VB.NET, Bryan is a Microsoft Certified Application Developer.
About the Technical Editor
DAMIEN FOGGON is a developer, writer, and technical reviewer in cutting-edge technologies and has contributed to more than 50 books on .NET, C#, Visual Basic, and ASP.NET. He is a multiple MCPD in .NET 2.0 and .NET 3.5 and can be found online at http://blog.littlepond.co.uk.
Credits
Acquisitions Editor
Paul Reese
Project Editor
Maureen Spears
Technical Editor
Damien Foggon
Production Editor
Eric Charbonneau
Copy Editor
Luann Rouff
Editorial Director
Robyn B. Siesky
Editorial Manager
Mary Beth Wakefield
Marketing Manager
David Mayhew
Production Manager
Tim Tate
Vice President and Executive Group Publisher
Richard Swadley
Vice President and Executive Publisher
Barry Pruett
Associate Publisher
Jim Minatel
Project Coordinator, Cover
Lynsey Stanford
Proofreader
Jen Larsen, Word One
Indexer
Johnna VanHoose Dinse
Cover Image
© biffspandex/istockphoto
Acknowledgments
First and Foremost I Want to thank God for giving me the wisdom and knowledge to share with others and for the many talents that he has blessed me with. I would also like to thank all the people at Wiley who work so hard to bring this book to market. I'd be remiss if I didn't thank my good friend and co-author Bryan Newsome; thanks for your hard work and dedication.
—Thearon
Thanks to Everyone at Wiley who worked so hard to get this book on the shelves. Special thanks to Maureen Spears who went above and beyond to help me finish my review on schedule. Of course, thanks goes out to Thearon Willis for completing one more edition.
—Bryan
Introduction
Visual Basic 2010 is Microsoft's latest version of the highly popular Visual Basic .NET programming language, one of the many languages supported in Visual Studio 2010. Visual Basic 2010's strength lies in its ease of use and the speed at which you can create Windows Forms applications, WPF Windows applications, Web applications, WPF Browser applications, mobile device applications, and Web Services.
In this book, we introduce you to programming with Visual Basic 2010 and show you how to create these types of applications and services. Along the way you'll also learn about object-oriented techniques and learn how to create your own business objects and Windows controls.
Microsoft's .NET Framework provides Visual Basic 2010 programmers with the capability to create full object-oriented programs, just like the ones created using C# or C++. The .NET Framework provides a set of base classes that are common to all programming languages in Visual Studio 2010, which provides you with the same capability to create object-oriented programs as a programmer using C# or C++.
This book will give you a thorough grounding in the basics of programming using Visual Basic 2010; from there the world is your oyster.
This book is designed to teach you how to write useful programs in Visual Basic 2010 as quickly and easily as possible.
There are two kinds of beginners for whom this book is ideal:
You're a beginner to programming and you've chosen Visual Basic 2010 as the place to start. That's a great choice! Visual Basic 2010 is not only easy to learn, it's also fun to use and very powerful.You can program in another language but you're a beginner to .NET programming. Again, you've made a great choice! Whether you've come from Fortran or Visual Basic 6, you'll find that this book quickly gets you up to speed on what you need to know to get the most from Visual Basic 2010.Visual Basic 2010 offers a great deal of functionality in both tools and language. No one book could ever cover Visual Basic 2010 in its entirety—you would need a library of books. What this book aims to do is to get you started as quickly and easily as possible. It shows you the roadmap, so to speak, of what there is and where to go. Once we've taught you the basics of creating working applications (creating the windows and controls, how your code should handle unexpected events, what object-oriented programming is, how to use it in your applications, and so on) we'll show you some of the areas you might want to try your hand at next:
Chapters 1 through 9 provide an introduction to Visual Studio 2010 and Windows programming.Chapter 6 provides an introduction to XAML and Windows Presentation Foundation (WPF) programming.Chapter 10 provides an introduction to application debugging and error handling.Chapters 11 through 13 provide an introduction to object-oriented programming and building objects.Chapter 14 provides an introduction to creating Windows Forms user controls.Chapters 15 and 16 provide an introduction to programming with databases and covers Access, SQL Server, and ADO.NET.Chapters 17 and 18 provide an introduction to Dynamic Data Web Sites and ASP.NET and show you how to write applications for the Web.Chapter 19 provides a brief introduction to XML, a powerful tool for integrating your applications with others—regardless of the language they were written in.Chapter 20 introduces you to deploying applications using ClickOnce technology.Apart from a willingness to learn, all you'll need for the first 15 chapters are a PC running Windows 7 (preferred), or Windows Vista, Windows XP (Home or Professional Edition), Windows Server 2008, Windows Server 2003; Internet Explorer; and of course:
Microsoft Visual Basic 2010 Professional Editionor
or
or
To help you get the most from the text and keep track of what's happening, we've used a number of conventions throughout the book.
Try It Out
The Try It Out is an exercise you should work through, following the text in the book.
1. They usually consist of a set of steps.
2. Each step has a number.
3. Follow the steps through with your copy of the database.
How It Works
After each Try It Out, the code you've typed will be explained in detail.
Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text.
Tips, hints, tricks, and asides to the current discussion look like this.
As for other conventions in the text:
New terms and important words are highlighted in italics when first introduced.Keyboard combinations are treated like this: Ctrl+R.Filenames, URLs, and code within the text are treated like so: persistence.properties.This book uses monofont type with no highlighting for most code examples.
This book uses bolding to emphasize code that is of particular importance in thepresent context.
As you work through the examples in this book, you may choose either to type in all the code manually or to use the source-code files that accompany the book. All of the source code used in this book is available for download at www.wrox.com. Once at the site, simply locate the book's title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book's detail page to obtain all the source code for the book.
Because many books have similar titles, you may find it easiest to search by ISBN; this book's ISBN is 978-0-470-50222-8.
Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.
We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save another reader hours of frustration, and at the same time you will be helping us provide even higher-quality information.
To find the errata page for this book, go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that have been submitted for this book and posted by Wrox editors. A complete book list, including links to each book's errata, is also available at www.wrox.com/misc-pages/booklist.shtml.
If you don't spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We'll check the information and, if appropriate, post a message to the book's errata page and fix the problem in subsequent editions of the book.
For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a web-based system on which you can post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.
At http://p2p.wrox.com you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:
1. Go to p2p.wrox.com and click the Register link.
2. Read the terms of use and click Agree.
3. Complete the required information to join as well as any optional information you wish to provide, and click Submit.
4. You will receive an e-mail with information describing how to verify your account and complete the joining process.
You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.
Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.
For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.
Chapter 1
Welcome to Visual Basic 2010
What You Will Learn in this Chapter
Using event-driven programmingInstalling Visual Basic 2010A tour of the Visual Basic 2010 integrated development environment (IDE)Creating a simple Windows programUsing the integrated Help systemThis is an exciting time to enter the world of programming with Visual Basic 2010 and Windows 7. Windows 7 represents the latest Windows operating system from Microsoft and is packed with a lot of new features to make Windows programming fun. Much has changed in the Windows user interface, and Visual Basic 2010 makes it easy to write professional-looking Windows applications as well as web applications and web services. Haven't upgraded to Windows 7 yet? No worries, Visual Basic 2010 also enables you to write professional-looking applications for previous versions of Windows as well.
The goal of this book is to help you use the Visual Basic 2010 programming language, even if you have never programmed before. You will start slowly and build on what you have learned in subsequent chapters. So take a deep breath, let it out slowly, and tell yourself you can do this. No sweat! No kidding!
Programming a computer is a lot like teaching a child to tie his shoes. Until you find the correct way of giving the instructions, not much is accomplished. Visual Basic 2010 is a language you can use to tell your computer how to do things; but, like a child, the computer will understand only if you explain things very clearly. If you have never programmed before, this sounds like an arduous task, and sometimes it can be. However, Visual Basic 2010 offers an easy-to-use language to explain some complex tasks. Although it never hurts to have an understanding of what is happening at the lowest levels, Visual Basic 2010 frees the programmer from having to deal with the mundane complexities of writing Windows applications. You are free to concentrate on solving real problems.
Visual Basic 2010 helps you create solutions that run on the Microsoft Windows operating systems, such as Windows 7, Windows Server 2008, and Windows Mobile 6.1. If you are looking at this book, you might have already felt the need or desire to create such programs. Even if you have never written a computer program before, as you progress through the Try It Out exercises in this book, you will become familiar with the various aspects of the Visual Basic 2010 language, as well as its foundations in the Microsoft .NET Framework. You will find that it is not nearly as difficult as you imagined. Before you know it, you will feel quite comfortable creating a variety of different types of programs with Visual Basic 2010.
Visual Basic 2010 can also be used to create web applications and web services, as well as mobile applications that can run on Pocket PCs or smartphones. However, you will begin by focusing on Windows applications before extending your boundaries to other platforms.
A Windows program is quite different from yesteryear's MS-DOS program. A DOS program follows a relatively strict path from beginning to end. Although this does not necessarily limit the functionality of the program, it does limit the road the user has to take to get to it. A DOS program is like walking down a hallway; to get to the end you have to walk down the entire hallway, passing any obstacles that you may encounter. A DOS program would only let you open certain doors along your stroll.
Windows, on the other hand, opened up the world of event-driven programming. Events in this context include clicking a button, resizing a window, or changing an entry in a text box. The code that you write responds to these events. In terms of the hallway analogy: In a Windows program, to get to the end of the hall you just click the end of the hall. The hallway itself can be ignored. If you get to the end and realize that is not where you wanted to be, you can just set off for the new destination without returning to your starting point. The program reacts to your movements and takes the necessary actions to complete your desired tasks.
Another big advantage in a Windows program is the abstraction of the hardware, which means that Windows takes care of communicating with the hardware for you. You do not need to know the inner workings of every laser printer on the market just to create output. You do not need to study the schematics for graphics cards to write your own game. Windows wraps up this functionality by providing generic routines that communicate with the drivers written by hardware manufacturers. This is probably the main reason why Windows has been so successful. The generic routines are referred to as the Windows , and most of the classes in the .NET Framework take care of communicating with those APIs.
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!