28,99 €
Learn Visual Basic step by step and start programming right away Beginning Visual Basic 2015 is the ideal guide for new programmers, especially those learning their first language. This new edition has been updated to align with Visual Studio 2015, and also refocused to concentrate on key beginner topics. Precise, step-by-step instructions walk you through important tasks, and clear explanations targeted to beginners will have you writing your first Visual Basic application quickly. You'll start from the absolute beginning, assuming no prior programming experience, and then gradually build your skills to write Visual Basic applications for Windows and the Web. Coverage includes objects, class libraries, graphics, databases, and much more, with explicit instructions on using ASP.NET, SQL Server, ADO.NET, and XML. Visual Studio is the usual environment for Visual Basic programming, and the latest upgrade has made Visual Basic more feature compatible with C# to allow programmers to move fluidly between the two languages. Don't know C#? Don't worry! This book starts from the very beginning of Visual Basic programming to help you build your skills from the ground-up. * Understand flow control and data structure * Debug Windows applications, dialog boxes, and menus * Master objects and object-oriented techniques * Access databases, program graphics, and program for the Web Over three million programmers use Visual Basic, and many of them learned it as their first language. It's beginner-friendly, versatile, and visually oriented, making it an ideal introduction to the programming mindset, workflow, and hard skills. Beginning Visual Basic 2015 gets you started on the right foot, with clear, patient instruction and plenty of hands-on practice.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 904
Veröffentlichungsjahr: 2015
Bryan Newsome
Beginning Visual Basic® 2015
Published byJohn Wiley & Sons, Inc.10475 Crosspoint BoulevardIndianapolis, IN 46256www.wiley.com
Copyright © 2016 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-119-09211-7
ISBN: 978-1-119-09207-0 (ebk)
ISBN: 978-1-119-09208-7 (ebk)
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 publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2015953114
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, 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. Visual Basic is a registered trademark of Microsoft Corporation. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book.
For my girls, Jennifer and Katelyn.
BRYAN NEWSOME leads a team of top developers specializing in Microsoft solutions. Since starting building Visual Basic 5 solutions, he has embraced each new version of 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.
VALAN MONEY is a Microsoft Certified Solution Developer for Web and Windows applications. He has been using Visual Basic since 1996 and got his first certification in Visual Basic 4.0. Since then, he has also received certifications in newer versions of VB and VB.NET. He works as Technical Team Lead and Senior Consultant for projects using Microsoft technologies. He holds a Masters degree in Computer Science from St. Joseph’s College, Tiruchirappalli, India.
Senior Acquisitions Editor Kenyon Brown
Project Editor Maureen S. Tullis
Technical Editor Valan Money
Production Editor Dassi Zeidel
Copy Editor Scott D. Tullis
Manager of Content Development & Assembly Mary Beth Wakefield
Marketing Director David Mayhew
Marketing Manager Carrie Sherrill
Professional Technology & Strategy Director Barry Pruett
Business Manager Amy Knies
Associate Publisher Jim Minatel
Project Coordinator, Cover Brent Savage
Proofreader Nancy Carrasco
Indexer Johnna VanHoose Dinse
Cover Designer Wiley
Cover Image ©anasimin/iStockphoto
Thanks to those who worked so hard to get this book on the shelves at Wiley and Wrox. Special thanks (again) to Maureen Tullis who went above and beyond to help me finish mostly on schedule.
INTRODUCTION
WHO THIS BOOK IS FOR
WHAT THIS BOOK COVERS
WHAT YOU NEED TO USE THIS BOOK
CONVENTIONS
SOURCE CODE
ERRATA
P2P.WROX.COM
1 WELCOME TO VISUAL BASIC 2015
IMPLEMENTING EVENT-DRIVEN PROGRAMMING
INSTALLING VISUAL BASIC 2015
THE VISUAL STUDIO 2015 IDE
CREATING A SIMPLE APPLICATION
USING THE HELP SYSTEM
SUMMARY
2 THE MICROSOFT .NET FRAMEWORK
THE .NET VISION
WRITING SOFTWARE FOR WINDOWS
COMMON LANGUAGE RUNTIME
THE COMMON TYPE SYSTEM AND COMMON LANGUAGE SPECIFICATION
SUMMARY
3 WRITING SOFTWARE
INFORMATION AND DATA
WORKING WITH VARIABLES
COMMENTS AND WHITESPACE
DATA TYPES
STORING VARIABLES
METHODS
SUMMARY
4 CONTROLLING THE FLOW
MAKING DECISIONS
THE IF STATEMENT
SELECT CASE
LOOPS
SUMMARY
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
6 BUILDING WINDOWS APPLICATIONS
RESPONDING TO EVENTS
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
ABOUT DIALOG
SUMMARY
7 DISPLAYING DIALOGS
THE MESSAGEBOX
THE OPENFILEDIALOG CONTROL
THE SAVEDIALOG CONTROL
THE FONTDIALOG CONTROL
THE COLORDIALOG CONTROL
THE PRINTDIALOG CONTROL
THE FOLDERBROWSERDIALOG CONTROL
SUMMARY
8 CREATING MENUS
UNDERSTANDING MENU FEATURES
CREATING MENUS
CONTEXT MENUS
SUMMARY
9 DEBUGGING AND ERROR HANDLING
MAJOR ERROR TYPES
DEBUGGING
ERROR HANDLING
USING STRUCTURED ERROR HANDLING
SUMMARY
10 BUILDING OBJECTS
UNDERSTANDING OBJECTS
BUILDING CLASSES
REUSABILITY
DESIGNING AN OBJECT
CONSTRUCTORS
INHERITANCE
OBJECTS AND STRUCTURES
THE FRAMEWORK CLASSES
SUMMARY
11 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
12 ACCESSING DATA USING STRUCTURED QUERY LANGUAGE
WHAT YOU NEED TO COMPLETE THIS CHAPTER’S EXERCISES
WHAT IS A DATABASE?
UNDERSTANDING BASIC SQL SYNTAX
EXECUTING QUERIES IN SQL SERVER
SUMMARY
13 DATABASE PROGRAMMING WITH SQL SERVER AND ADO.NET
ADO.NET
ADO.NET DATA NAMESPACES
THE ADO.NET CLASSES IN ACTION
DATA BINDING
SUMMARY
14 ASP.NET
THIN-CLIENT ARCHITECTURE
WEB FORMS VERSUS WINDOWS FORMS
WEB APPLICATIONS: THE BASIC PIECES
ACTIVE SERVER PAGES
BUILDING WEBSITES
SUMMARY
15 DEPLOYING YOUR APPLICATION
WHAT IS DEPLOYMENT?
VISUAL STUDIO 2015 SETUP APPLICATION OPTIONS
DEPLOYING DIFFERENT SOLUTIONS
SUMMARY
16 WINDOWS 8 APPS
WINDOWS 8 APPLICATION DESIGN PRINCIPLES
APPLICATION LAYOUT
SUMMARY
APPENDIX EXERCISE SOLUTIONS
ADVERT
EULA
Chapter 1
Table 1.1
Chapter 7
Table 7.1
Table 7.2
Table 7.3
Table 7.4
Table 7.5
Table 7.6
Table 7.7
Table 7.8
Table 7.9
Table 7.10
Table 7.11
Table 7.12
Chapter 12
Table 12.1
Chapter 13
Table 13.1
Table 13.2
Chapter 14
Table 14.1
Table 14.2
Chapter 16
Table 16.1
Chapter 1
Figure 1.1
Figure 1.2
Figure 1.3
Figure 1.4
Figure 1.5
Figure 1.6
Figure 1.7
Figure 1.8
Figure 1.9
Figure 1.10
Figure 1.11
Figure 1.12
Figure 1.13
Figure 1.14
Figure 1.15
Figure 1.16
Figure 1.17
Figure 1.18
Figure 1.19
Chapter 2
Figure 2.1
Figure 2.2
Chapter 3
Figure 3.1
Figure 3.2
Figure 3.3
Figure 3.4
Figure 3.5
Figure 3.6
Figure 3.7
Figure 3.8
Figure 3.9
Figure 3.10
Figure 3.11
Figure 3.12
Figure 3.13
Figure 3.14
Figure 3.15
Figure 3.16
Chapter 4
Figure 4.1
Figure 4.2
Figure 4.3
Figure 4.4
Figure 4.5
Figure 4.6
Figure 4.7
Figure 4.8
Figure 4.9
Figure 4.10
Figure 4.11
Figure 4.12
Figure 4.13
Figure 4.14
Figure 4.15
Figure 4.16
Figure 4.17
Figure 4.18
Figure 4.19
Figure 4.20
Figure 4.21
Figure 4.22
Figure 4.23
Figure 4.24
Chapter 5
Figure 5.1
Figure 5.2
Figure 5.3
Figure 5.4
Figure 5.5
Figure 5.6
Figure 5.7
Figure 5.8
Figure 5.9
Figure 5.10
Figure 5.11
Figure 5.12
Figure 5.13
Figure 5.14
Figure 5.15
Figure 5.16
Figure 5.17
Figure 5.18
Figure 5.19
Figure 5.20
Figure 5.21
Figure 5.22
Figure 5.23
Figure 5.24
Figure 5.25
Chapter 6
Figure 6.1
Figure 6.2
Figure 6.3
Figure 6.4
Figure 6.5
Figure 6.6
Figure 6.7
Figure 6.8
Figure 6.9
Figure 6.10
Figure 6.11
Figure 6.12
Figure 6.13
Chapter 7
Figure 7.1
Figure 7.2
Figure 7.3
Figure 7.4
Figure 7.5
Figure 7.6
Figure 7.7
Figure 7.8
Figure 7.9
Figure 7.10
Figure 7.11
Figure 7.12
Figure 7.13
Figure 7.14
Figure 7.15
Figure 7.16
Chapter 8
Figure 8.1
Figure 8.2
Figure 8.3
Figure 8.4
Figure 8.5
Figure 8.6
Figure 8.7
Figure 8.8
Figure 8.9
Figure 8.10
Chapter 9
Figure 9.1
Figure 9.2
Figure 9.3
Figure 9.4
Figure 9.5
Figure 9.6
Figure 9.7
Figure 9.8
Figure 9.9
Figure 9.10
Figure 9.11
Figure 9.12
Figure 9.13
Figure 9.14
Figure 9.15
Figure 9.16
Figure 9.17
Figure 9.18
Figure 9.19
Figure 9.20
Figure 9.21
Figure 9.22
Figure 9.23
Figure 9.24
Figure 9.25
Figure 9.26
Chapter 10
Figure 10.1
Figure 10.2
Figure 10.3
Figure 10.4
Figure 10.5
Figure 10.6
Figure 10.7
Figure 10.8
Figure 10.9
Figure 10.10
Figure 10.11
Figure 10.12
Figure 10.13
Figure 10.14
Figure 10.15
Figure 10.16
Figure 10.17
Chapter 11
Figure 11.1
Figure 11.2
Figure 11.3
Figure 11.4
Figure 11.5
Figure 11.6
Figure 11.7
Figure 11.8
Figure 11.9
Figure 11.10
Figure 11.11
Figure 11.12
Chapter 12
Figure 12.1
Figure 12.2
Figure 12.3
Figure 12.4
Figure 12.5
Figure 12.6
Figure 12.7
Figure 12.8
Figure 12.9
Chapter 13
Figure 13.1
Figure 13.2
Figure 13.3
End Class
Figure 13.4
Figure 13.5
Figure 13.6
Figure 13.7
Figure 13.8
Figure 13.9
Figure 13.10
Figure 13.11
Figure 13.12
Figure 13.13
Chapter 14
Figure 14.1
Figure 14.2
Figure 14.3
Figure 14.4
Figure 14.5
Figure 14.6
Figure 14.7
Figure 14.8
Figure 14.9
Figure 14.10
Figure 14.11
Figure 14.12
Figure 14.13
Figure 14.14
Figure 14.15
Figure 14.16
Figure 14.17
Chapter 15
Figure 15.1
Figure 15.2
Figure 15.3
Figure 15.4
Figure 15.5
Figure 15.6
Figure 15.7
Chapter 16
Figure 16.1
Figure 16.2
Figure 16.3
Figure 16.4
Figure 16.5
Figure 16.6
Figure 16.7
Figure 16.8
Figure 16.9
Figure 16.10
Figure 16.11
Cover
Table of Contents
Chapter
ix
xi
xiii
xv
xxvii
xxviii
xxix
xxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
515
516
517
518
519
520
521
522
523
524
525
526
527
529
530
531
532
533
534
536
537
538
542
543
544
545
546
547
548
549
550
551
552
553
554
555
557
558
559
560
561
562
563
565
566
567
592
VISUAL BASIC 2015 IS Microsoft’s latest version of the highly popular Visual Basic .NET programming language, one of the many languages supported in Visual Studio 2015. Visual Basic 2015’s strength lies in its ease of use and the speed at which you can create Windows Forms and Windows 8 applications, web applications, and mobile device applications.
In this book, we introduce you to programming with Visual Basic 2015 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 2015 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 2015, 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 2015; from there the world is your oyster.
This book is designed to teach you how to write useful programs in Visual Basic 2015 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 2015 as the place to start. That’s a great choice! Visual Basic 2015 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 Cobol, 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 2015.
Visual Basic 2015 offers a great deal of functionality in both tools and language. No one book could ever cover Visual Basic 2015 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 8 provide an introduction to Visual Studio 2015 and Windows programming.
Chapter 9 provides an introduction to application debugging and error handling.
Chapter 10 provides an introduction to object-oriented programming and building objects.
Chapter 11 provides an introduction to creating Windows Forms user controls.
Chapters 12 and 13 provide an introduction to programming with databases and covers Structured Query Language, SQL Server, and ADO.NET.
Chapter 14 provides an introduction to ASP.NET and shows you how to write applications for the web.
Chapter 15 introduces you to deploying applications using ClickOnce technology.
Chapter 16 show you how to build your first Windows 8 application and introduces you to design principles for touch interfaces.
Apart from a willingness to learn, all you’ll need are a PC running Windows 8 (preferred), Windows 7, Windows Server 2008 R2, Windows Server 2015; Internet Explorer; and of course:
Microsoft Visual Basic 2015Community Edition or higher
For the database chapters, you should install SQL Server 2014 Express, although, any edition you have should work with little to no changes required.
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.
The Try It Out is an exercise you should work through, following the text in the book.
They usually consist of a set of steps.
Each step has a number.
Follow the steps through with your copy of the database.
After each Try It Out, the code you’ve typed will be explained in detail.
WARNING Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text.
NOTE 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 the present 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.
NOTE Because many books have similar titles, you may find it easiest to search by ISBN; this book’s ISBN is 978-1-119-09211-7
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 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 email 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:
Go to
p2p.wrox.com
and click the Register link.
Read the terms of use and click Agree.
Complete the required information to join as well as any optional information you wish to provide, and click Submit.
You will receive an email with information describing how to verify your account and complete the joining process.
NOTE 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 emailed 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.
WHAT YOU WILL LEARN IN THIS CHAPTER:
Using event-driven programming
Installing Visual Basic 2015
Touring the Visual Basic 2015 integrated development environment (IDE)
Creating a simple Windows program
Using the integrated Help system
WROX.COM CODE DOWNLOADS FOR THIS CHAPTER
The wrox.com code downloads for this chapter are found at www.wrox.com/begvisualbasic2015 on the Download Code tab. The code is in the 092117 C01.zip download.
This is an exciting time to enter the world of programming with Visual Basic 2015 and Windows 8 and the new Windows 10. The new Windows operating systems represent the latest from Microsoft and are packed with a lot of new features to make Windows programming fun. Much has changed in the Windows user interface, and Visual Basic 2015 makes it easy to write professional-looking Windows applications as well as web applications and web services. Haven’t upgraded to Windows 8 or 10 yet? No worries; Visual Basic 2015 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 2015 programming language, even if you have never programmed before. You 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 2015 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 2015 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 2015 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 2015 helps you create solutions that run on the Microsoft Windows operating systems, such as Windows 7, 8, or 10, Windows Server 2008, and Windows Phone. 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 2015 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 2015.
Visual Basic 2015 can also be used to create web applications and web services, as well as mobile applications that can run on Tablet 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 let you open only 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 application programming interface (API), and most of the classes in the .NET Framework take care of communicating with those APIs.
Before Visual Basic 1 was introduced to the world in 1991, developers had to be well versed in C and C++ programming, as well as the building blocks of the Windows system itself, the Windows API. This complexity meant that only dedicated and properly trained individuals were capable of turning out software that could run on Windows. Visual Basic changed all that, and it has been estimated that there are now as many lines of production code written in Visual Basic as in any other language.
Visual Basic changed the face of Windows programming by removing the complex burden of writing code for the user interface (UI). By allowing programmers to draw their own UI, it freed them to concentrate on the business problems they were trying to solve. When the UI is drawn, the programmer can then add the code to react to events.
Visual Basic has also been extensible from the very beginning. Third-party vendors quickly saw the market for reusable modules to aid developers. These modules, or controls, were originally referred to as VBXs (named after their file extension). Prior to Visual Basic 5, if you did not like the way a button behaved, you could either buy or create your own, but those controls had to be written in C or C++. Database access utilities were some of the first controls available. Version 5 of Visual Basic introduced the concept of ActiveX, which enabled developers to create their own ActiveX controls.
When Microsoft introduced Visual Basic 3, the programming world changed significantly. You could build database applications directly accessible to users (so-called front-end applications) completely with Visual Basic. There was no need to rely on third-party controls. Microsoft accomplished this task with the introduction of Data Access Objects (DAOs), which enabled programmers to manipulate data with the same ease as manipulating the user interface.
Versions 4 and 5 extended the capabilities of version 3 to enable developers to target the new Windows 95 platform. They also made it easier for developers to write code, which could then be manipulated to make it usable to other language developers. Version 6 provided a new way to access databases with the integration of ActiveX Data Objects (ADOs). The ADO feature was developed by Microsoft to aid web developers using Active Server Pages (ASP) to access databases. All the improvements to Visual Basic over the years have ensured its dominant place in the programming world—it helps developers write robust and maintainable applications in record time.
You may own Visual Basic 2015 in one of the following forms:
As part of Visual Studio 2015, a suite of tools and languages that also includes C# (pronounced “C-sharp”) and Visual C++:
The Visual Studio 2015 product line includes Visual Studio Professional Edition or Visual Studio Tools Team Editions. The Team Edition versions come with progressively more tools for building and managing the development of larger, enterprise-wide applications.
As Visual Basic 2015 Express Edition:
This is a free edition for students and beginners, which includes the Visual Basic 2015 language and a smaller set of the tools and features available with Visual Studio 2015.
Both these products enable you to create your own applications for the Windows platform. The installation procedure is straightforward. In fact, the Visual Studio Installer is smart enough to figure out exactly what your computer requires to make it work.
The descriptions in the following Try It Out exercise are based on installing Visual Studio 2015 CTP 6. Most of the installation processes are straightforward, and you can accept the default installation options for most environments. Therefore, regardless of which edition you are installing, the installation process should be smooth when accepting the default installation options.
There are two common ways to install Visual Studio. You can burn a DVD or attach a downloaded image (iso) or use the web installer. To use the web installer, just follow the on-screen prompts. If you choose to download the iso, follow these steps:
After downloading vs2015.ctp_ult_enu.iso, right-click the file and select Mount for the context menu. The Visual Studio 2015 DVD will open in a new Explorer Window. You need to run
vs_ultimate.exe
from the root directory of the DVD. Then click OK to start the setup program. After the setup program initializes, you see the initial screen, as shown in
Figure 1.1
. This dialog displays the location and size of the installation.
You need to select the type of installation on this screen. You should select Typical. After selecting the option, click Install to begin.
FIGURE 1.1
NOTEIf you are a Windows Vista, Windows 7, or Windows 8 user, you may be prompted that the setup program needs to run, in which case you will need to grant permission to let it continue. After the setup program continues, you can sit back and relax while all the features are being installed. This process can take 20 minutes or more depending on the installation features chosen and the speed of your computer.
Once the installation is complete, you are presented with a dialog informing you of the status of the installation, after which you will most likely have to restart.
Now the real fun can begin—so get comfortable, relax, and enter the world of Visual Basic 2015.
You don’t need Visual Basic 2015 to write applications in the Visual Basic .NET language. The capability to run Visual Basic .NET code is included with the .NET Framework. You could write all your Visual Basic .NET code using a text editor such as Notepad. You could also hammer nails using your shoe as a hammer, but that slick pneumatic nailer sitting there is a lot more efficient. In the same way, by far the easiest way to write in Visual Basic .NET code is by using the Visual Studio 2015 Integrated Development Framework, known as the IDE. This is what you see when working with Visual Basic 2015: the windows, boxes, and so on. The IDE provides a wealth of features unavailable in ordinary text editors—such as code checking, visual representations of the finished application, and an explorer that displays all the files that make up your project.
An IDE is a way of bringing together a suite of tools that makes developing software a lot easier. If Visual Studio is not running, fire it up and see what you’ve got. If you used the default installation, go to your Windows Apps or Search Menu and then select Visual Studio 2015 CTP. A splash screen appears briefly so you can sign in. Because you will work offline for this book, you should choose Not Now, Maybe Later. When you create an account for free, your settings are saved and all your computers are synced to the same settings as far as Visual Studio is concerned. You will have more direct access to some online resources.
Then you see the Choose Default Environment Settings dialog. Select the Visual Basic Development Settings option and your choice for color theme; then click Start Visual Studio, as shown in Figure 1.2.
FIGURE 1.2
After Visual Studio configures the environment based on the chosen settings, the Microsoft Development Environment appears, as shown in Figure 1.3.
FIGURE 1.3
By now, you might be eager to start writing some code; but hold off and begin your exploration of the IDE by looking at the menu and toolbar, which are not really all that different from the toolbars and menus available in other Windows applications (although they differ from the Ribbon in Microsoft Office 2007 and some of the newer Windows applications).
The Visual Studio 2015 menu is dynamic, which means items are added or removed depending on what you are trying to do. When looking at the blank IDE, the menu bar consists only of the File, Edit, View, Build, Debug, Team, SQL, Data, Format, Tools, Unit Test, Analyze, Window, and Help menus. When you start working on a project, however, the full Visual Studio 2015 menu appears, as shown in Figure 1.4.
FIGURE 1.4
At this point, there is no need to cover each menu topic in detail. You will become familiar with each of them as you progress through the book. Here is a quick rundown of what activities each menu item pertains to:
File:
Most software programs have a File menu. It has become the standard where you should find, if nothing else, a way to exit the application. In this case, you can also find ways of opening and closing single files and whole projects.
Edit:
The Edit menu provides access to the common items you would expect: Undo, Redo, Cut, Copy, Paste, and Delete.
View:
The View menu provides quick access to the windows that exist in the IDE, such as the Solution Explorer, Properties window, Output window, Toolbox, and so on.
Debug:
The Debug menu enables you to start and stop running your application within the Visual Basic 2015 IDE. It also gives you access to the Visual Studio 2015 debugger. The debugger enables you to step through your code while it is running to see how it is behaving.
Team:
This menu connects to Team Foundation Server. You use this when working with a team to build software.
Tools:
The Tools menu has commands to configure the Visual Studio 2015 IDE, as well as links to other external tools that may have been installed.
Architecture:
This menu provides options for high-level design tools.
Test:
The Test menu provides options that enable you to create and view unit tests for your application to exercise the source code in various scenarios.
Analyze:
The Analyze menu helps you check your code. Use this menu to run performance- and code-analysis tools to help you write better code.
Window:
This menu has become standard for any application that allows more than one window to be open at a time, such as Word or Excel. The commands on this menu enable you to switch between the windows in the IDE.
Help:
The Help menu provides access to the Visual Studio 2015 documentation. There are many different ways to access this information (for example, through the Help contents, an index, or a search). The Help menu also has options that connect to the Microsoft website to obtain updates or report problems.
Many toolbars are available within the IDE, including Formatting, Image Editor, and Text Editor, which you can add to and remove from the IDE through the View ➪ Toolbars menu option. Each one provides quick access to frequently used commands, preventing you from having to navigate through a series of menu options. For example, the icon (New Project) on the default toolbar (called the Standard toolbar), shown in Figure 1.5, is available from the menu by navigating to File ➪ New Project.
FIGURE 1.5
The toolbar is segmented into groups of related options, which are separated by vertical bars:
Navigation:
The first group of icons is for navigating through code. Use these to go backward and forward as you move through your code.
Project and file options:
The next four icons provide access to the commonly used project- and file-manipulation options available through the File and Project menus, such as opening and saving files.
Code commenting:
The third group of icons is for commenting out and un-commenting sections of code. This process can be useful in debugging when you want to comment out a section of code to determine what results the program might produce by not executing those lines of code.
Managing code edits:
The fourth group of icons is for undoing and redoing edits and for navigating through your code.
Code step through:
The fifth group of icons provides the ability to start (via the green triangle), pause, and stop your application. You can also use three icons in this group to step into your code line by line, step over entire sections of code, and step out of a procedure. The solution configuration is used to build your project so you can debug and step into your code or so you can produce a build that you can release to customers. These icons will be covered in depth in Chapter 9.
Find in files dialog:
The final icon gives you access to the Find in Files dialog. This is an important feature that you will use often. You can also access this dialog by the shortcut keys Ctrl+F.
If you forget what a particular icon does, you can hover your mouse pointer over it so that a tool tip appears displaying the name of the toolbar option.
You could continue to look at each of the windows in the IDE by clicking the View menu and choosing the appropriate window; but as you can see, they are all empty at this stage and therefore not very revealing. The best way to look at the capabilities of the IDE is to use it while writing some code.
To finish your exploration of the IDE, you need to create a project so that the windows shown earlier in Figure 1.3 have some interesting content for you to look at.
In this Try It Out exercise, you will create a very simple application called Hello User that allows you to enter a person’s name and display a greeting to that person in a message box:
Click the New Project button on the toolbar.
In the New Project dialog, select Visual Basic in the Installed Templates tree-view box to the left. The Templates pane on the right will display all the available templates for the project type chosen. Select the Windows Forms Application template.
Type
Hello User
in the Name text box and click OK. Your New Project dialog should look like
Figure 1.6
.
FIGURE 1.6
Visual Studio 2015 allows you to target your application to a specific version of the Microsoft .NET Framework. The combo box at the top of the Templates pane in the New Project dialog has version 4.5 selected, but you can target your application to earlier versions of the .NET Framework.
The IDE then creates an empty Windows application for you. So far, your Hello User program consists of one blank window, called a Windows Form (or sometimes just a form), with the default name of Form1.vb, as shown in Figure 1.7.
FIGURE 1.7
Whenever Visual Studio 2015 creates a new file, either as part of the project creation process or when you create a new file, it uses a name that describes what it is (in this case, a form) followed by a number.
At this point, you can see that the various windows in the IDE are beginning to show their purposes, and you should take a brief look at them now before you come back to the Try It Out exercise.
NoteIf any of these windows are not visible on your screen, you can use the View menu to show them. Also, if you do not like the location of any particular window, you can move it by clicking its title bar and dragging it to a new location. The windows in the IDE can float (stand out on their own) or be docked (as they appear in Figure 1.7).
The following list introduces the most common windows:
Toolbox:
This contains reusable controls and components that can be added to your application. These range from buttons to data connectors to customized controls that you have either purchased or developed.
Design window:
This window is where a lot of the action takes place. This is where you draw your user interface on your forms. This window is sometimes referred to as
the Designer
.
Solution Explorer:
This window contains a hierarchical view of your solution. A
solution
can contain many projects, whereas a
project
contains forms, classes, modules, and components that solve a particular problem.
Properties:
This window shows what
properties
the selected object makes available. Although you can set these properties in your code, sometimes it is much easier to set them while you are designing your application (for example, drawing the controls on your form). You will notice that the
File Name
property has the value
Form1.vb
. This is the physical filename for the form’s code and layout information.
Next, you’ll give your form a name and set a few properties for it:
Change the name of your form to something more indicative of your application. Click
Form1.vb
in the Solution Explorer window. Then, in the Properties window, change the
File Name
property from
Form1.vb
to
HelloUser.vb
and press Enter, as shown in
Figure 1.8
.
When changing properties you must either press Enter or click another property for it to take effect. Note that the form’s filename has also been updated in the Solution Explorer to read
HelloUser.vb
.
Click the form displayed in the Design window. The Properties window changes to display the form’s
Form
properties (rather than the
File
properties, which you have just been looking at).
FIGURE 1.8
NOTE The Properties window is dramatically different. This difference is the result of two different views of the same file. When the form name is highlighted in the Solution Explorer window, the physical file properties of the form are shown. When the form in the Design window is highlighted, the visual properties and logical properties of the form are shown.
FIGURE 1.9
NOTE If you have trouble finding properties, click the little AZ icon on the toolbar toward the top of the Properties window. This changes the property listing from being ordered by category to being ordered by name.
The Properties window allows you to set a control’s properties easily. Properties are a particular object’s set of internal data; they usually describe appearance or behavior. In Figure 1.9 you can see that properties appear alphabetically. The properties can also be grouped together in categories: Accessibility, Appearance, Behavior, Data, Design, Focus, Layout, Misc, and Window Style.
Right now, the title (
Text
property) of your form (displayed in the bar at the top of the form) is
Form1
. This is not very descriptive, so change it to reflect the purpose of this application. Locate the
Text
property in the Properties window. Change the
Text
property’s value to
Hello from Visual Basic 2015
and press Enter. Note that the form’s title has been updated to reflect the change.
You are now finished with this procedure. Click the Start button on the Visual Studio 2015 toolbar (the green triangle) to run the application. As you work through the book, whenever we say “run the project” or “start the project,” just click the Start button. An empty window with the title Hello from Visual Basic 2015 appears.
That was simple, but your little application isn’t doing much at the moment. Let’s make it a little more interactive. To do this, you will add some controls—a label, a text box, and two buttons—to the form. This will enable you to see how the Toolbox makes adding functionality quite simple. You may be wondering at this point when you will actually look at some code. Soon! The great thing about Visual Basic 2015 is that you can develop a fair amount of your application without writing any code. Sure, the code is still there, behind the scenes, but as you will see, Visual Basic 2015 writes a lot of it for you.
The Toolbox is accessed through the View ➪ Toolbox menu option, by clicking the Toolbox icon on the Standard menu bar, or by pressing Ctrl+Alt+X. Alternatively, the Toolbox tab appears on the left of the IDE; hovering your mouse over this tab causes the Toolbox window to fly out, partially covering your form.
The Toolbox contains a node-type view of the various controls and components that can be placed onto your form. Controls such as text boxes, buttons, radio buttons, and combo boxes can be selected and then drawn onto your form. For the HelloUser application, you’ll use only the controls in the Common Controls node. Figure 1.10 shows a listing of common controls for Windows forms.
FIGURE 1.10
Controls can be added to your forms in any order, so it doesn’t matter whether you add the label control after the text box or the buttons before the label.
In the following Try It Out exercise, you start adding controls.
Stop the project if it is still running because you now want to add some controls to your form. The simplest way to stop your project is to click the close (X) button in the top right corner of the form. Alternatively, you can click the blue square on the toolbar (which displays a tool tip that says “Stop Debugging” if you hover over it with your mouse pointer).
Add a Label control to the form. Click Label in the Toolbox, drag it over to the form’s Designer, and drop it in the desired location. (You can also place controls on your form by double-clicking the required control in the Toolbox or clicking the control in the Toolbox and then drawing it on the form.)
If the Label control you have just drawn is not in the desired location, no problem. When the control is on the form, you can resize it or move it around.
Figure 1.11
shows what the control looks like after you place it on the form. To move it, click the control and drag it to the desired location. The label automatically resizes itself to fit the text that you enter in the
Text
property.
After drawing a control on the form, you should at least configure its name and the text that it will display. You will see that the Properties window to the right of the Designer has changed to
Label1
, telling you that you are currently examining the properties for the label. In the Properties window, set your new label’s
Text
property to
Enter Your Name
. Note that after you press Enter or click another property, the label on the form has automatically resized itself to fit the text in the
Text
property. Now set the
Name
property to
lblName
.
Directly beneath the label, you want to add a text box so that you can enter a name. You will repeat the procedure you followed for adding the label, but this time make sure you select the TextBox control from the toolbar. After you have dragged and dropped (or double-clicked) the control into the appropriate position as shown in
Figure 1.12
, use the Properties window to set its
Name
property to
txtName
. Notice the sizing handles on the left and right side of the control. You can use these handles to resize the text box horizontally.
In the bottom left corner of the form, add a Button control in exactly the same manner as you added the label and text box. Set its Name property to btnOK and its Text property to &OK. Your form should now look similar to the one shown in Figure 1.13.
The ampersand (&) is used in the Text property of buttons to create a keyboard shortcut (known as a hot key). The letter with the & sign placed in front of it becomes underlined (as shown in Figure 1.13) to signal users that they can select that button by pressing the Alt+letter key combination, rather than using the mouse (on some configurations the underline doesn’t appear until the user presses Alt). In this particular instance, pressing Alt+O would be the same as clicking the OK button. There is no need to write code to accomplish this.
Now add a second Button control to the bottom right corner of the form by dragging the Button control from the Toolbox onto your form. Notice that as you get close to the bottom right of the form, a blue snap line appears, as shown in Figure 1.14. This snap line enables you to align this new Button control with the existing Button control on the form. The snap lines assist you in aligning controls to the left, right, top, or bottom of each other, depending on where you are trying to position the new control. The light blue line provides you with a consistent margin between the edge of your control and the edge of the form. Set the Name property to btnExit and the Text property to E&xit. Your form should look similar to Figure 1.15.
FIGURE 1.11
FIGURE 1.12
FIGURE 1.13
FIGURE 1.14
FIGURE 1.15
Now, before you finish your sample application, the following section briefly discusses some coding practices that you should be using.
You might have noticed that the names given to the controls look a little funny. Each name is prefixed with a shorthand identifier describing the type of control it is. This makes it much easier to understand what type of control you are working with as you look through the code. For example, say you had a control called simply Name, without a prefix of lbl or txt. You would not know whether you were working with a text box that accepted a name or with a label that displayed a name. Imagine if, in the previous Try It Out exercise, you had named your label Name1 and your text box Name2—you would very quickly become confused. What if you left your application for a month or two and then came back to it to make some changes?
When working with other developers, it is very important to keep the coding style consistent. One of the most commonly used styles for control names within application development in many languages was designed by Dr. Charles Simonyi, who worked for the Xerox Palo Alto Research Center (XPARC) before joining Microsoft. He came up with short prefix mnemonics that allowed programmers to easily identify the type of information a variable might contain. Because Simonyi is from Hungary, and the prefixes make the names look a little foreign, this naming system became known as Hungarian Notation. The original notation was used in C/C++ development, so the notation for Visual Basic 2015 is termed Modified Hungarian Notation. Table 1-11.1 shows some of the commonly used prefixes that you’ll utilize in this book.
Table 1.1 Common Prefixes in Visual Basic 2015
CONTROL
PREFIX
Button
btn
ComboBox
cbo
CheckBox
chk
Label
lbl
ListBox
lst
MainMenu
mnu
RadioButton
rdb
PictureBox
pic
TextBox
txt
