ptg7068940 i Rebecca M. Riordan Fluent Visual Basic ® ptg7068940 ii Fluent Visual Basic © Copyright © 2011 by Rebecca Riordan All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. ISBN-13: 9780672335808 ISBN-10: 0672335808 Library of Congress Cataloging-in-Publication Data is on file. Printed in the United States of America First Printing November 2011 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. The Windlass Lowercase and Brandywine fonts are copyrights of the Scriptorium foundry, www.fontcraft.com. Wa r n i n g and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact International Sales international@pearson.com Associate Publisher Greg Wiegand Signing Editor Neil Rowe Managing Editor Kristy Hart Project Editor Andy Beaster Indexer Cheryl Lenser Proofreader Karen Gill Technical Editor John Hardesty Publishing Coordinator Cindy Teeters Cover Designer Gary Adair Composition Rebecca Riordan ptg7068940 Acknowledgements Yes, I know it says “Rebecca M. Riordan” on the cover, but that’s not really true. Without the assistance of some amazing people, this book would never have made it out of my head, much less into your hands. So, in order of appearance, I would like to thank: Neil Rowe, my editor, who took a chance on a very different way of writing computer tutorials. Without Neil’s leap of faith, Fluent Learning would never have happened. My technical reviewers, David Sceppa, Eric Weinburger and John Hardesty, who collectively caught an embarassing number of code typos and I-knew-what-I-meant obscurities. Finally, my copy editor, Karen Gill, who not only made sure the language in the book resembles English, but also expressed an unexpected and greatly appreciated enjoyment in the project. (Any remaining errors and infelicities are, of course, my responsibility.) Jake von Slatt of The Steampunk Workshop (steampunkworkshop.com), Samantha Wright (samantha-wright.deviantart.com) and Mindbloom (mindbloom.com) were all gracious enough to allow me to use their images. These are all seriously cool people, folks. I can’t urge you strongly enough to go explore their sites. iii ptg7068940 iv Getting Started Introduction . . 1 Fluent Learning Because This book isn’t for everyone What you’ll learn What you’ll need How it works Application Development . . 9 The development process System design Creating executables The .NET Platform . . . . . . . .43 .NET Components Say hello Say what? The Visual Studio UI 67 Solutions, projects and stuff Take control Get some help Testing & Deployment . . . . .93 Errors & exceptions Deployment The Language Part 1: Nouns . . . . . . . . . . . .121 Statements Declared elements Comments Directives & Attributes Part 2: Transitive Verbs . . .155 Literal expressions Object expressions Part 3: Intransitive Verbs . . 181 Control of flow commands Exception handling commands The .NET Framework Library Classes in the .NET Framework . 221 The Class Designer Class definitions Fields & properties Methods Other Framework Types . . . . . . . . 269 Structures Enumerations Interfaces Wo r k i n g w i t h t y p e s The Class Library, Part 1 . . .305 Namespaces The Object Browser Numeric data Character data Times & dates The Class Library, Part 2 . . .349 Arrays Specialized Sets Generics Discover the secret to efficient programming: The best code is the code you don’t have to write yourself. Learn how to speak Visual Basic. It’s a language, much like English, Spanish or Latin, only simpler. Find out what this whole “being a programmer” thing is all about and how to use the tools you’ll need to build applications. ptg7068940 v best practice OOA & D . 381 Type relationships OOP principles Type mo difiers Programming Principles . . . . 425 The Single Responsibility Principle The Open/Closed Principle The Liskov Substitution Principle The Law of Demeter Patterns . . . . . . . . . . . . . . . . . .457 The Strategy Pattern The Observer Pattern Architectural Patterns wpf XAML . . . . . . . . . . . . . . . . . .501 Fundamentals WPF types XAML & Visual Basic WPF Controls . . . . . . . . . . 531 WPF panels Control classes Content controls Items controls Dependency Properties . . . 591 The basics Creating dependency properties WPF Interactions . . . . . . . . 627 Routed events WPF Commands WPF Graphics . . . . . . . . . . 669 Color Brushes Pens Typo graphy Effects Resources . . . . . . . . . . . . . . . 719 Resource dictionaries Styles Property triggerrs Event triggers Templates . . . . . . . . . . . . . . .765 Building controls Building control templates The VisualStateManager WPF Binding . . . . . . . . . . . .797 Creating bindings Binding to collections Wo r k i n g w i t h c o l l e c t i o n s Put all you’ve learned to good use by learning how to use Microsoft’s latest and greatest interface platform. Stand on the shoulders of the experts by learning the best programming practices and how to implement them. Contents ptg7068940 vi Tell Us What you think! As the reader of this book, you are our most important critic and commentator. We value your opinion and want to know what we’re doing right, what we could do better, what areas you’d like to see us publish in, and any other words of wisdom you’re willing to pass our way. As a Executive Editor for Sams, I welcome your comments. You can fax, email, or write me directly to let me know what you did or didn’t like about this book —as well as what we can do to make our books stronger. Please note that I cannot help you with technical problems related to the topic of this book, and that due to the high volume of email I receive, I might not be able to reply to every message. When you write, please be sure to include this book’s title and author as well as your name and email address, phone, or fax number. I will carefully review your comments and share them with the author and editors who worked on this book. Email: feedback@samspublishing.com Fax: 317-428-3310 Mail: Neil Rowe, Executive Editor Sams Publishing 800 East 96th Street Indianapolis, IN 46240 USA ptg7068940 1 Welcome! I’m so glad you stopped by. This book doesn’t look much like other technical tutorials, does it? Well, for once, looks aren’t deceiving, because Fluent Learning books aren’t much like other technical tutorials. We don’t want to teach you things. We want to help you learn things. We’ve done a lot of research into how people learn, and it turns out that talk, talk, talking at you (like most books do) isn’t wrong, exactly, but it makes learning harder than it needs to be. Did you learn to speak your native language by reading a book? Of course not; that’s not how people learn. You heard people speaking, tried it for yourself, and then corrected yourself when other people pointed out your mistakes. Sure, you studied grammar and learned new words in school, but the basics (“More milk, Mommy”) you learned by yourself. Now, barring accident and illness (or one-too-many mojitos), you’re not likely to forget it, are you? And you don’t have to think about the mechanics of speech, just what you want to say. That’s really how we learn everything. We gather some initial information, practice, correct our mistakes, and then add to our basic knowledge. That’s not what happens in most tutorials, but that’s how Fluent Learning works. I’ll give you enough information to get started, give you some exercises to figure out how to do something, and then elaborate on what you’ve learned. Simple, natural, and if you do the work, you will learn. Soon you’ll be able to concentrate on what you want to do, not how to do it, just like when you learned to speak. (But it won’t take as long as learning to speak well.) ptg7068940 2 Put on your thinking hat… You have a messy stack of papers and a spool of wire. What can you do? public String Message; On your own Objects instantiate classes. That is, they are instances of them. The statement below contains both a class and an object. Which do you think is which? Fluent Learning Because… We Want to Learn, not listen Our minds like to learn anything the way we learned our native language: by trial & error. Instead of reading a lot of words, you’ll do a lot of exercises. Real exercises, that make you think, not walkthroughs that tell you what to type. (But we’ll have a few of those, too.) We want to work, not pass tests You want to be able to apply what you learn in the real world, not just pass a test on the subject. To help you do that, the On Your Own exercises invite you to make those connections as part of the learning process. ptg7068940 3 We don’t live in a world of words Our minds absorb information through all our senses, not just speech. We’ll use graphics, context and rhythmic language to appeal to sight, touch and sound. We think in patterns, not straight lines Our minds work best with patterns, not individual facts. We’ll always present new ideas in context to help you understand how all the pieces fit together. We want a map, not a mystery Our minds are constantly evaluating progress, but that’s not possible unless we know where we’re going. We’ll use lots of signposts so you always know where you are and where you’re headed. We’re artists, not freight trains Our minds don’t chug along from point A to point B on a single track. Like any artist, we start with a sketch and then fill in the details. Rather than presenting all the information about a topic at one time, we’ll start simply and add the details as you have more context. That way they’ll stick. ptg7068940 4 This book is for you if: • You don’t know anything (or at least not much) about programming but want to learn. • You’ve been using a computer for awhile but don’t really know how applications get written. • You’ve done a little playing around with Visual Studio and Visual Basic but don’t really feel comfortable about your skills. It’s scary not knowing what you don’t know, and this book can help with that. This book probably isn’t for you if: • You don’t know how to use a computer at all. I assume that you know how to do things like opening applications, clicking buttons and choosing items from menus, so if you don’t know that yet, you might want to start with a basic book on using Windows and come back to this one later. • You know a version of VB, but haven’t used it in awhile and want to brush up your skills. This book isn’t well suited to browsing and skimming. This book isn’t for everyone I really hate it when technical books announce that they’re for “everyone”, don’t you? It can’t possibly be true, and saying that it is doesn’t sell any more books in the long run. It just irritates people who get confused (because the book’s too advanced for them) or bored (because the book’s too basic). I don’t want to irritate or bore you, so I’ll say it plainly: This book isn’t for everyone. This book might be for you if: • You’re a Visual Basic 6 programmer and want to learn Visual Basic .NET starting with the basics. • You’re a Visual Basic 6 programmer and want to learn to build WPF or Silverlight applications in Visual Basic .NET. There are lots of WPF/Silverlight tutorials around, and some of them are very good, but most of them assume you know C#. If you don’t, this might be a good introductory book for you. • You’re a beginning Visual Basic programmer and want to learn patterns & best practices. There are books around that specialize on that subject, but they tend to be pretty advanced, so you might find this book more comfortable. [...]... testers and visual designers Like most Visual Basic programmers, we’ll be using Microsoft Visual Studio, and we’ll look at all these functions throughout this book Once the application is written and compiled into an executable, it operates in a specific runtime environment In this book we’ll be developing applications that run under Microsoft Windows, but you should know that Visual Basic and Visual Studio... and I can’t give you that But I can get you started after you finish this book you will: • Understand the structure of the Visual Basic language, its basic syntax, expressions and commands • Understand the role that the NET Framework plays in application development • Know the basics of the NET Framework core types and how to use them • Have a working knowledge of designing object-oriented applications using design... won’t: • Know everything there is to know about how the Visual Basic compiler or the CLR works (I’m not sure anyone does.) • Know every nook and cranny of the Framework Class Library (But you will know how to explore it.) 5 What you’ll need You don’t need much to get started This book, of course (You did buy this copy, didn’t you?) And a copy of Visual Studio You’ll also need something to write with,... project starts all over again! 10 The software executes in a specific runtime environment, like Windows or a browser Using the specification, a programmer creates in a programming language like Visual Basic source code The development environment provides the tools to turn the source code into working software 11 hOW’D YOU DO? I gave you a messy stack of paper and spool of wire Thinking Cap... design patterns, which are standard solutions to common programming problems 16 .one more time “Programming” is largely (but not only) a matter of writing source code That means, for us, writing Visual Basic statements and using the functionality provided by the NET Framework As you’ll see, the NET Framework does a lot of the work for us Most programmers use an integrated development environment (ide)...What you’ll learn You and I both know that you won’t be an expert Visual Basic programmer after reading one book, no matter how good it might be You won’t be an expert after reading two books, or three, or a dozen Of course you should read what the experts have to say But... Visual Studio You’ll also need something to write with, because not all of the exercises are done at the computer, and you might want a notebook of some kind if you don’t like writing in books getting visual studio Getting the source code You won’t need to download the source code in order to do the exercises in this book, but it’s available on the Web at informit.com/title/9780672335808 6 How it... Executables The integrated development environment (IDE) provides the tools that translate source code into an executable, a piece of software that a user or another piece of software can execute We’ll be using Visual Studio to create executables, and before we can make decisions about how to structure an application, we need a little more detail about how that works in the context of the NET Framework On Your... ingredient maintenance screen System error System displays descriptive message 22 Put on your thinking hat Time to try writing some use cases on your own Based on the descriptions below, try writing out the basic steps and one or two alternate paths for each case “Our recipes are based on a number of servings that’s right for the restaurant, but for catering, we often need to scale the recipes up or down... understand UML diagrams That makes them easy to use in teams and will help you understand books (like this one) that use UML diagrams There are tools that can help you manage UML diagrams As we’ll see, Visual Studio uses a kinda-sorta UML for generating source code in some of its designers There are other programs and languages out there that do the same thing 25 An Example The abstract diagram on . ptg7068940 i Rebecca M. Riordan Fluent Visual Basic ® ptg7068940 ii Fluent Visual Basic © Copyright © 2011 by Rebecca Riordan All rights reserved. No part. and want to learn Visual Basic .NET starting with the basics. • You’re a Visual Basic 6 programmer and want to learn to build WPF or Silverlight applications in Visual Basic .NET. There are lots. too basic) . I don’t want to irritate or bore you, so I’ll say it plainly: This book isn’t for everyone. This book might be for you if: • You’re a Visual Basic 6 programmer and want to learn Visual