Chapter 3 Introduction to Visual Basic Environment
description
Transcript of Chapter 3 Introduction to Visual Basic Environment
![Page 1: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/1.jpg)
Chapter 3
Introduction to Visual Basic Environment
![Page 2: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/2.jpg)
Application – another name for a program. Interface – is what appears on the screen when the application is
running. Program Code – is instructions that tells an application’s objects
how to behave. Object Oriented Programming - Uses classes, which are program
code and data, to create objects. Objects- can have visual representation such as a dialog box,
button, Label etc. Event-driven program – when a program runs due to an object
being selected by the user.
A Visual Basic Application
![Page 3: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/3.jpg)
IDE – Integrated Development Environment.◦ Used to create a Visual Basic application
The Visual Basic IDE consists of:◦ Menu Bar◦ Tool Bar◦ Tool Box◦ Project Window◦ Project Explorer Window◦ Properties Window◦ Form Layout Window
The Visual Basic IDE
![Page 4: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/4.jpg)
The Visual Basic IDE
![Page 5: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/5.jpg)
Menu Bar Tool Bar Project Window Project Explorer Window Properties Window Form Layout Window
Visual Basic Environment
![Page 6: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/6.jpg)
Form – is a container object for other objects
Adding Objects to a Form
![Page 7: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/7.jpg)
Objects are added during design time The time during which the applications interface is being
created. Label object is used to display information. Command Button object is something a user can click on.
Objects are added by clicking and moving your mouse over to the screen and dragging it across the form.
Design Time
![Page 8: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/8.jpg)
Properties – defines its appearance, behavior, position, and other attributes.
Each type of objects have many different types of properties.
Object Property Values
![Page 9: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/9.jpg)
Name – identifies an object all names should start with the prefix “lbl”.
Caption – changes the text displayed in the label Font – used to change font style, and size. Alignment – Changes the alignment of text in a label’s
caption (left & right justify and center)
Label Properties
![Page 10: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/10.jpg)
Name – identifies the object. Prefix “cmd”. Cannot be change at runtime.
Caption – Changes the text displayed in the command button.• Example: cmdCancelorDone.Caption = “Done”.
Form Properties Name – identifies an object. Prefix “frm”. Cannot change
during runtime. Caption – changes the text displayed in the title bar.
• Example: frmUserApp.Caption = “My Application”
Command Button Properties
![Page 11: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/11.jpg)
When naming an object, it should start with the appropriate prefix and then be descriptive of the object’s purpose.
Programming Style
![Page 12: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/12.jpg)
Selecting an object is done by simply clicking on it. Resizing by using the drag handle Marquee Selection – Selecting the pointer control and
create a dash box around several objects at one time.
Resizing and Moving Objects
![Page 13: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/13.jpg)
File menu and select the “Save Project” command or clicking on disk icon shortcut key.
First saved – both form and project must be given descriptive names.◦ Save the form using the same name as its Name Property and then
save the project using a descriptive name of the application.
Saving a Project
![Page 14: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/14.jpg)
F5 – start command Run Menu and select run Run icon on the toolbar
Run Time Refers to the time during which application is being
executed. A VB program can be run at any time of its development.
Running a Visual Basic Application
![Page 15: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/15.jpg)
Run Menu – select “End” command. Click the End icon on the Tool Bar.
End Command
![Page 16: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/16.jpg)
Also refer to as Program Contains set of instructions that tells the computer how to
perform specific task. Each line of code is referred to as a statement.
Event Procedures Is a block of code that executes in response to an event. Event (User Event) – is a way in which the user can
interact with an object, such as clicking a button. Specific actions will be taken when the user clicks on the
button.
Application
![Page 17: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/17.jpg)
Container for program code. Code Editor – is the area displaying the form module
Form Module
View Code button View Object button
![Page 18: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/18.jpg)
Display by double-clicking on the form. Display by clicking on the View Code Button in the Project
Explorer. Clicking the button beside the view
code button returns you back to the form.
Code Editor Window
![Page 19: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/19.jpg)
Removes a form from memory and ends the application. Unload statement requires a form name to be unloaded. If the form to be unloaded is the current form, then you
could use Me◦Example:
Unload Me or Unload Form1
Unload Statement
![Page 20: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/20.jpg)
Examples of Unload Statement
![Page 21: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/21.jpg)
Select object name from Object list and then a corresponding event is selected from the Event name.
Adding Event Procedures
![Page 22: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/22.jpg)
Indicates that the procedure cannot be accessed outside of the form module.
Sub declares the procedures End Sub – ends the Sub statement. Body is between Sub and End Sub statements.
Private
![Page 23: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/23.jpg)
The interface and program code of an application are printed by selecting the print command from the file menu
CTRL + P
Removing a Project from the IDE File menu – Select the Remove Project command.
Printing A Project
![Page 24: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/24.jpg)
Assignment – is used to change the value of an object property at run time.
Assignment Statement changes property values using the equal sign (=).
Each Object property can only be assigned a valid property values.
Using Assignment to Change Property Values
![Page 25: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/25.jpg)
Is displayed when typing an object’s name and a dot operator notation mark.
AutoList
![Page 26: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/26.jpg)
Name – Cannot be change during run time by an assignment statement
Caption – Text in double Quote• lblMessage.Caption = “Adios”
Font – has several Subproperties• Size – 0 – 2048
Example: lblMessage.Font.Size = 10• Bold & Italic are True or False
Example: lblMessage.Font.Bold = True• Name – enclosed in double quote
lblMessage.Font.Name = “Arial” Alignment – 0 – left, 1 – right, 2 – center
Valid Values of the label object properties
![Page 27: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/27.jpg)
Event procedures can also be written for form events. The Form_Load event procedure is executed when the
form is loaded into memory.(Application is started or run)
ExamplesPrivate Sub Form_Load() lblSample.Caption = “This text is centered.” lblSample.Alignment = 2End Sub
The Form_Load Event Procedure
![Page 28: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/28.jpg)
Changing object property values in the Form_Load event procedure is alternative to setting property values in the Properties window.
Initializing the form – Setting object properties through the Form_Load event.
Form_Load
![Page 29: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/29.jpg)
Comments – are used to explain and clarify program code for a human reader.
No effect on how the application runs. Single quotation mark (‘) begins a comment Mark code that is ambiguous or misleading. Example: lblSample.Alignment = 0 ‘left justify All programs should state the following:
◦ ‘ Author: Your Name◦ ‘ Date: Current Date◦ ‘ Chapter and Exercises
Commenting Code
![Page 30: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/30.jpg)
File menu – Open Project CTRL + O Open Project Button on the Tool Bar
Opening a Project
![Page 31: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/31.jpg)
Makes programs more interesting Makes it easier to interact with.
Graphics
![Page 32: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/32.jpg)
Located in the Tool Box Used to create Images
Image Control
![Page 33: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/33.jpg)
Name – Use the img prefix when naming and image.
Picture – is used to display a dialog box for selecting the graphic to display in the image area.
Stretch – True or False – True if it has been resized. False otherwise.
Visible – True or False – To display at run time Visible = True. To hide Visible = False.
Properties of an Image
![Page 34: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/34.jpg)
Displays as a box, when added to a form.
Image Object
![Page 35: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/35.jpg)
Go to Property box and select Picture. Click on box on ellipsis.
Stretch should be set to true so image will fit in the image box.
Click event Is when the user clicks on an image object to perform an
action.
Selecting an Image
![Page 36: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/36.jpg)
Built-in arithmetic operators◦ “^” - exponential◦ “*” - multiplication◦ “/” – division◦ “\” – Integer division◦ “Mod” - Modulus◦ “+” - addition◦ “-” - subtraction
Operators and Expressions
![Page 37: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/37.jpg)
Arithmetic operators are used to form expressions. Expression can be anywhere a numeric value is allowed
◦ Example: lblAnswer.Caption = 3.14 * 10^2◦ Expressions are NOT enclosed in quotes.
Expression
![Page 38: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/38.jpg)
Exponentiation Multiplication and Division Left to Right Addition and Subtraction Left to Right You can change order of precedence by using parentheses.
Order of Precedence
![Page 39: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/39.jpg)
Always use parentheses when any ambiguity or maybe questions about the expression.
Programming Style
![Page 40: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/40.jpg)
Visual Basic also has a compiler that translate the program code into separate executable file.
Executable file can be run on any Computer that uses Windows 95 or later.
Visual Basic acts as an interpreter that reads each line of program code as it is entered.
Visual Basic will highlight any errors immediately. Interpreter executes the code line-by-line and displays the
output in the IDE.
Compiler & Interpreter
![Page 41: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/41.jpg)
ALT + Q File menu – Exit
Exiting Visual Basic
![Page 42: Chapter 3 Introduction to Visual Basic Environment](https://reader033.fdocuments.in/reader033/viewer/2022061604/56815de1550346895dcc088f/html5/thumbnails/42.jpg)
Create the program interface first Name all objects before writing and code. Use Object list in Code Editor window to select the object
event procedure.
Visual Basic Programming Guidelines