working with the vb ide. running a program u clicking the”start” tool begins the program u the...

39
Working with the VB IDE

Upload: rosemary-simon

Post on 03-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Working with the VB IDE

Running a Program

Clicking the”start” tool begins the program

The “break” tool pauses a program in mid-execution

The “end” tool terminates a program

The Forms Editor

Like the Program Editor, the Forms Editor allows the user to modify the properties of forms, the sort of object that may be placed on a form and the location, properties and size of these objects.

Most of these commands are highly intuitive and do not need much in the way of instruction.

There are, however, a few additional commands in the Forms Editor that are less intuitive.

Grouping Objects

On the form several objects can be selected at the same time. Do this by clicking on the desired objects while holding the Shift Key down.

Here the last command

selected was

“cmdcompute”.

Resizing Objects Once several objects have

been selected they may be “gang” adjusted by using the “format” menu

One of the options permits resizing all selected objects to match the last one selected.

Other Commands

Dragging all tagged objects without disturbing their relative locations.

Changing justification of all text.

Changing Font and size of all text.

Changing the visibility of all tagged objects

Changing the spacing (vertical, horizontal or both) of all tagged objects.

Aligning all tagged objects.

To Learn More...

Use the search term “Format Command” in the help system to get more detailed data.

(note: Format is such a common term that you must limit your search.)

The Program Editor

Visual Basic (VB) supports a text editor (not a word processor) that permits the writing and modification of program code.

The editor is one of two sub-windows that appears in the Workspace. The other is the Forms window.

Many of the commands used in the editor are like those used in word processors, but there are differences.

Entering the Editor The editor is normally

entered by “double clicking” on an “Event Producing Object” in the workspace.

The result is a code window which opens with the “subroutine” describing that event as the focus.

Line Completion Help (1) When a line is being

entered, the editor will make suggestions.

When changing the caption on the button “cmdthingie” the editor presents suggestions when you add the “dot”.

Line Completion Help (2)

I actually typed:

cmdthingie.cap=”start (no capitals, only part of the

word caption, no spaces at all and no closing quote. The represents the <CR>.)

The editor did the rest.

Line Completion Help (3) When I typed a

syntactically incorrect line, the line turned red, and a notice box was posted as soon as I struck the <CR> key or moved the cursor to a different line.

Line Completion Help (4) When I completed the

line by typing the “)” the line turned black again and was correctly spaced.

Note I had typed no spaces.

Line Completion Help (5) If I had typed:

cmdthingee.

The editor would not have provided any help since I had no existing data object named “cmdthingee”.

Line Completion Help (6)

If I had completed the line anyhow and run the program, only then would the system be able to find this more complex syntactical error.

The Helpful Program Editor

The Program Editor will correctly capitalize the names of existing data objects and legally spelled system commands. If you misspell one of them things remain in lower case.

The Program Editor will suggest and complete the spelling of properties of data objects. It will insert appropriate spaces and complete the lines of correctly entered command lines.

The Long and the Short of It

Some lines of code that are too long to fit neatly on a printed line These lines can be entered on multiple lines

Some are so short that they need only a few characters to print One line can hold multiple statements

One statement on multiple lines

Dim strThing As String

strThing = "This is text that " _

& “is too long to fit on one line.“

The underscore “_” character on the first line indicates continuation

the ampersand “&” character concatenates

One statement on multiple lines

Dim sngVolume as Single, _

sngRadius as Single, sngHeight _

as Single

sngVolume = 3.14159 * sngRadius _

^ 2 * sngHeight

(You can place the underscore wherever a space may go, except within a quotation.)

Multiple statements on one line

Dim intRed As Byte, intBlue As Byte

intRed = 3: intBlue = 4

Note the two commands on the second line separated by the colon “:” character. This would work just as if the two commands were put on two separate lines. Use whatever makes the program read more easily.

Option Explicit

To force VB to require all variables be defined, first click on the “option” sub-option of the “tools” command.

Option Explicit

Then be sure to check the box “require variable declaration” on the revealed “option” menu.

Option Explicit

You can, equivalently, simply type option explicit at the top of the code

Some Common Controls

The Command Control

The command control is one of the most important tools in the toolbox

Double clicking on the command button icon will install a command button on the working form

Adding a Command Button

The command buttons are named, by default Command1, Command2, etc.

They may be moved or changed in size using the mouse

Properties of Command Buttons

Each command button has several properties. govern the button’s appearance and behavior

The most important are the (name) and the caption (name) is how a programmer can

refer to the button in VB code caption is the text a user will see

displayed inside the button

Highlighting a Property

When you highlight a property, its definition appears below the properties window

Names and Captions

Clicking on the (name) property lets us name it “cmdstop”, (note the name starts with “cmd”)

Changing the caption to “Stop” gives the user an idea as to the button’s function

Programming Convention

Good practice in Visual Basic suggests that we should name all objects in a program with names that indicate their function These names are invariant during a program’s execution and are

seen only by programmers Command buttons names should begin with the prefix “cmd” Names should begin with the prefix for the given object type, and

should contain only letters and numbers

Captions are used to convey information to the user They can change as the program runs They may contain any letters, numbers, spaces, etc.

Entering Event Code

Double clicking on the “cmdstop” button itself, causes the Code window to open The VB commands the

define the button’s function go here

The “End” command terminates the VB program

The Label Control The label control is an

object with 37 named Properties.

You can adjust its size and position with the mouse.

You can change its (name) its caption and visibility using the Properties window.

Label Control Properties

Using Labels

The label control is usable only for output It cannot accept input from the user

Its size, shape, color, visibility and the form of the information presented can be controlled By editing its properties By writing VB code to change its properties

Labels can be sized and moved using the mouse.

A Program Using a Label

Using a Label

When program execution begins, the system starts as in “A”, when the cmdby button is pressed the system goes to “B”

Repeated pressing of the cmdby button bounces the system between the two states

Programming Convention

Labels should be given a (name) that begins with “lbl”.

The Caption property of a label may be anything that makes sense; one or more words or symbols. Caption is displayed for the user (name) is only accessible to the programmer

The Visible property may be either True or False If false, the label is not displayed

The Text Box Control

Labels are designed to be used for output Command buttons can accept input from

the user, but only of a limited nature The text box control allows the user

input data

Text Box Properties The text property contains the string of

text displayed by the text box Unlike labels, by default this text can be

edited by the user at runtime The proper prefix for text box names is

txt