Introduction

Welcome to Microsoft's Visual Basic 2010, another milestone version of the most popular programming language for building Windows and web applications. In modern software development, however, the language is only one of the components we use to build applications. The most important component is the .NET Framework, which is an indispensable component of every application; it's actually more important than the language itself. You can think of the Framework as an enormous collection of functions for just about any programming task. All drawing methods, for example, are part of the System.Drawing class. To draw a rectangle, you call the DrawRectangle method of the System.Drawing class, passing the appropriate arguments. To create a new folder, you call the CreateDirectory method of the Directory class, and to retrieve the files in a folder, you call the GetFiles method of the same class.

The Framework contains all the functionality of the operating system and makes it available to your application through methods. Methods are very similar to functions, which extend the basic capabilities of a language. The Framework is a huge collection of such methods, organized in units according to their role and in a way that makes it fairly easy to locate the methods for the task at hand. The language and the Framework are the two "programming" components absolutely necessary to build Windows applications. It's possible to develop applications with these two components alone, but the process would be awfully slow.

The software development process relies on numerous tools that streamline the coding experience. The third component is an integrated environment that hosts those tools, enabling you to perform many common tasks with point-and-click operations. It's basically an environment in which you can design your forms with visual tools and write code as well. This environment, provided by Visual Studio, is known as an integrated development environment, or IDE. You'll be amazed by the functionality provided by the tools of Visual Studio: you can actually design a functional data-driven application without writing a single line of code. You can use similar tools in the same environment to design a fancy data-driven web page without a single line of code. Visual Studio even provides tools for manipulating databases and allows you to switch between tasks, all in the same, streamlined environment. You realize, of course, that Visual Studio isn't about writing applications without code; it just simplifies certain tasks through wizards, and more often than not, we step in and provide custom code to write a functional application. Even so, Visual Studio provides numerous tools, from debugging tools that help you track and fix all kinds of bugs in your code to database-manipulation tools and deployment wizards that streamline the process of deploying applications.

This book shows you how to use Visual Studio 2010 and Visual Basic 2010 to design rich Windows and web applications. We'll start with the visual tools and then we'll explore Visual Basic and the Framework. A Windows application consists of a visual interface and code behind the elements of the interface. (The code handles the user actions on the visual interface, such as the click of a button, the selection of a menu item, and so on.) You'll use the tools of Visual Studio to build the visual interface, and then you'll program the elements of the application with Visual Basic. For any nontrivial processing, such as file and folder manipulation, data storage, and so on, you'll use the appropriate classes of the .NET Framework. A substantial segment of this book deals with the most useful components of the Framework. We will also explore databases and data-driven applications, which are the most common type of business applications. Finally, we'll go through the basics of web programming. You'll learn how to build web applications with Visual Basic and how to write web services.

The Mastering Series

The Mastering series from Sybex provides outstanding instruction for readers with intermediate and advanced skills in the form of top-notch training and development for those already working in their field and clear, serious education for those aspiring to become pros. Every Mastering book includes the following:

  • Real-World Scenarios, ranging from case studies to interviews, that show how the tool, technique, or knowledge presented is applied in actual practice

  • Skill-based instruction, with chapters organized around real tasks rather than abstract concepts or subjects

  • Self-review test questions, so you can be certain you're equipped to do the job right

Who Should Read This Book?

You don't need a solid knowledge of Visual Basic to read this book, but you do need a basic understanding of programming. You need to know the meaning of variables and functions and how an If...Then structure works. This book is aimed at the typical programmer who wants to get the most out of Visual Basic. It covers the topics I felt are of use to most VB programmers, and it does so in depth. Visual Basic 2010 and the .NET Framework 4.0 are two extremely rich programming tools, and I had to choose between a superficial coverage of many topics and an in-depth coverage of fewer topics. To make room for more topics, I have avoided including a lot of reference material and lengthy listings. For example, you won't find complete project listings or form descriptions. I assume that you can draw a few controls on a form and set their properties and that you don't need long descriptions of the control properties (even if you don't know how to design a form, you'll learn how in the first two chapters). I'm also assuming that you don't want to read the trivial segments of each application. Instead, the listings concentrate on the "meaty" part of the code: the procedures that explain the topic at hand.

The topics covered in this book were chosen to provide a solid understanding of the principles and techniques for developing applications with Visual Basic. Programming isn't about new keywords and functions. I chose the topics I felt every programmer should learn in order to master the language. I was also motivated by my desire to present useful, practical examples. You will not find all topics equally interesting or important. My hope is that everyone will find something interesting and something of value for their daily work — whether it's an application that maps the folders and files of a drive to a TreeView control, an application that prints tabular data, a data-driven application for editing customers or products, or an application that saves a collection of objects to a file.

Many books offer their readers long, numbered sequences of steps to accomplish a task. Following instructions simplifies certain tasks, but programming isn't about following instructions. It's about being creative; it's about understanding principles and being able to apply the same techniques in several practical situations. And the way to creatively exploit the power of a language such as Visual Basic 2010 is to understand its principles and its programming model.

In many cases, I provide a detailed, step-by-step procedure that will help you accomplish a task, such as designing a menu, for example. But not all tasks are as simple as designing menus. I explain why things must be done in a certain way, and I present alternatives and try to connect new topics to those explained earlier in the book. In several chapters, I expand on applications developed in earlier chapters. Associating new knowledge with something you have mastered already provides positive feedback and a deeper understanding of the language.

This book isn't about the hottest features of the language either; it's about solid programming techniques and practical examples. After you master the basics of programming Windows applications with Visual Basic 2010 and you feel comfortable with the more advanced examples of the book, you will find it easy to catch up with the topics not discussed in this book.

How about the Advanced Topics?

Some of the topics discussed in this book are nontrivial, and quite a few topics can be considered advanced. Creating collections of custom objects and querying them and exposing some functionality in the form of web services are not trivial topics, but these are the tools that will allow you to make the most of Visual Studio.

You may also find some examples to be more difficult than you expected. I have tried to make the text and the examples easy to read and understand, but not unrealistically simple. Understanding the basic functions for manipulating files and folders isn't difficult. To make the most of these functions, however, you need to understand how to scan a folder's files, including the files in its subfolders and the files in their subfolders, with a technique known as recursion. To make each chapter as useful as possible, I've included nontrivial examples, which will provide a better understanding of the topics. In addition, many of these examples can be easily incorporated into your applications.

You can do a lot with the TreeView control with very little programming, but to make the most out of this control, you must be ready for some advanced programming — nothing terribly complicated, but some things just aren't trivial. Programming most of the operations of the TreeView control, for instance, is not complicated, but if your application calls for populating a TreeView control with an arbitrary number of branches (such as mapping a directory structure to a TreeView control), the code can get complex. The same goes for printing; it's fairly straightforward to write a program that prints some text, but printing tabular reports takes substantial coding effort.

The reason I've included the more advanced examples is that the corresponding chapters would be incomplete without them. If you find some material to be over your head at first reading, you can skip it and come back to it after you have mastered other aspects of the language. But don't let a few advanced examples intimidate you. Most of the techniques are well within the reach of an average VB programmer. The few advanced topics were included for the readers who are willing to take that extra step and build elaborate interfaces by using the latest tools and techniques.

There's another good reason for including advanced topics. Explaining a simple topic, such as how to populate a collection with items, is very simple. But what good is it to populate a collection if you don't know how to save it to disk and read back its items in a later session? Likewise, what good is it to learn how to print simple text files? In a business environment, you will most likely be asked to print a tabular report, which is substantially more complicated than printing text. One of my goals in writing this book was to exhaust the topics I've chosen to discuss and present all the information you need to do something practical: not just how to create collections, but also how to save them in disk files; not just how to write to a file, but also how to prompt users for a filename with the same dialog box all Windows applications use; not just how to print something, but also how to create a preview of the printout. In short, I've tried to include everything you need to know in order to incorporate in your applications the features everybody has come to expect from a Windows application.

The Structure of the Book

This book isn't meant to be read from cover to cover, and I know that most people don't read computer books this way. Each chapter is independent of the others, although all chapters contain references to other chapters. Each topic is covered in depth; however, I make no assumptions about the reader's knowledge of the topic. As a result, you may find the introductory sections of a chapter too simple. The topics become progressively more advanced, and even experienced programmers will find some new information in most chapters. Even if you are familiar with the topics in a chapter, take a look at the examples. I have tried to simplify many of the advanced topics and to demonstrate them with clear, practical examples.

This book tries to teach through examples. Isolated topics are demonstrated with short examples, and at the end of many chapters you'll build a large, practical application (a real-world application) that "puts together" the topics and techniques discussed throughout the chapter. You may find some of the more advanced applications a bit more difficult to understand, but you shouldn't give up. Simpler applications would have made my job easier, but the book wouldn't deserve the Mastering title, and your knowledge of Visual Basic wouldn't be as complete.

The book starts with the fundamentals of Visual Basic, even though very little of it is specific to version 2010. You'll learn how to design visual interfaces with point-and-click operations and how to program a few simple events, such as the click of the mouse on a button. After reading the first two chapters, you'll understand the structure of a Windows application. Then you'll explore the elements of the visual interface (the basic Windows controls) and how to program them. You'll also learn about the My object and code snippets, two features that make Visual Basic so simple and fun to use (again). These two objects will also ease the learning process and make it much simpler to learn the features of the language.

In Part 2, I discuss in detail the basic components of Windows applications. I explain the most common controls you'll use in building Windows forms as well as how to work with forms: how to design forms, how to design menus for your forms, how to create applications with multiple forms, and so on. You will find detailed discussions of many Windows controls as well as how to take advantage of the built-in dialog boxes, such as the Font and Color dialog boxes, in your applications.

Visual Basic 2010 is a truly object-oriented language, and objects are the recurring theme in every chapter. Part 3 of the book (Chapter 8, Chapter 9, and Chapter 10) contains a formal and more systematic treatment of objects. You will learn how to build custom classes and controls, which will help you understand object-oriented programming a little better. You will also learn about inheritance and will see how easy it is to add custom functionality to existing classes through inheritance.

Part 4 deals with some of the most common classes of the .NET Framework. The Framework is at the very heart of Windows programming; it's your gateway to the functionality of the operating system itself. The first chapter in this part of the book is an introduction to the Framework at large, and it shows you how to use the basic classes for manipulating files and folders, how to manipulate data and time, how to work with time spans, how to create graphics and printouts, and other interesting aspects of the Framework. In the next chapter you'll learn how to use collections in your code and then you'll find a chapter on XML and a chapter on LINQ. You will see how easy it is to create and use XML in your VB code as well as how to query collections, XML, and databases with a new language that's embedded into VB: Language Integrated Query (LINQ). LINQ is the hottest new technology that allows you to query data with widely different structures, and data from different sources, in a uniform way.

The first 14 chapters deal with the fundamentals of the language and Windows applications. Following these chapters, you will find an overview of the data-access tools. I'm assuming that the majority of you will eventually build a data-driven application. The emphasis in Part 5 is on the visual tools, and you will learn how to query databases and present data to the user. You will also find information on programming the basic objects of ADO.NET and write simple data-driven Windows applications.

In the last few chapters of this book you will learn about web applications, the basics of ASP.NET 4, how to develop data-bound web applications, and how to write web services. Since I could not discuss both Windows and web applications in the same detail, I've decided to focus on Windows applications, and in the last few chapters (Part 6) show you how to apply your knowledge to the Web. While the interface is totally different, the essential code is the same.

Don't Miss the Tutorials

In addition to the printed material, this book is accompanied by a number of tutorials, which you can download from www.sybex.com/go/masteringvb2010. These tutorials are actual chapters (some of them quite lengthy); we couldn't include them in the printed version of the book, so we included them as PDF files. They are as follows:

  • Accessing Files and Folders

  • Creating Graphics with VB 2010

  • Printing with VB 2010

  • Making the Most of the ListView and TreeView Controls

This book is a revision of Mastering Visual Basic 2008. As the book couldn't keep growing — and we had to make room for new topics — we decided to remove some chapters that were included in the previous edition of the book from the printed version. These chapters have been revised and edited and you will find them in PDF format at this book's website. Throughout this book, I'll be referring to them as tutorials; they're complete chapters with sample projects and the same structure as the book's chapters. You can download the tutorials from the same site as the book's projects and read them on your computer screen.

Downloading This Book's Code

The code for the examples and projects can be downloaded from the Sybex website (www.sybex.com). At the main page, you can find the book's page by searching for the author, the title, or the ISBN (9780470187425) and then clicking the book's link listed in the search results. On the book's page, click the Download link and it will take you to the download page. Or, you can go directly to the book's page at www.sybex.com/go/masteringvb2010. The downloaded source code is a ZIP file, which you can unzip with the WinZip utility.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.127.37