View
215
Download
0
Tags:
Embed Size (px)
Citation preview
ITP 150 - Lecturer: A. Borquez 2
Review: Controls Propeties Events Methods Procedures Functions
BackStyle FillColorBorderStyle FillStyleCaption NameEnabled Visible
Move ClearSetFocus AddItem
Activate DragOver LoadChange GotFocus LostFocusClick KeyDown MouseDownDblClick KeyPress MouseMoveDragDrop KeyUpMouseUp
ITP 150 - Lecturer: A. Borquez 3
Controls for Displaying andEntering Text
To provide this feature Use this control Text that can be edited by the user, for example Text box
an order entry field or a password box
Text that is displayed only, for example to identify Label
a field on a form or display instructions to the user
Controls That Present Choices to Users
To provide this feature Use this control A small set of choices from which a user can choose one or Option buttons
more options. Check boxes A small set of options from which (use frames if
a user can choose just one. additional groups are needed)
A scrollable list of choices from which the user can choose. List box
A scrollable list of choices along with a text edit field. Combo box
The user can either choose from the list or type a choice
in the edit field.
Controls That Display Pictures and Graphics
To provide this feature Use this control A container for other controls. Picture box
Printing or graphics methods. Picture box
Displaying a picture. Image control or picture box
Displaying a simple graphical element Shape or line control
ITP 150 - Lecturer: A. Borquez 6
Managing Forms
To perform this task Use this method or statementLoad a form into memory,but do not display it
Load statement, or you can reference aproperty or control on the form
Load and display a form Show methodDisplay a loaded form Show methodHide a form from view Hide methodHide a form from view andunload from memory
Unload statement
ITP 150 - Lecturer: A. Borquez 7
Variables
In Visual Basic, you use variables to temporarily store values during the execution of an application
Variables have a name (the word you use to refer to the value the variable contains) and a data type (which determines the kind of data the variable can store)
To declare a variable is to tell the program about it in advance. You declare a variable with the Dim statement, supplying a name for the variable:
Dim variablename [As type]
ITP 150 - Lecturer: A. Borquez 8
Variable rules…...
Must begin with a letter.
Can't contain an embedded period or embedded type-declaration character.
Must not exceed 255 characters.
Must be unique within the same scope, which is the range from which the variable can be referenced — a procedure, a form, and so on.
ITP 150 - Lecturer: A. Borquez 9
Visual Basic Variables
Variables: temporarily store values during the
execution of an application. Variables have a:
name (the word you use to refer to the value the
variable contains) and a
data type (which determines the kind of data the variable
can store).
Variables can be used in many ways, including:
As a counter that stores the number of times a procedure or code
block executes.
As a temporary storage for property values.
As a place to hold a value returned from a function.
As a place to store directory or file names.
ITP 150 - Lecturer: A. Borquez 10
Other Variable issues to think about….
Implicit vs. Explicit Declarations Declaring a variable in the Declarations section of a form, standard,
or class module, rather than within a procedure, makes the variable available to all the procedures in the module.
Declaring a variable using the Public keyword makes it available throughout your application.
Declaring a local variable using the Static keyword preserves its value even when a procedure ends.
ITP 150 - Lecturer: A. Borquez 11
Variable Data Types
Data type Storage size
Byte 1 byte
Boolean 2 bytes
Integer 2 bytes
Long (long integer) 4 bytes
Single (single floating-point) 4 bytes
Double(double floating-point) 8 bytes
Currency (scaled integer) 8 bytes
Date 8 bytes
Object 4 bytes + size of object
String (variable-length) 10 bytes + string length
String (fixed-length) length of string
Variant 16 bytes
Data type Storage size
Byte 1 byte
Boolean 2 bytes
Integer 2 bytes
Long (long integer) 4 bytes
Single (single floating-point) 4 bytes
Double(double floating-point) 8 bytes
Currency (scaled integer) 8 bytes
Date 8 bytes
Object 4 bytes + size of object
String (variable-length) 10 bytes + string length
String (fixed-length) length of string
Variant 16 bytes
ITP 150 - Lecturer: A. Borquez 12
Variable Data Types Naming Conventions
Data type Prefix
Byte bytBoolean blnInteger intLong lngSingle sngDouble dblCurrency curDate dtmObject objString strVariant var
Data type Prefix
Byte bytBoolean blnInteger intLong lngSingle sngDouble dblCurrency curDate dtmObject objString strVariant var
Understanding the Scope of Variables
Scope Private Public
Procedure-level Variables are private to Not applicable. You
the procedure in which cannot declare public
they appear. variables within a
procedure.
Module-level Variables are private to Variables are available
the module in which to all modules.
they appear.
ITP 150 - Lecturer: A. Borquez 14
Introduction to Procedures
There are two major benefits of programming with procedures:
Procedures allow you to break your programs into discrete logical units, each of which you can debug more easily than an entire program without procedures.
Procedures used in one program can act as building blocks for other programs, usually with little or no modification.
ITP 150 - Lecturer: A. Borquez 15
Working with Date and Time
Dim dtNewDate As DatedtNewDate = dtDate + 5
MsgBox "The current date and time is " & Now()MsgBox "The date is " & Date()MsgBox "The time is " & Time()
Now, Date, and Time functions
ITP 150 - Lecturer: A. Borquez 16
VB Date/Time Functions
Function Example Value displayed
Year() Year(Now) 1999Month() Month(Now) 2Day() Day(Now) 22Weekday() Weekday(Now) 7Hour() Hour(Now) 11Minute() Minute(Now) 38Second() Second(Now) 9
The DateDiff Function
The DateDiff function returns the number of time intervals between two dates.
DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])
Dim dtTheDate As DateDim strMsg As String
dtTheDate = InputBox("Enter a date")strMsg = "Days from today: " & DateDiff("d", Now, dtTheDate)MsgBox strMsg
VB Format Functionthe Format function accepts a numeric value and converts it to a string
Format syntax Result
Format(Now, “m/d/yy”) 1/27/99Format(Now, “dddd, mmmm dd, yyyy”) Wednesday, January 27, 1999Format(Now, “d-mmm”) 27-JanFormat(Now, “mmmm-yy”) January-99Format(Now, “hh:mm AM/PM”) 07:18 AMFormat(Now, “h:mm:ss a/p”) 7:18:00 aFormat(Now, “d-mmmm h:mm”) 3-January 7:18
• txtDate.Text = Format(Now, "Long Date")
• txtCost.Text = Format(1000, "Currency")
Using Named Formats
ITP 150 - Lecturer: A. Borquez 19
Sub Procedures vs. Function Procedures
Sub procedures do not return a value.
Function procedures return a value.
Sub procedures do not return a value.
Function procedures return a value.
ITP 150 - Lecturer: A. Borquez 20
A Closer Look at Sub_Procedures...
• A Sub procedure is a block of code that is executed in response to an event.
• By breaking the code in a module into Sub procedures, it becomes much easier to find or modify the code in your application.
• A Sub procedure is a block of code that is executed in response to an event.
• By breaking the code in a module into Sub procedures, it becomes much easier to find or modify the code in your application.
[Private|Public][Static]Sub procedurename (arguments)statements
• Each time the procedure is called, the statements between Sub and
• End Sub are executed.
• Sub procedures can be placed in standard modules, class modules, and form modules.
• Sub procedures are by default Public in all modules
• Each time the procedure is called, the statements between Sub and
• End Sub are executed.
• Sub procedures can be placed in standard modules, class modules, and form modules.
• Sub procedures are by default Public in all modules
A Closer Look at Functions….
•Visual Basic includes built-in, or intrinsic functions, like Sqr, Val, or Chr.
•You can use the Function statement to write your own Function procedures.
•Like a Sub procedure, a Function procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments.
•Function procedures have data types, just as variables do.
•You return a value by assigning it to the procedurename itself.
[Private|Public][Static]Function procedurename (arguments) [As type]statements