Beginner's Guide to Visual Basic

  • Published on
    04-Oct-2015

  • View
    215

  • Download
    0

DESCRIPTION

Visual basic

Transcript

Beginner's Guide To Visual Basic Take A Crash Course In A Language That Aids Novice Windows Programmers

Beginner's Guide To Visual Basic Take A Crash Course In A Language That Aids Novice Windows Programmers

Design a user interface. Set properties. Write code. Nothing gets more people started building Windows applications faster than the Visual Basic programming language. So its easy to understand why bookstores shelves groan with the weight of books claiming to make you a Visual Basic programmer by yesterday.

Writing Windows applications has become an enormously complicated task, but Visual Basic manages to hide much of that complexity. While it remains hidden, it doesnt go away. Writing good applications in Visual Basic doesnt come as easily as an afternoon of reading. Writing them takes hard-won skills. This article cant take you from here to there, but it can get you started, and it does give you a roadmap for the hardest part of the journey: the beginning.

INCLUDEPICTURE "http://www.smartcomputing.com/siteart/pixblue.gif" \* MERGEFORMATINET

Some History & Perspective

How did Microsoft end up with a product called Visual Basic? Basic comes from the computer language of BASIC (Beginners All-Purpose Symbolic Instruction Code), which has cast its shadow upon more programmers than any other code in the short history of computing. Since the development of BASIC in 1964, it has held a central role in programming. The BASIC language has been in widespread use for both casual and corporate developers. Even in Microsofts own company history, BASIC has endured in one platform or another. A form of BASIC was Microsofts first product, and the company has produced a version of it ever since.

Visual Basic is yet another result of Microsofts adoption of the BASIC language. It doesnt take too much exposure to realize that in application development, Microsoft uses the term visual to refer to the method used to put together a programs user interface. This term definitely has meaning in Visual Basic. Rather than writing voluminous code to describe the placement and appearance of interface elements, Visual Basic simply lets you choose and place prebuilt components on-screen.

While Visual Basic has its advantages and is dramatically different from the BASIC language of yore, it is still saddled with baggage and bad habits from the past. Still, it has Microsofts full support and backing. In fact, Microsoft inextricably intertwined it with its Windows operating system. Due to its simple syntax and ease of use, many developers use Visual Basic to create Windows applications or incorporate new elements into their existing applications. The typical application developed with Visual Basic, however, is created by corporate developers for internal consumption. In this domain, its low learning curve and quick development cycle more than make up for its design and performance flaws.

The Different Faces Of Visual Basic

Before addressing the basics of Visual Basic, its important to set the scope of discussion. Visual Basic is a family of Microsoft products and languages. There are other forms of BASIC around, but they are miniscule compared to the Microsoft family. Visual Basic is a family because Microsoft uses variants of the BASIC language in different contexts. The major family members are Visual Basic, Visual Basic for Applications (VBA), and Visual Basic Scripting Edition (VBScript).

Visual Basic. The Visual Basic format is a standalone tool. You can use it to create software components, including executable programs, COM components, and ActiveX controls. It is most useful when you are building an application or component, especially a graphical one, from scratch.

VBA. This version provides much of Visual Basics power in the context of an existing application such as Microsoft Excel and Word. Its the natural tool for customizing such software. Microsofts site says Visual Basic for Applications 6.0 is a core Office 2000 component and is integrated into each Office application.

VBScript. You can use this lightweight version of the Visual Basic language for scripting. Scripting generally means that the source code is compiled into an executable format only at the time the program is run. Microsoft specifically designed VBScript for use on Web pages. It lacks an integrated development environment.

This article focuses on Visual Basic, the standalone development tool of applications. One significant benefit of learning Visual Basic is that the language and its tools are significantly similar across all three contexts. While the concepts addressed by Visual Basic are more extensive than for the other two versions, the language syntax is similar, and there is a rich crossover so a large amount of the effort you invest in learning Visual Basic will also carry over.

The Latest Editions

Visual Basic 6 is the current incarnation of Visual Basic. While the older versions of Visual Basic (especially version 5) are still viable tools for producing Windows applications, it is generally best to stick with the latest tools despite the upgrade expense.

Microsoft continually rolls out enhancements for its language products so that users can take advantage of new features in its operating systems. It describes Visual Basic 6 as ready for Windows 2000. Concrete improvements in this version include new database access tools and a high-performance native code compiler. The native code compiler adds fast code execution to interactive development.

In addition to choosing a version, you must also decide which of three editions suits your situation.

Visual Basic Learning Edition. This entry-level tool, targeted at hobbyist or novice programmers, contains all the basic controls, such as buttons, grids, and text boxes, as well as the all-important Microsoft Developer Network (MSDN) Library. The library contains reams of articles and other references essential for developers.

Visual Basic Professional Edition. This edition of Visual Basic is considerably more expensive, and its primarily for professional and corporate developers. It includes additional components and tools aimed at database and Web development.

Visual Basic Enterprise Edition. This advanced edition, designed for teams of multiple corporate developers, will probably cost you more than your mortgage payment.

To get help in choosing the right edition, see Microsofts Visual Basic Web site (http://msdn.microsoft.com/vbasic).

Visual Basic 101

While you can follow a simple recipe and create your first application in literally three minutes, mastering this beast is going to take months of meticulous dedication. The concepts required to effectively use Visual Basic can be broken down in many different ways. Most of the books on this subject choose their own method of organization. One simple division is to consider language concepts and application concepts.

Language concepts relate to learning the syntax and semantics (loosely interpreted, the form and meaning) of the Visual Basic language itself. Application concepts are concerned with the organization of an application and the libraries of functions and objects used to create your finished product. Application concepts build on your understanding of the language concepts.

For example, consider the skills needed to build a cathedral. Language concepts are equivalent to skills such as shaping stone and laying stones on each other. Application concepts are equivalent to designing blueprints calling for buttresses, transepts, and vaults. You cant understand the latter until youve mastered the former.

Language concepts. Unfortunately, there is not enough space in this article to cover the full syntax of Visual Basic, but it is not particularly difficult to learn. People familiar with programming in general should be able to get started immediately.

Conditional statements include a familiar, though wordy, if-this-then-this construct, as well as a case statement for times when your program logic has to choose from among more than two alternatives. The go-to and go-sub statements are remnants of darker days for BASIC; most people avoid them today. Visual Basics four looping constructs should be enough for anyones stylistic preferences.

Visual Basic also contains a stable full of built-in data types for storing values such as numbers, characters, and strings. In this article, weve only scratched the surface of the language concepts. Microsofts program documentation or a strong reference book such as Pure Visual Basic by Dan Fox are good places to look for more details.

Application concepts. These concepts describe the basic building blocks of a Visual Basic program, such as windows, forms, controls, classes, and modules. Programming a Visual Basic application begins with creating the programs visual interface, which you build by assembling forms and controls.

Windows play a central role in Microsofts modern operating systems. They provide a foundation for interacting with users. Forms in Visual Basic are analogous to windows. A form is an object with properties defining its appearance. It also has methods defining its behavior as well as events defining the ways in which it interacts with users. Visual Basic makes it easy to set a forms properties. And by writing Visual Basic code to respond to events, it is relatively easy to customize the object for your application.

Controls are user interface objects embedded into a form. A dialog box provides a simple example. The dialog box itself is a form containing a number of controls such as buttons, text, checkboxes, and input fields. Controls, like forms, have properties, methods, and events, each suited to the particular type of control. For example, a button responds to a click event, but a text-label control does not. By writing code that responds to a click event, a Visual Basic programmer attaches the basic logic of the program to its user interface.

Modules are the containers for Visual Basic code. The form, standard, and class modules are common to all editions of Visual Basic. Form modules (using the .FRM file extension) are the basis of most Visual Basic applications. Standard modules (.BAS) are not associated with a particular form and can be reused in many applications; as such, they are containers for generic declarations and procedures that are useful beyond the scope of a single form. Class modules (.CLS) are the basis for object-oriented programming in Visual Basic. The Professional and Enterprise editions include additional module types.

Templates are a concept added to Visual Basic 5. A template is essentially a standard form or object created once and reused in a number of applications. By reusing the same templates, such as one for a log-in dialog box, developers can enforce a consistent look and feel to all their applications. Everyone wins with templates: Developers do not have to continuously write and maintain similar code, and users only have to learn a single style for common forms.

The Integrated Development Environment

For the Visual Basic beginner, simply firing up the development environment can be a little intimidating. First, youll see the complicated-looking New Project dialog box, complete with colorful icons and multiple choices. Fear not, getting comfortable with the Visual Basic IDE (Integrated Development Environment) isnt difficult, and if you understand the application concepts described above, its that much easier. Half of the fun of learning Visual Basic is getting to know how to use the IDE, which serves as home to your development efforts.

The Project area is a control in the upper-right corner that displays all the forms and modules in your current project. A project is a set of files (containing application objects described above) that youre using to build an application or component.

The Properties window, found directly beneath the Project area, is probably the most important control. It exposes all the properties of the currently selected form or control. Property names display on the left, and the current value displays on the right. By changing a property value, you change your applications look or behavior. In the lower-right corner, the Form Layout window is where to set the Desktop position where the forms in your application will appear.

On the left side of the IDE is the toolbox. This includes a palette of controls, or user interaction objects, that you can incorporate into programs. The central space in the IDE is, by default, reserved for the Form window, which is active when you have a project open. It lets you design and customize the interface of your application. The Code window below is an editor where you enter application code.

Other windows (such as the Immediate, Locals, and Watches windows) come into play while debugging your application. Use the View menu to toggle all of these windows on and off.

No tour of the IDE would be complete without covering a critical tool, the Object Browser. Press F2 to activate the Object Browser, which lists objects available for use in our project. Use the Object Browser to quickly navigate through your source code and see what methods and properties are available in your forms and controls, as well as in other applications.

Master The Art

Building simple Windows applications with Visual Basic is easy, learning to master Visual Basic takes real time and effort. Probably the most common method for building skills is to buy the program and a do-it-yourself book.

You can broaden your experience with community college classes and continuing education courses that target Visual Basic skills. Taking a course means someone is available to directly field your questions. Corporate courses and seminars are usually more expensive but can be an effective solution for corporate developers.

You can search the Web to find Visual Basic users groups near you. Search for vbug or Visual Basic Users Group and your location.

Other resources include Web pages such as http://www.vbexplorer.com and http://vbwire.com, as well as newsgroups such as comp.lang.basic .visual.announce and comp.lang.basic .visual.database.

This overview is the first step toward joining the ranks of Visual Basic programmers. The rest is up to you. Dont forget to have fun along the way.

10 Commandments Of Learning Visual Basic

1. Learn by doing. No matter how much you read, your abilities are always strengthened by actually writing, editing, debugging, and running code.

2. The F1 key is your friend. The Visual Basic development environment makes heavy use of context-sensitive help. For example, to find out about a property, simply click its name in the Properties window and press the F1 key.

3. The F2 key is another close ally. Learn to use the Object Browser. Navigating the vast number of properties attached to all the objects and controls available in Visual Basic is a snap once youre familiar with the Object Browser.

4. Get to know your controls. Much of Visual Basic development consists of assembling a set of controls and setting their properties. The better you know the tools in your toolbox, the better your applications.

5. Step through your code. The debugging capabilities built into the Visual Basic environment are the envy of programmers on other platforms. Learn them and use them frequently.

6. Step through someone elses code. Run the sample code that comes with Visual Basic. Studying someone elses code is a fast way to pick up programming ideas, especially when theyre more experienced than you are.

7. Run your code often. Microsoft has gone to great lengths to make executing your program fast within the IDE. Press the F5 key and test your program often.

8. Factor in common code. Take advantage of templates and standard modules to reuse a single version of code rather than constantly duplicating it.

9. Buy rather than build. One of the most powerful aspects of Visual Basic is its vast array of of-the-shelf components. Reduce your projects complexity by acquiring functionality instead of building it yourself.

10. Keep yourself up to date. Visual Basic is a moving target. Its capabilities are always growing and changing. Maintain the skills youve worked hard to develop and take advantage of tutorials, newsgroups, FAQs (frequently asked questions), and other online help.