introduction to visual basic - program-info.net · 1/19/2015 2 what is visual basic® visual basic...
TRANSCRIPT
1/19/2015
1
VISUAL BASIC®
Introduction to
Visual Basic®
Copyright © 2014 Dan McElroy
Table of Contents
1. What is Visual Basic
2. Visual Basic Controls
3. Developer and Run Modes
4. View Designer, View Code
1/19/2015
2
What is Visual Basic®
Visual Basic (VB) is a programming
language provided as part of Microsoft
Visual Studio for developing application
programs for Microsoft Windows®
Although VB is most often used for
developing Windows applications, it can
also be used for non-Windows projects.
1) What is Visual Basic
Visual Basic Uses Objects
Visual Basic (VB) is an object-based language which gives the programmer to opportunity to do Object Oriented Programming (OOP). Other object-based languages include: C++, Objective-C, Swift, Smalltalk, Java, C#, Perl, Python, Ruby and PHP.
VB provides a collection of objects in its Toolbox that can be placed on a Form. Objects in the Toolbox are called controls. The VB Form provides the layout of what the VB program will look like when it is completed and is run by the user.
Controls 1/3
1/19/2015
3
Visual Basic Controls
This sample program includes the following controls that were selected by the programmer from the Toolbox.
Controls 2/3
Label (Text)
Button
TextBox (User Input)
NOTE: The Form is also an object.
Controls – Properties and Methods Each of the controls from VB’s Toolbox already have Properties (data, attributes) and Methods (code, functions, subroutines, procedures) that will do things to the control or respond to Events that happen to the control.
Most controls include:
Controls 3/3
Properties Methods
Font Click
Font Color Move
Location MouseDown
Size Resize
Text TextChanged
1/19/2015
4
Visual Basic Modes
Visual Basic has two modes of operation:
a) Developer Mode – The VB program is created by designing the Form (what the program looks like when it runs in Windows) and the code to make the program work.
b) Run Mode – Debug, execute and test the program with known values to make sure it works properly. Publish the program for other users to run the program.
Modes
1)
Developer Mode Two parts of the Developer Mode
1) The Design Window - The programmer places controls from the Toolbox on the Form that will be displayed when the program runs.
2) The Code Window – The programmer writes the code that will be executed when the program runs.
Modes – Developer Mode
1/19/2015
5
The Design Window When developing the program, select an control from the Toolbox then use the mouse to draw a rectangular area on the Form to select its size and position.
Modes – Developer Mode – The Design Window 1/4
The Design Window The most common controls used from the Toolbox include:
Label – Used to place text on the form or used to display the results to the user
TextBox – Get input from the user of the program
Button – Make something happen in the program PictureBox – put a picture on the Form
Modes – Developer Mode – The Design Window 2/4
1/19/2015
6
The Design Window
Modes – Developer Mode – The Design Window 3/4
Label
TextBox
Button
Label1.Text
Properties Label1.Location
Label1.Size
Controls are selected from the Toolbox and placed on the form. Each control has a list of properties that can be displayed or modified in the Properties window.
Button
Label
The Properties Window The Properties Window can display either Properties or Event Handlers
Modes – Developer Mode – The Design Window 4/4
The Properties or Event Handlers can either be displayed by Category or Alphabetically. The Alphabetic display is used for all the discussions and videos.
1/19/2015
7
The Code Window The Code Window is the place where the programmer can type the program. The program can consist of:
1) Event Handlers – code that gets executed when something happens to a control, such as a button being clicked.
2) Subroutines and Procedures – extra code created by the programmer to help the program run.
Modes – Developer Mode – The Code Window 1/4
The Code Window
While in the Design Window, double-clicking a control opens the Code Window with an empty Event Handler subroutine.
Modes – Developer Mode – The Code Window 2/4
1/19/2015
8
The Code Window The programmer can enter the code that is to be executed during the Run Mode.
For example, the first Event Handler subroutine in the example below shows what code is to be executed when the “Compute” button is clicked.
Modes – Developer Mode – The Code Window 3/4
The Code Window When entering code, BE VERY CAREFUL that you only enter the program between the Private Sub and the End Sub lines provided by Visual Basic.
If you modify or delete any of the lines provided by VB, you may need to start your project all over from the beginning.
Modes – Developer Mode – The Code Window 4/4
1/19/2015
9
Switching Views
You can easily switch between the Design Window and the Code Window by clicking the tabs at the top of the window. The View menu selection can also be used.
Modes – Developer Mode – Switching Views
Divide and Conquer Medium or large sized programs are usually divided into smaller pieces called; methods, functions or subroutines. Example: A very simple payroll program. The work is subdivided so that each part of the program does only one thing – and does it well.
Modes – Developer Mode – Switching Views
main FormLoad
Read Hours
Compute Pay
Compute Taxes
Print Paycheck
Hours Hours Pay Pay Taxes Net pay
1/19/2015
10
Event Handlers Event handlers are a special form of a subroutine that are activated by an event occurring on a control instead of being called (activated) by the main program. Examples might be a Button is clicked, text is changed in a TextBox, a tick occurred on a timer, etc.
Modes – Developer Mode – Switching Views
main FormLoad
Read Hours
Compute Pay
Compute Taxes
Print Paycheck
Hours Hours Pay Pay Taxes Net pay
Button
Review Paycheck
Click Event
Review
Have Fun With Programming
1/19/2015
11
ACKNOWLEDGEMENTS
IMAGE
DMV image – UCSF
VISUAL BASIC and MICROSOFT WINDOWS
are registered trademarks of Microsoft Corp.