Download advanced lessons in VB
Download advanced lessons in VB
What is Visual Basic?
Visual Basic is a programming language that allows you to create your
own software with the look of Windows programmes that you are familiar with. It
is the most common and easiest to learn of modern computer languages
Visual Basic is also able to add additional features to Microsoft
Office applications. When you write a
Macro in Excel it is actually created for you using Visual Basic.
The word visual in Visual Basic means graphical,
easily seen such as pictures and symbols that you can click on and drag.
BASIC is short for Beginners All purpose Symbolic Instruction Code. BASIC was originally developed to make it possible for home
users to program PCs.
BASIC is considered to be a high
level language because BASIC code uses a simple set of English words and
symbols to give instructions to the computer, not the low level instructions
that deal with the zeros and ones that the computer understands.
Visual Basic is also an Event
Driven language. Event driven
languages allow you to choose what you do rather than have to respond to a
series of questions the computer dictates. Event driven languages such as
Visual Basic are called Object Oriented
Languages or OOLs.
There are two parts to creating Visual Basic applications.
1. You firstly place the objects (buttons etc) on a
form then
2. Secondly write the code that is hidden behind
these buttons. The code makes the buttons respond in the way you want them to.
Variables
Variables are placeholders used to store values; they have names and
data types. The data type of a variable determines how the bits representing
those values are stored in the computer's memory. When you declare a variable,
you can also supply a data type for it. All variables have a data type that
determines what kind of data they can store.
You can think of variables as containers, and you choose to put data in
the most suitable container. You would not put a small object in a large box or
try to stuff a large object into a small box.
The following variables are available in visual basic.
Variable Data Types
Data type
|
Prefix
|
Example
|
What
the data is used to hold
|
Bytes
|
Boolean
|
bln
|
blnWillgo
|
True
or False
|
|
Byte
|
byt
|
bytRasterData
|
Whole
numbers from 0–255
|
1
|
Currency
|
cur
|
curMoneyspent
|
Money
values, has a fixed decimal point
|
|
Date
(Time)
|
dtm
|
dtmStartwork
|
Date
|
|
Double
|
dbl
|
dblTolerance
|
Large
decimal numbers more accurately
|
8
|
Integer
|
int
|
intQuantity
|
Smaller
whole numbers –32,768 to 32,767,
|
2
|
Long
|
lng
|
lngDistance
|
Whole
numbers that are very large
|
4
|
Single
|
sng
|
sngAverage
|
Decimal
number
|
4
|
String
|
str
|
strFName
|
Any
collection of keyboard characters but usually words
|
|
Variant
|
vnt
|
vntCheckSum
|
Stores
all the above data types but is slow and wasteful of memory
|
16
|
Here is how you would declare a variable
Dim
curMoneyspent As Currency
The cur is the abbreviation
for currency. This is a naming convention that helps you by letting you know
the type of data that is attached to a particular variable.
What are Labels?
Programmers use labels to
place titles around the form and to label text boxes so users know what to type
into the text box. When you use the Label control, your code can change the
label's text so that different messages can appear when needed.
When you place labels on a form, you'll almost always set the Label
control's Name property and type a
new Caption value. In addition,
you'll want to change the Font property
and possibly the label's color and
style.
Below are the most common Label control properties that you'll set as
you work with the Label control.
Common label properties.
Property
|
Description
|
Alignment
|
Determines whether the
label's caption appears left-justified, centered, or right-justified within
the label's boundaries.
|
AutoSize
|
Enlarges the label's size
properties, when True, if you assign a caption that is too large to fit in
the current label's boundaries at runtime.
|
BackColor
|
Specifies the label's
background color. Click the BackColor's palette down arrow to see a list of
colors and click Categorized to see a list of common Windows control colors.
|
BackStyle
|
Determines whether the
background shows through the label or if the label covers up its background
text, graphics, and color.
|
BorderStyle
|
Determines whether a
single-line border appears around the label.
|
Caption
|
Holds the text that
appears on the label.
|
Enabled
|
Determines whether the
label is active. Often, you'll change the Enabled property at runtime with
code when a label is no longer needed.
|
Font
|
Produces a Font dialog box
in which you can set the caption's font name, style, and size.
|
ForeColor
|
Holds the color of the
label's text.
|
Height
|
Holds the height of the
label's outline in twips.
|
Left
|
Holds the number of twips
from the label's left edge to the Form window's left edge.
|
MousePointer
|
Determines the shape of
the mouse cursor when the user moves the mouse over the label.
|
TabIndex
|
Specifies the order of the
label in the focus order. Although the label cannot receive the direct focus,
the label can be part of the focus order.
|
ToolTipText
|
Holds the text that
appears as a tooltip at runtime.
|
Top
|
Holds the number of twips
from the label's top edge to the Form window's top edge.
|
Visible
|
Determines whether the
label appears or is hidden from the user.
|
Width
|
Holds the width of the label
in twips.
|
WordWrap
|
Determines whether the
label expands to fit whatever text appears in the caption.
|
What are Text Boxes?
Text boxes accept user
input. Although several other controls accept user input, text boxes are
perhaps the easiest to set up and respond to. In addition, a text box is simple
to use, and people see text boxes on Windows forms all the time.
The table below lists the common properties associated with text boxes.
By familiarising yourself with the properties now, you will be able to more
quickly produce applications as you learn more about Visual Basic.
Common text box properties
Property
|
Description
|
Alignment
|
Determines whether the
text box's text appears left-justified, centered, or right-justified within
the text box's boundaries.
|
BackColor
|
Specifies the text box's
background colour. Click the BackColor property's palette down arrow to see a
list of colours and click Categorized to see a list of common Windows control
colours.
|
BorderStyle
|
Determines whether a
single-line border appears around the text box.
|
Enabled
|
Determines whether the
text box is active. Often, you'll change the Enabled property at runtime with
code when a text box is no longer needed.
|
Font
|
Produces a Font dialog box
in which you can set the Text property's font name, style, and size.
|
ForeColor
|
Holds the color of the
text box's text.
|
Height
|
Holds the height of the
text box's outline in twips.
|
Left
|
Holds the number of twips
from the text box's left edge to the Form window's left edge.
|
Locked
|
Determines whether the
user can edit the text inside the text box that appears.
|
MaxLength
|
Specifies the number of
characters the user can type into the text box.
|
MousePointer
|
Determines the shape of
the mouse cursor when the user moves the mouse over the text box.
|
MultiLine
|
Lets the text box hold
multiple lines of text or sets the text box to hold only a single line of
text. Add scrollbars if you wish to put text in a multiline text box so your
users can scroll through the text.
|
PasswordChar
|
Determines the character
that appears in the text box when the user enters a password (keeps prying
eyes from knowing what the user enters into a text box).
|
ScrollBars
|
Determines whether
scrollbars appear on the edges of a multiline text box.
|
TabIndex
|
Specifies the order of the
text box in the focus order.
|
TabStop
|
Determines whether the
text box can receive the focus.
|
Text
|
Holds the value of the
text inside the text box. The Text property changes at runtime as the user
types text into the text box. If you set an initial Text property value, that
value becomes the default value that appears in the text box when the user
first sees the text box.
|
ToolTipText
|
Holds the text that
appears as a tooltip at runtime.
|
Top
|
Holds the number of twips
from the text box's top edge to the Form window's top edge.
|
Visible
|
Determines whether the
text box appears or is hidden from the user.
|
Width
|
Holds the width of the
text box in twips.
|
What are Form Properties?
Forms have properties that you can and should set when you create an
application. Being the background of your application, the form's properties
help set the stage for the rest of the project. The form supports more property
values than the other controls described here, but the list below lists only
the most common properties that you'll need.
New Term: Pixel stands for picture element and represents the
smallest addressable graphic dot on your monitor.
Common form properties.
Property
|
Description
|
BackColor
|
Specifies the form's
background color. Click the BackColor's palette down arrow to see a list of
colors and click Categorized to see a list of common Windows control colors.
|
BorderStyle
|
Determines how the Form
window appears. The BorderStyle property specifies whether the user can
resize the form and also determines the kind of form you wish to display.
|
Caption
|
Displays text on the
form's title bar at runtime.
|
ControlBox
|
Determines whether the
form appears with the Control menu icon. The Control menu appears when your
application's user clicks the Control menu icon.
|
Enabled
|
Determines whether the
form is active. Often, you'll change the Enabled property at runtime with
code when a form is no longer needed. Generally, only multiform applications,
such as MDI applications, need to modify a form's Enabled property.
|
Font
|
Produces a Font dialog box
in which you can set the text's font name, style, and size.
|
ForeColor
|
Holds the color of the
form's text.
|
Height
|
Holds the height of the
form's outline in twips.
|
Icon
|
Describes the icon graphic
image displayed on the taskbar when the user minimizes the form.
|
Left
|
Holds the number of twips
from the form's left edge to the screen's left edge.
|
MaxButton
|
Specifies whether a
maximize window button appears on the form.
|
MinButton
|
Specifies whether a
minimize window button appears on the form.
|
MousePointer
|
Determines the shape of
the mouse cursor when the user moves the mouse over the form.
|
Moveable
|
Specifies whether the user
can move the form at runtime.
|
Picture
|
Determines a graphic image
that appears on the form's background at runtime.
|
ScaleMode
|
Determines whether the
form's measurements appear in twips, pixels (the smallest graphic dot image
possible), inches, centimeters, or other measurements.
|
ShowInTask
bar
|
Determines whether the
form appears on the Windows taskbar.
|
StartUpPosition
|
Determines the state
(centered or default) of the form at application startup.
|
Top
|
Holds the number of twips
from the form's top edge to the Form window's top edge.
|
Visible
|
Determines whether the
form appears or is hidden from the user.
|
Width
|
Holds the width of the
form in twips.
|
WindowState
|
Determines the initial
state (minimized, maximized, or normal) in which the window appears at
runtime...........
|
Download advanced lessons in VB
0 commentaires: