College of computer technology Information network department Programming with Visual Basic lecture 1

advertisement
College of computer technology
Information network department
Programming with Visual Basic
lecture 1
====================================================
General Introduction
(A Brief on Programming and VB)
Where Programmers Began
The very early computers were not programmed through keyboards
or mouse as we do today. As a matter of fact, the first few computers
did not even have keyboards! The early computers had to be
programmed by a rather weird method which involved using wires
to join one component to another. Thus, the early computers were
programmed by electrical engineers, rather than programmers.
Those “hard-wired” programming methods (as it is called) were
simply too tedious to be reasonably productive. Besides, if the
computer ever had to be used to perform a different task, somebody
had to rewire the computer again (and again and again). So
programmers in those days had to have some good electrical and
engineering experience just to make a computer do something.
In a bid to speed up things, one of the computer experts of that time
developed a smart idea of using the memory to hold both data and
program instructions, instead of using the memory for holding data
only and the wires for programming.
Now, those early computers had memory similar to the way in which
today’s computers have memory. The difference, though, was that the
early memory was miniscule (very small), and even the largest
computers had only a few hundred memory locations for data
storage. Thus, memory was expensive and its supply was short. But
despite the short supply of memory, the idea of using it to hold both
data and the code that instructed the computer was taken seriously
and by putting the computer’s instructions inside the memory along
with data, computer programs became easier to change because
memory could be changed without physically reconnecting wires.
Engineers were, therefore, no longer required to rewire the
computer every time the program needed changing (praise the
Lord!).
With this new system of making computers work, the computer
scientists of those days put a panel of switches on those computers to
get rid of hard-wired programming. Figure 1 next page shows a simple
College of computer technology
Information network department
Programming with Visual Basic
lecture 1
====================================================
representation of those antiquated switches. The programmers would
flip the switches into a series of unique ON and OFF stats, press the
ENTER button, and repeat the process until a series of instructions that
looked like the following “program” appeared in the computer memory:
Which
code:
1 0 1
0 1 1
0 0 0
1 1 1
0 0 1
1 1 0
0 0 1
would correspond to the binary Status
of
switchboard
buttons
representing a program:
1 0 0 0 1
ON OFF ON ON OFF OFF OFF ON
0 1 1 0 0
OFF ON ON OFF ON ON OFF OFF
1 1 0 1 0
OFF OFF OFF ON ON OFF ON OFF
1 0 0 0 1
ON ON ON ON OFF OFF OFF ON
0 0 1 1 0
OFF OFF ON OFF OFF ON ON OFF
1 1 0 0 1
ON ON OFF ON ON OFF OFF ON
0 1 1 1 0
OFF OFF ON OFF ON ON ON OFF
College of computer technology
Information network department
Programming with Visual Basic
lecture 1
====================================================
Although this ON and OFF program is virtually indecipherable by
humans, each combination of ON and OFF switches represents a
single instruction which the computer could understand perfectly. By
combining several instructions that operated on data located
elsewhere in the machine’s memory, a complicated program made of
30 instructions might really not do anything more than just add two
numbers! Despite the difficulties involved however, this system of
programming proved to be very fast in calculations and the military
of the US and other countries immediately begun to use computers
for trajectories and other calculations.
The world of computers began moving forward with the switch panel.
More memory was added, and the programs got more powerful.
Sometime in the late 1940s I think, somebody got the bright idea of
replacing the switchboard panel with a typewriter-like keyboard, so that
instead of assigning ON and OFF combinations to mean ADD and STORE
for example, programmers could actually type the words ADD and
STORE on the keyboard. The computer would analyse the instruction,
look up the ON and OFF combinations needed, and set the memory
switches internally. That was machine language in short.
But now machine language is dead; long live high-level language
<grin>! Luckily, you don’t have to write programs in the ON and OFF
machine language anymore. The computer programming languages
of today are much closer to human language than ON and OFF
switches could ever hope to be. Even so however, today’s most
powerful computers programmed with advanced programming tools
such as Visual Basic, Java, Python or C++ still recognize only machine
language (so, machine language isn’t actually dead yet!). Human
beings don’t like machine language because it is too difficult to use.
But computers don’t like anything else other than machine language.
The job of all programming languages, then, is to take the source code
that you type in a language you understand and convert it to machine
language so that the computer hardware can understand and execute
the instructions in your source code.
College of computer technology
Information network department
Programming with Visual Basic
lecture 1
====================================================
Programming Languages Improved
Once programmers got hold of keyboards, there was no stopping
them. The languages grew from the primitive ON and OFF system to
higher-level languages that read more like spoken text. Such
languages include FORTRAN, COBOL, and the Beginner’s All-purpose
Symbolic Instruction Code — more commonly known as BASIC.
Although languages like FORTRAN and COBOL may look cryptic to
you if you can set your eyes on them now, the more textual approach
to programming offered by those languages, as opposed to ON and
OFF switches, opened doors for more people to become
programmers. With languages like BASIC, programmers could also
write programs that had a less rigid style than those before. The
software industry blossomed in the 1950s, and programs went from
simple calculating tools to complete business and scientific
applications.
Well into the 1980s, the rate at which the number of programmers
grew remained high. Despite all new programmers however, the
programming tools themselves really did not advance much. Many
people developed new programming languages, but despite their
“new and improved” claims, most of the languages could not make
programming much easier. Improvements did not really occur until
hardware breakthroughs occurred: NASA’s space efforts led to the
development of the microchip, which led to the birth of
microcomputers and efforts were made to improve the friendliness
of programming languages like BASIC. New versions of BASIC
enabled programmers to write more powerful programs with less
effort.
Using common English to perform processor tasks, BASIC quickly
became popular, although it was disliked back then by programmers
of more “low-level” languages such as Assembly Language and
FORTRAN. In 1985, Microsoft released their own version of BASIC,
called QBasic, with their MS-DOS 5.0 operating system. Since then,
nearly every PC user owns their own copy of QBasic, making it a
widely known language.
QBasic is a very simple language to pick up; and yet it can accomplish a
great deal. Granted you will probably never write COMMAND.COM or
WordPerfect with QBasic, but it has its strong points as a simple
College of computer technology
Information network department
Programming with Visual Basic
lecture 1
====================================================
programming language. One of them is to introduce people to
programming without having to worry much
about the internal workings of the computer. It is quite easy to use in
creating robust games, business applications, simple databases, and
graphics.
The BASIC programming language was originally invented to make
programming easier for engineers who needed specialized programs
but weren’t interested in the “mundaneness” of programming in
FORTRAN, COBOL and other programming languages of those olden
days. Since then, BASIC has grown to become a language of fun and
(it is now widespread) — that’s why it is an extremely popular
language with hundreds of different versions worldwide. Believe me,
the language has developed so well that there is not a single task that
a C or Java programmer can accomplish that cannot also be done in
one or more dialects of BASIC — and usually in half the time!
Enter Visual Basic!
Visual Basic (or “VB”, if you like) is a descendant of QBasic. Microsoft
designed and developed VB to help programmers take advantage of
the enormous powers of the Windows OS; in such a way that the
programmer can develop programs that can do complex tasks; yet
without [the programmer] having to code everything from scratch as
was the case with QBasic in DOS.
VB is inherently object-oriented. Most of its operations involve
manipulating objects and widgets of all sorts. It certainly is a
powerful object-oriented programming language. In VB, the
programs or applications that you develop are called projects.
Although it was derived from QBasic, VB programs will run in
Windows only, not in DOS as is the case with QBasic.
VB supports event-driven programming, meaning that the
programs you write don’t have to follow any fixed sequence of
execution but rather, execute according to whoever events are fired.
VB programs can respond to events in any order without any
problems. The objects that are manipulated by VB are also known as
controls. A command button and a text box are examples of
controls. Some special controls can contain or serve as placeholders
for other controls. Such special controls are called containers. A
College of computer technology
Information network department
Programming with Visual Basic
lecture 1
====================================================
frame, a form, and a picture box are examples of containers in Visual
Basic.
All controls in VB respond to some form of events. When you click a
command button or double click an icon for example, an event might
be triggered. Clicking and double clicking are examples of mouse
events. VB also supports other events, such as keyboard events
(KeyPressed, KeyDown, KeyUp, and so on).
Methods are those actions that can be carried out by objects. For
example, most objects can move, they can be cleared, and items can
be added to or removed from them. Those are examples of methods
that are applicable to objects.
Finally, let me add that VB is a strong, modular programming language.
The codes that handle the events behind the methods of every control
you use in a program are written in discrete subroutines and functions
known as event handlers. Well, I wish you good luck in your quest to
learn and master this wonderful programming language, though I must
say that you need to rely more on your willingness to learn rather than
on luck to achieve great things in programming. Be eager to try out new
things with the simple examples given in this manual. If you want to
make remarkable progress in the programming world, you must try and
experiment at every opportunity and break new grounds for yourself.
Trust me, that is the only way you could one day become a guru.
Download