Visual Basic

From Wikibooks, the open-content textbooks collection

(Redirected from Programming:Visual Basic)
Jump to: navigation, search

This book is written from a VB6 perspective although much of what is said is also valid for VB5 and VB4. This book does not cover VB.NET which is in fact a radically different language. It covers many different techniques and topics including optimization of programs, object oriented programming and coding guidelines to name a few. To assist readers in getting started several complete applications of varying degrees of sophistication are presented in the Case Studies chapter.

Contents


[edit] Introduction

This book's sole purpose is to help people better understand Visual Basic.

[edit] History

[edit] Evolution of Visual Basic

VB 1.0 was introduced in 1991. The approach for connecting the programming language to the graphical user interface is derived from a system called Tripod (sometimes also known as Ruby), originally developed by Alan Cooper, which was further developed by Cooper and his associates under contract to Microsoft.

[edit] Timeline of Visual Basic

  • Visual Basic 1.0 (May 1991) was released for Windows.
  • Visual Basic 1.0 for DOS was released in September 1992. The language itself was not quite compatible with Visual Basic for Windows, as it was actually the next version of Microsoft's DOS-based BASIC compilers, Microsoft QuickBASIC compiler QuickBASIC and BASIC Professional Development System. The interface was barely graphical, using extended ASCII characters to simulate the appearance of a GUI.
  • Visual Basic 2.0 was released in November 1992. The programming environment was easier to use, and its speed was improved.
  • Visual Basic 3.0 was released in the summer of 1993 and came in Standard and Professional versions. VB3 included a database engine that could read and write Access databases.
  • Visual Basic 4.0 (August 1995) was the first version that could create 32-bit as well as 16-bit Windows programs. It also introduced the ability to write classes in Visual Basic.
  • With version 5.0 (February 1997), Microsoft released Visual Basic exclusively for 32-bit versions of Windows. Programmers who preferred to write 16-bit programs were able to import programs written in Visual Basic 4.0 to Visual Basic 5.0, and Visual Basic 5.0 programs can easily be converted with Visual Basic 4.0. Visual Basic 5.0 also introduced the ability to create custom user controls, as well as the ability to compile to native Windows executable code, speeding up runtime code execution.
  • Visual Basic 6.0 (Mid 1998) improved in a number of areas, including the ability to create web-based applications. VB6 is currently scheduled to enter Microsoft's "non-supported phase" starting March [2008].

[edit] Getting Started

VB6 IDE (Integrated Development Environment) is highly useful for developing GUI (Graphical User Interface) applications. In VB each visible object on the screen is referred to as a control. All controls are contained in forms.

[edit] Simple Arithmetic

Introduction to calculating and operands with Visual Basic

[edit] Branching

Branching occurs when the program makes a decision. The flow of execution follows a particular branch like a fork in the road.

[edit] Loops

Loops are control structures used to repeat a given section of code a certain number of times or until a particular condition is met.

[edit] Using Strings

Strings hold printable characters. They are very useful for communicating with human beings and for transferring data between programs and machines in text files and web pages.

[edit] Using Arrays and Matrices

Arrays are used to group data items of the same type together, to make it easier to search and sort through this data.

[edit] User Interfaces

A user interface is the part of your program that is visible to a human user. It can be as simple as a DOS command line or as sophisticated as an immersive virtual reality simulator. However, in the context of Visual Basic it usually means what is commonly referred to as a Graphical User Interface or GUI which generally consists of one or more Forms that contain text boxes, labels, buttons, picture boxes, etc.

[edit] Simple Graphics

Visual Basic can easily be used to draw simple graphics such as line drawings and diagrams.

[edit] Files

Another essential part of Visual Basic is file Input/Output, that is, working with files. While programming, you may want to at some point save data so they may be accessible for further use. This is where file I/O comes in. VB allows us to perform most operations available in Windows Explorer and DOS command line. Before you can do this, you need to understand how file I/O works.

[edit] Data Types

A datatype is a specification of the kind of information that a variable can hold, for example, a number or a piece of text.

Dictionary: A data structure that holds a key / value pair. For example it could be set up to have the key of "NC" and the value would be "North Carolina".

[edit] Procedures and Functions

A function is a calling procedure or small part of a program that performs some specific task and returns a result; for example the cosine function.

[edit] Using Windows Dialogs

Windows dialogs are useful when one requires standard interfaces, including opening files, saving files, choosing color and/or font, specifying printer settings.

[edit] Using Databases

VB can connect to databases in many different ways. The most common way now is to use ActiveX Data Objects (ADO).

To connect to an already existing ODBC database use:

Dim Conn As Object
Set Conn = CreateObject("ADODB.Connection")
Conn.Open "odbc name", "user", "password"

[edit] Using the Windows API

Although VB6 compiler can't create true DLLs, it has ability to call existing DLLs (not necessarily just Windows API of course). Declaration of such function contains extra keyword declare and lib, plus optionally can be defined an alias if the real name of function differs from name of dll function alias. For example it can be:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal ByteLen As Long)

For datatype Any accepts compiler any parameter provided. For effective using of API calls are important operators VarPtr which returns pointer at variable specified and AddressOf which is important for callback functions. Beginners often do mistake on missing or badly specified ByRef/Byval in declaration. Simply said, ByVal means that on stack will be pushed value and ByRef (or missing specification of calling convention) means that on stack will be pushed pointer at variable. In practice parameter type Long (4 bytes) will be called ByVal, but String or complex data types will be called ByRef.

Unfortunately, VB6 doesn't support C-like memory handling. To deal with this problem VB programmers uses function CopyMemory to copy memory like this:

Dim a As Long
Dim b() As Byte
ReDim b(3)
CopyMemory ByVal varptr(a), ByVal varptr(b), 4

which is relatively safe. Some of programmers at the higher level of knowledge can modify pointers at objects as well. Although most of API functions can be called this way, there is not possible to call entry points of dlls dynamically (LoadLibrary-GetEntryPoint-FreeLibrary) for general case. Much clearer way how to deal with such tasks is to move code of this kind into separated dll written in C++ and call this dll.

[edit] Subclassing - receiving messages from Windows

Visual Basic hides the details of all the messages that Windows sends to an application but you can get at them if you really need to. Then you can do things that plain Visual Basic cannot.

[edit] External Processes

Sometimes you want to call another program from Visual Basic.

[edit] Object Oriented Programming

There is a lot of complicated talk about Object Oriented Programming but in essence it is quite simple.

[edit] Effective Programming

It isn't enough that the program work, you must be able to maintain it as well. Effective programmers can read their code as well as write it.

[edit] Idioms

An idiom is a sort of template or generic method of expressing an idea. In the same way that idioms in a human language make life easier for the both speaker and listener good idioms in computer programming make life easier for the programmer. Naturally what is idiomatic in one language might not be in another.

[edit] Optimizing Visual Basic

Once your program is up and running and producing correct results you might want to consider optimizing it. This means making it run faster or use fewer or less expensive resources. As you gain experience you will recognize that some programming idioms produce better results than others under some circumstances. Being able to recognize these situations will enable you to choose the best method straight away

[edit] Examples

This section contains a number of ready made code examples for your use. They vary from short snippets to complete modules and short demo applications.

Visual Basic 6 Tutorials and Sample Source Code

Fun Example (How To Mimic a Cell Phone using control array)

More Tutorials Like This one here: Visual Basic Tutorials

and This One Mike's VB Help

[edit] Regular Expression Tester

A small, almost throwaway application that demostrates that useful application do not need to be either large or complicated.

[edit] JArithmetic

[edit] The Language

This sections summarizes the syntax of Visual Basic and describes all the key concepts that of Visual Basic. It also notes some features that exist in other languages but do not exist in Visual basic.

[edit] Coding Standards

Consistent style in coding helps everyone to maintain a program whether the maintainer is the original author or not.

[edit] Selected Functions

Short descriptions of some of the functions that are used in this book.

[edit] Command Reference

This section serves as guide to the commands that can be used in Visual Basic.

[edit] Glossary

Brief explanations of various important, difficult or otherwise interesting words and phrases with links to longer explanations in the body of the text.

[edit] Work in Progress

Various unfinished pages. Please look here for anything obscure or difficult.

[edit] Related Works

Anyone who is serious about the art of programming should know about more than one language and should also be conversant with the foundations of computing. As mentioned in the Idioms chapter you can borrow ideas from other languages even if the language you use appears not to support them. Here is a list of suggested works, some on-line some in print.

Donald Knuth
The Art of Computer Programming. Knuth's masterpiece, let's hope he lives long enough to complete it.
Data Structures
Programs operate on data so knowledge of effective data structures is very important.
Algorithms
Many algorithms were described long before computers were available and many have both naive inefficient forms and sophisticated efficient forms. In many cases this is independent of the implementation language.

[edit] Wikibooks and modules on related subjects

[edit] Books on the same subject in other languages

Dansk
WikiBook
Deutsch
WikiBook
English
Wikipedia
Español
WikiBook (planned)
Français
VBScript WikiBook
Nederlands
VB/VBA WikiBook
Norsk
WikiBook, Wikipedia

[edit] External Links

Link Comment
Visual Basic Tutorial 37 Lessons on VB6 and sample source code
Visual Basic 6 (VB6) tutorials and source code Example VB6 tutorials and source code
VBCorLib Do exceptions in VB6
Visual Basic Tutorials
WinsockVB Everything there is to know about sockets and VB6. Still has active forums. OFF LINE
VBSpeed Objective evidence you can use to help you write faster code
GPWiki Contains more game related Visual Basic tutorials
XtremeVBTalk A VB forum
AllAPI Examples for Windows APIs
Hardcore Visual Basic Bruce McKinney's masterpiece; shows that you can write almost anything in VB6
Planet Source Code Sample Source Code
VBCode.com Free Source Code for VB
A1VBcode.com Another Free Source Code site for VB
VBexemple.com Free examples(source code),Tutorials for VB6

[edit] Authors and Contributors

Details of permissions given to include content from other sites and documents is on the Credits and Permissions page.

Name Comment
Batjew Wrote almost everything to start, getting help now.
EugeneH Corrected mistakes and wrote the rest. User name is now Orage
T94xr Fixed a dead link and added a couple good sites.
kwhitefoot Added: distinction between Type and Class, optimization of programs. Split into separate chapters. Added coding standards, case studies, optimization.
Elliot Spencer Contributed many examples from his web site: http://www.ilook.fsnet.co.uk/index.htm
Aadnk Some sections translated from the Norwegian Visual Basic WikiBook (by Kwhitefoot). Also wrote the Windows Dialogs chapter.
GUI Computing Pty Ltd/Mark Trescowthick Mark Trescowthick kindly gave permission for the [GUI Computing coding standards] and [Australian Visual Developers Forum] to be used, see Credits and Permissions.

[edit] Indexes

There is no index of concepts and things yet because there is no easy way to maintain such a thing. There is however a complete list of all the chapters:

- Chapters

In other languages