Upload
gabriel-dediu
View
226
Download
0
Embed Size (px)
Citation preview
7/28/2019 Visual Basic Programing
1/48
Visual Basic Programming
An Introduction
7/28/2019 Visual Basic Programing
2/48
Why Visual Basic?
H Programming for the Windows User
Interface is extremely complicated.
H Other Graphical User Interfaces (GUI) areno better.
H Visual Basic provides a convenient method
for building user interfaces.
H Visual Basic can interface with code written
in C, for efficiency.
7/28/2019 Visual Basic Programing
3/48
What Visual Basic is not
H Visual Basic is not, a powerful
programming language that enables you to
do anything you want.H Visual Basic is not, elegant or fast.
H Visual Basic is not, a replacement for C.
H Visual Basic is not, anything like any other
programming language you have ever used.
7/28/2019 Visual Basic Programing
4/48
When You Program in VB:
H You draw pictures of your user interface.
H You draw buttons, text boxes, and other
user-interface items.H You add little snippets of code to handle the
user interaction.
H You add initialization code, usually as thelast step.
H If you like, you can code more complex
functions. (But many do not.)
7/28/2019 Visual Basic Programing
5/48
The Visual Basic Interface
Draw Your
Program
Here!
7/28/2019 Visual Basic Programing
6/48
Drawing The Program
Select A Control From Here
(Click on the appropriate button)
Then Draw the control on the form
7/28/2019 Visual Basic Programing
7/48
Types of Controls
Static Text
Group Box
Check Box
Scroll Bar
Timer
Folder Hierarchy
Circles and StuffPictures
Pictures
Editable Text
Button
Radio ButtonDrop-Down List List
Drive List
File ListLines
Data Base Access
Scroll Bar
And the List Goes On and On ...
7/28/2019 Visual Basic Programing
8/48
A Simple Program
Double-Click to
Add Code
Single-Click toSelect and
Change
Properties
Using controls: Static Text
Editable Text
Buttons
7/28/2019 Visual Basic Programing
9/48
The Properties Window
List of Properties
For Currently Selected
Control
Click on Property, and
Type In New Value, or
Select New Value FromMenu.
7/28/2019 Visual Basic Programing
10/48
Adding CodeControl
Name
External Event
Name
What to Do When It Happens
You must Write
The Body
Yourself
7/28/2019 Visual Basic Programing
11/48
More Complex Controls
H Complex Controls Have:
Action Properties to Execute Commands
Active Properties that Cause Actions WhenValues Are Assigned to Them
Many Types of Events for Program Interaction
H Examples: Spreadsheets
Word Processors
Web Browsers
7/28/2019 Visual Basic Programing
12/48
Using C Code
H Write a DLL in C
H Use the _export Property on Appropriate
Functions
H Write Visual Basic Definitions for each
Function
H Add VB Definitions to The (general)
section of the VB Program
H Use Functions as if they were VB functions
7/28/2019 Visual Basic Programing
13/48
C Definition vs. VB Definition
Declare FunctionHexToLongLib FIRSTONE.DLL
(ByValInStringAs String) As Long
long FAR PASCAL _exportHexToLong(char *Hex)
C:
VB:
Function Name Must Be The Same in Both Declarations.
The Lib keyword Must Give The Name of the Library.
Argument Name in VB is arbitrary.
7/28/2019 Visual Basic Programing
14/48
A (Very Annoying) Problem
H It is sometimes difficult for VB toFIND the
.DLL file.
H If this occurs, copy the .DLL file to theWINDOWS directory.
H Remember to Delete the file when you are
done.
7/28/2019 Visual Basic Programing
15/48
Alternative Methods
H Some Versions of VB do not allow DLL
function definitions in the (general) section
of a form.H To Get Around this Problem, Create a new
Module (File Menu)
H Add the declarations to the (general) sectionof the module
H You can add your own VB functions to the
(general) section of a form or a module.
7/28/2019 Visual Basic Programing
16/48
Syntax Considerations
H All Functions are Global in VB
H Variables are declared using the syntax:
Dim As
Every variable must have a type
Dim A,B,C As will work, but gives
weird resultsH Most Common Types: Integer, String, Long
7/28/2019 Visual Basic Programing
17/48
More VB Syntax
H Use Integers for Booleans
As in C, 0 = False, everything else = True
Symbolic constants True and False may be used
True = -1, False = 0
H Assignments are the same as in C
H The Val function converts strings to integers
H The Format$ function converts integers to
strings
7/28/2019 Visual Basic Programing
18/48
VB Statements
H Assignments are the Same as in C
H Case is not significant
Case will be adjusted for you on keywords
For Variable Names, Case is ignored
H The Usual Operators can be used
AND is the same as both & and && dependingon context
OR = | and ||
NOT = !
7/28/2019 Visual Basic Programing
19/48
VB IF Statements
If Then
Else
EndIf
If Then
EndIf
DONT FORGET THE ENDIF!
Comparators: =,, =, < > (not equal)Connectives: And, Or, Not
7/28/2019 Visual Basic Programing
20/48
VB While Statements
While do
Wend
The VB Manual Recommends a different structure.Use the alternative if you wish.
7/28/2019 Visual Basic Programing
21/48
VB For Statements
For = to
Next
For = to Step
Next
Example:For I = 1 to 10 do
A[I] = A[I] + 1
Next I
7/28/2019 Visual Basic Programing
22/48
VB Arrays
H Indices Always Start With Zero
HDim A[10] As IntegerDeclares 11 elements,
indexed from 0 through 10.H Multi-Dimensional Arrays are Permitted.
H Arrays can be resized at run time (See VB
Help File forReDim)
7/28/2019 Visual Basic Programing
23/48
VB Strings
H Variable Length
H Compare using standard comparators
H Maximum length is about 64Kb
H Minimum length is zero
H Allocated from VB String Space, so may
run out of space even on systems with much
memory.
7/28/2019 Visual Basic Programing
24/48
And in Conclusion ...
GoHave
Fun!
7/28/2019 Visual Basic Programing
25/48
Visual Basic Programming
An Introduction
7/28/2019 Visual Basic Programing
26/48
Why Visual Basic?
H Programming for the Windows User
Interface is extremely complicated.
H Other Graphical User Interfaces (GUI) areno better.
H Visual Basic provides a convenient method
for building user interfaces.H Visual Basic can interface with code written
in C, for efficiency.
7/28/2019 Visual Basic Programing
27/48
What Visual Basic is not
H Visual Basic is not, a powerful
programming language that enables you to
do anything you want.H Visual Basic is not, elegant or fast.
H Visual Basic is not, a replacement for C.
H Visual Basic is not, anything like any otherprogramming language you have ever used.
7/28/2019 Visual Basic Programing
28/48
When You Program in VB:
H You draw pictures of your user interface.
H You draw buttons, text boxes, and other
user-interface items.H You add little snippets of code to handle the
user interaction.
H
You add initialization code, usually as thelast step.
H If you like, you can code more complex
functions. (But many do not.)
7/28/2019 Visual Basic Programing
29/48
The Visual Basic Interface
Draw YourProgram
Here!
7/28/2019 Visual Basic Programing
30/48
Drawing The Program
Select A Control From Here
(Click on the appropriate button)
Then Draw the control on the form
7/28/2019 Visual Basic Programing
31/48
Types of Controls
Static Text
Group Box
Check Box
Scroll Bar
Timer
Folder Hierarchy
Circles and Stuff
Pictures
Pictures
Editable TextEditable Text
Button
Radio ButtonDrop-Down List List
Drive List
File ListLines
Data Base Access
Scroll Bar
And the List Goes On and On
7/28/2019 Visual Basic Programing
32/48
A Simple Program
Double-Click to
Add Code
Single-Click toSelect and
Change
Properties
Using controls: Static Text
Editable Text
Buttons
7/28/2019 Visual Basic Programing
33/48
The Properties Window
List of Properties
For Currently Selected
Control
Click on Property, and
Type In New Value, or
Select New Value FromMenu.
7/28/2019 Visual Basic Programing
34/48
Adding CodeControl
NameExternal Event
Name
What to Do When It Happens
You must Write
The Body
Yourself
7/28/2019 Visual Basic Programing
35/48
More Complex Controls
H Complex Controls Have:
Action Properties to Execute Commands
Active Properties that Cause Actions WhenValues Are Assigned to Them
Many Types of Events for Program Interaction
H
Examples: Spreadsheets
Word Processors
Web Browsers
7/28/2019 Visual Basic Programing
36/48
Using C Code
H Write a DLL in C
H Use the _export Property on Appropriate
FunctionsH Write Visual Basic Definitions for each
Function
H Add VB Definitions to The (general)section of the VB Program
H Use Functions as if they were VB functions
7/28/2019 Visual Basic Programing
37/48
C Definition vs. VB Definition
Declare FunctionHexToLongLib FIRSTONE.DLL
(ByValInStringAs String) As Long
long FAR PASCAL _exportHexToLong(char *Hex)
C:
VB:
Function Name Must Be The Same in Both Declarations.
The Lib keyword Must Give The Name of the Library.
Argument Name in VB is arbitrary.
7/28/2019 Visual Basic Programing
38/48
A (Very Annoying) Problem
H It is sometimes difficult for VB toFIND the
.DLL file.
H If this occurs, copy the .DLL file to theWINDOWS directory.
H Remember to Delete the file when you are
done.
7/28/2019 Visual Basic Programing
39/48
Alternative Methods
H Some Versions of VB do not allow DLL
function definitions in the (general) section
of a form.H To Get Around this Problem, Create a new
Module (File Menu)
H Add the declarations to the (general) sectionof the module
H You can add your own VB functions to the
(general) section of a form or a module
7/28/2019 Visual Basic Programing
40/48
Syntax Considerations
H All Functions are Global in VB
H Variables are declared using the syntax:
Dim As Every variable must have a type
Dim A,B,C As will work, but gives
weird resultsH Most Common Types: Integer, String, Long
7/28/2019 Visual Basic Programing
41/48
More VB Syntax
H Use Integers for Booleans
As in C, 0 = False, everything else = True
Symbolic constants True and False may be used True = -1, False = 0
H Assignments are the same as in C
H The Val function converts strings to integersH The Format$ function converts integers to
strings
7/28/2019 Visual Basic Programing
42/48
VB Statements
H Assignments are the Same as in C
H Case is not significant
Case will be adjusted for you on keywords For Variable Names, Case is ignored
H The Usual Operators can be used
AND is the same as both & and && dependingon context
OR = | and ||
NOT = !
7/28/2019 Visual Basic Programing
43/48
VB IF Statements
If Then
Else
EndIf
If Then
EndIf
DONT FORGET THE ENDIF!
Comparators: =,, =, < > (not equal)
Connectives: And, Or, Not
7/28/2019 Visual Basic Programing
44/48
VB While Statements
While do
Wend
The VB Manual Recommends a different structure.Use the alternative if you wish.
7/28/2019 Visual Basic Programing
45/48
VB For Statements
For = to
Next
For = to Step
Next
Example:
For I = 1 to 10 do
A[I] = A[I] + 1
Next I
7/28/2019 Visual Basic Programing
46/48
VB Arrays
H Indices Always Start With Zero
HDim A[10] As IntegerDeclares 11 elements,
indexed from 0 through 10.H Multi-Dimensional Arrays are Permitted.
H Arrays can be resized at run time (See VB
Help File forReDim)
7/28/2019 Visual Basic Programing
47/48
VB Strings
H Variable Length
H Compare using standard comparators
H Maximum length is about 64KbH Minimum length is zero
H Allocated from VB String Space, so may
run out of space even on systems with muchmemory.
7/28/2019 Visual Basic Programing
48/48
And in Conclusion ...
GoHave
Fun!