Introduction to Visual Basic - Bowling Green State University

Download Report

Transcript Introduction to Visual Basic - Bowling Green State University

Introduction to Visual Basic
laura leventhal and lisa weihl
1
Dinosaurs?
• The COBOL Programmer
•
•
There was once a COBOL programmer in the mid to late 1990s. For
the sake of this story, we'll call him Jack. After years of being
taken for granted and treated as a technological dinosaur by all the
UNIX programmers and Client/Server programmers and web site
developers, Jack was finally getting some respect.
He'd become a private consultant specializing in Year 2000
conversions. He was working short-term assignments for prestige
companies, traveling all over the world on different assignments. He
was working 70 and 80 and even 90 hour weeks, but it was worth it.
2
More COBOL
•
•
•
Several years of this relentless, mind-numbing work had taken its toll
on Jack. He had problems sleeping and began having anxiety dreams
about the Year 2000. It had reached a point where even the thought
of the year 2000 made him nearly violent. He must have suffered
some sort of breakdown, because all he could think about was how he
could avoid the year 2000 and all that came with it.
He was put into his cryogenic receptacle, the technicians set the
revive date, he was given injections to slow his heartbeat to a bare
minimum, and that was that.
The next thing that Jack saw was an enormous and very modern room
filled with excited people. They were all shouting "I can't believe it!"
and "It's a miracle" and "He's alive!". There were cameras (unlike any
he'd ever seen) and equipment that looked like it came out of a
3
science fiction movie.
More COBOL
•
•
•
Someone who was obviously a spokesperson for the group stepped
forward. Jack couldn't contain his enthusiasm. "It is over?" he
asked. "Is 2000 already here? Are all the millennial parties and
promotions and crises all over and done with?"
The spokesman explained that there had been a problem with the
programming of the timer on Jack's cryogenic receptacle, it hadn't
been year 2000 compliant. It was actually eight thousand years
later, not the year 2000. But the spokesman told Jack that he
shouldn't get excited; someone important wanted to speak to him.
Suddenly a wall-sized projection screen displayed the image of a
man that looked very much like Bill Gates. This man was Prime
Minister of Earth. He told Jack not to be upset. That this was a
wonderful time to be alive.
4
More COBOL
• That there was world peace and no more starvation. That
the space program had been reinstated and there were
colonies on the moon and on Mars. That technology had
advanced to such a degree that everyone had virtual reality
interfaces which allowed them to contact anyone else on the
planet, or to watch any entertainment, or to hear any music
recorded anywhere.
• "That sounds terrific," said Jack. "But I'm curious. Why is
everybody so interested in me?"
• "Well," said the Prime Minister. "The year 10000 is just
around the corner, and it says in your files that you know
COBOL".
5
Main Points
•
•
•
•
Tools to build GUI’s
Parts of Visual Basic Programs/Projects
Parts of the VB environment
Basic Language
6
Tools to Build GUI’s
• Need tools that make it easier to design, evaluate
and generate GUI’s
• Contemporary examples:
– Symantic café (generate Java)
– RapidApp (generate C++)
– Python/tk,Tcl/tk (scripting, freeware
languages,available for many platforms, can produce
applications for either desktops or web deliverable)
– Java forte, Motif widget set
7
Tools to build GUI’s
•
•
•
•
Event-driven vs. linear
High-level GUI objects as part of language
Support UI prototyping
Examples
– Visual Basic
– Hypercard/hypertalk
8
Why VB?
• Visual Basic is one of the world’s most
popular programming languages
– encapsulates the complexities of Windows
Application Program Interface (API) into easily
manipulated objects
• VB is similar to the QBasic (the non-visual Basic
language supplied with every version of MS-DOS 5.0 and
above)
– it contains important extensions that make it
event-driven and more object oriented.
9
Developing a UI in VB
• Four general steps
– Design UI (placement of buttons, labels...)
– Create the UI in VB
• place buttons and other control objects in a window
– Set properties of objects
– Write code to control how objects respond to
user events
1
0
Planning Event Procedures - General Steps for
planning your code
• Identify the controls around which the main events fo your
program will take place.
• Determine the specific events you expect to take place
around these controls.
• Write procedures that define your program’s action when
any one of these events take place.
• Note - visual basic has a powerful help facility to help you
along.
1
1
Visual Basic Environment
• The VB environment is shown in Figure 1
of the tutorial. Find
–
–
–
–
–
–
Menubar
Toolbox window
Form window
Project window
Properties window
Explorer window
1
2
VB Environment/Form window
• forms
– windows your application will use. If your
application runs as a standalone application,
users will see the forms as the application.
– place control objects (buttons, text boxes...) on
forms
1
3
VB Environment/Properties
• list of all of the properties that can be set for
an object (eg. size, captions, color...)
• objects with properties are any object on the
form and the form itself.
1
4
Properties - Example
• Suppose that you write
Shirt.color = “Blue”
• In VB, this would mean that you have an
object named Shirt. Shirt has a property,
color. We changed the property to Blue.
– Assumption is that “Blue” is valid value for the
property. Environment will tell you if it is not.
1
5
VBEnvironment/Toolbox
• Toolbox
– shown in Figure 2 of tutorial
– palette of control objects.
– choose objects from the toolbox for your form.
1
6
Toolbox Controls - Text
• Four controls for text:
–
–
–
–
Text box - for user input
Label - for displaying user output
List box - for listing choices
Combo box - pull down list box and text box
1
7
Examples/Toolbox Controls
• Demos
– Text box: change name to txtBox, change text
– Label: change caption to lblBox
– List box and Combo box
• Ways to add elements to list: VB code (additem), in
properties window (property = list)
• Usually initialize at LOAD with additem
– Method: name.additem “value”
1
8
Toolbox Controls - Containers
• Containers are used to house or contain
(like a bucket) a set of controls. There are
two types:
– Picture Box
– Frame
1
9
Toolbox Controls - Buttons
• Buttons are controls that users click to turn
them on or off. Clicking a button evokes an
event.
• Types
–
–
–
–
Option buttons
Check box
Command button
Image box
2
0
VB Environment/Project
Window
• List of all files that are required for a VB
application
• If a program consists of multiple forms,
they are listed in the project window.
• Usage warning - do not simply copy project
in file manager. Elements will be lost.
2
1
Parts of a VB Project/Application
• A project is made up of:
– Forms
• A window that contains visual elements.
– Classes
– Modules
• special window with declarations and programming
code.
– Custom controls
• visual objects like buttons that are add-ons.
2
2
Example 3.4
• In this exercise, I will show a simple
interface that has two command buttons and
a text box to display output
2
3
Methods
• A method is a reserved keyword that acts on
a particular object.
• For example, Clearis a method to clear the
contents of a control, such as a list box or
combo box.
• In VB, there are approximately 30 different
methods.
2
4
Methods - Example
• Syntax
object.method
• Example - clear the contents of the list box
named List1.
– List1.Clear
2
5
An Event
• An event is an action recognized by an
object.
– remember the word action. Action might be a
mouse click or pressing a keyboard key
• While the value of a property and a method
can be specified, an event typically requires
a set of programmed instructions.
2
6
A Statement
• A statement is a reserved word that causes
the VB program to do something
• Examples
– Kill (deletes a file from a disk)
– Option Explicit (forces explicit declaration of
all variables - a good idea!)
2
7
VB Program Instructions
• An instruction is a line of code in a VB
application.
• All instructions begin with a statement or a
method.
• Example
– Form1.Move Left + (Width\10), Top + (Height\10)
– Move is the method, Left, Width, Top and Height are
properties and Form is the object
2
8
Open the Code Window/Writing VB
Instructions
• To open the code window, double-click the active form.
• This will open a window with a template for a subroutine.
By definition, the name of the subroutine is the
combination of the object and the specific event.
• Use the names of the controls to see which events are
defined for which controls (eg. Command buttons have
“click”)
• Example
– Private Sub Form_Load
2
– Private Sub Form_Click
9
Coding Event Procedures for
Controls
• Adding a control to a from and setting the
control properties set the scene for adding a
procedure.
• A control will do nothing in an eventoriented environment unless an event
procedure is attached to it.
• More than one event can be associated with
3
a single control
0
Control Naming Conventions
• The property Name is typically the variable name of the
control.
• There is always a default name assigned to the form or
control.. For example, the first command button added to a
form is named Command1.
• To change the name and follow convention:
– Add a command button. Change the caption property
(ie what the user sees to StartI.
– Change the name property to
3
• See table 3.1 for conventions
1
A Few Coded Statements and
Methods
• See book and tutorial for more complete descriptions
• Dim Statement
– used to declare variables and allocate storage space.
– eg. Dim variablename [As type]
• Private
– All control procedures have the Private keyword
proceding them by default.
– When defining classes in VB, just like in C++ it is
important to determine which data members and
member functions are public and which are private.
3
2
Example 7.7
3
3
Defining Variable and Procedure
Scope
• Variable scope is determined by
– the placement of the variable declaration
– keywords (Dim, Static, Private, Public)
• Procedure scope determined by
– keywords (Private, Public, Static)
3
4
Variable Scope
• Variables declared with Dim are local to the
procedure.
• Declaring variables with Public instead of Dim
(same syntax) makes the variable global.
– In order for variables/constants to be global, they must
be declared in the Declarations section of a standard
module.
– Class data members should be private in general.
3
5
Grand Finale In Class
3
6
Project Management - File Menu
• Open Project, Save Project, Save Project As
– Use these to guarantee that your whole project is saved.
• Add File - opens a dialog box that enables you to add
.FRM (form), .BAS (coded module), and other files.
Important when you use a file built for another project.
• Remove File - removes the file from the current project
that is active.
• Save File
• Save File As - Saves current file to disk.
• Print
• Print Setup
• Make .EXE - creates an executable application.
3
7
Other Stuff
• Details about objects, properties and VB
syntax are in the tutorial
• In the lab, we will demo the maturity
calculator
3
8
3
9