Illuminating Computer Science CCIT 4-6Sep
Introduction to Programming Concepts Visual Basic: an Introduction Illuminating Computer Science CCIT 4-6Sep
Illuminating Computer Science CCIT 4-6Sep The audience should be familiar with the Visual Basic Environment. The audience should be able to build simple Visual Basic Applications. The audience should learn the precedence rules for writing equations. The audience should be able to write programs using control structures
Illuminating Computer Science CCIT 4-6Sep Performing a Task on the Computer Determine Output Identify Input Determine process necessary to turn given Input into desired Output
Illuminating Computer Science CCIT 4-6Sep Problem-solving approach like algebra class How fast is a car traveling if it goes 50 miles in 2 hours? Output: a number giving the rate of speed in Km per hour Input: the distance and time the car has traveled Process: rate = distance/time
Illuminating Computer Science CCIT 4-6Sep Pictorial representation of the problem solving process
Illuminating Computer Science CCIT 4-6Sep Program Planning Tips Always have a plan before trying to write a program The more complicated the problem, the more complex the plan must be Planning and testing before coding saves time coding
Illuminating Computer Science CCIT 4-6Sep Simplified Program development cycle 1. Analyze: Define the problem. 2. Design: Plan the solution to the problem. 3. Choose the interface: Select the objects (text boxes, buttons, etc.).
Illuminating Computer Science CCIT 4-6Sep Program development cycle continued 4. Code: Translate the algorithm into a programming language. Try to avoid jumping straight to this step, which can result in a sloppy solution! 5. Test and debug: Locate and remove any errors in the program. 6. Complete the documentation: Organize all the material that describes the program.
Illuminating Computer Science CCIT 4-6Sep What is Visual Basic? High Level 4th Generation Programming Language Object and Event Driven Visual - Windows Based Integrated Development Environment or IDE
Illuminating Computer Science CCIT 4-6Sep What is VB Used For? Most Popular Programming Language Stand alone programs Applets that can be run on many different kinds of computers Customized specialized Applications –E- Commerce Web based shopping forms and inquiries User friendly Interface to applications
Illuminating Computer Science CCIT 4-6Sep Used by a computer equipment retail outlet to record data regarding customer orders. Example Order Entry Screen Allows user to use a mouse to click on boxes for text entry. Also enables user to click on buttons to initiate processing steps.
Illuminating Computer Science CCIT 4-6Sep Generations of Computer Languages 1st - Machine language nd - Procedure-oriented languages –FORTRAN for scientists and engineers –COBOL for business applications –C for UNIX operating systems 3rd - Object-oriented languages 4th - Event-driven languages - i.e.VB 5th - Natural languages i.e. English
Illuminating Computer Science CCIT 4-6Sep Compiler Vs Interpreter Higher Languages are translated to Machine Language by: –Interpreter Translates instructions to machine code line-by-line. BASIC, Quick Basic, Visual Basic –Compiler Translates the entire program to machine code before running it. Fortran, C, C++, Visual Basic is also a compiler
Illuminating Computer Science CCIT 4-6Sep What are the Objects in VB? Pre-programmed Code for: –Command Buttons –Labels –Pictures –Text Boxes Have both data and procedures wrapped together
Illuminating Computer Science CCIT 4-6Sep What are the Events? –Visual Basic “watches” what the user does –A user activity is called an event –Events include mouse clicks and moves, menu selections, button presses, etc. –Sections of code are only executed in response to an event
Illuminating Computer Science CCIT 4-6Sep What is the Integrated Development Environment (IDE)?
Illuminating Computer Science CCIT 4-6Sep Visual Basic Environment GUI (Graphical User Interface) - forms and windows that the user sees Property - a characteristic or attribute of an object such as color and size Event - a user action such as clicking a button Code Editor window - editor specially designed to help you when writing code
Illuminating Computer Science CCIT 4-6Sep Visual Basic Environment Components of the VB design environment (Form view)
Illuminating Computer Science CCIT 4-6Sep Visual Basic Environment Components of the VB design environment (Code view)
Illuminating Computer Science CCIT 4-6Sep The Code Editor Window Object List Box Procedure List Box Auto List Members Full Module View button
Illuminating Computer Science CCIT 4-6Sep The Code Editor Window Help window Error message box Syntax error in Red Procedure view button
Illuminating Computer Science CCIT 4-6Sep VB Environment: Project Window The Project Window can be used to navigate between the code and form window for each form / window a project has
Illuminating Computer Science CCIT 4-6Sep VB Environment: Menu Bar The Menu Bar consists of 3 elements the –Title Bar, which holds the name of application, default if Microsoft Visual Basic [design] –Menu Bar, this is the link to Visual Basics menu facilities, each menu option drops down into sub-menus –Toolbar, contains icons which give access to the more commonly used commands (which are also available through the menu bar) Title barMenu BarToolbar
Illuminating Computer Science CCIT 4-6Sep VB Environment: Properties Window Used to set how a control looks and behaves Holds its default values
Illuminating Computer Science CCIT 4-6Sep VB Environment: Control Toolbox Pointer Label Group Frame Check Box Combo Box Horizontal Scroll Bar Timer Directory List Box Shape Image OLE Picture Text Box Command Button Option/Radio Button List Box Vertical Scroll Bar Drive List Box File List Box Line Data Control
Illuminating Computer Science CCIT 4-6Sep VB Environment: Context Sensitive Help Context sensitive help is very useful Select the item control that you want help on Press the F1 key F1
Illuminating Computer Science CCIT 4-6Sep The File Menu Project commands Make executable commands Most recent project list Exit command
Illuminating Computer Science CCIT 4-6Sep The File Menu New Project Open Project Add Project Remove Project Save Project Save Project As Save Component Save Component As Print Print Setup
Illuminating Computer Science CCIT 4-6Sep The File Menu Template - particular pattern for a project or a project element; start a project from a template saves time Existing tab - displays VB projects Recent tab - displays most recently accessed projects Project Groups - a collection of several projects
Illuminating Computer Science CCIT 4-6Sep Building Your First Application There are three primary steps in building a Visual Project: –Place (or draw) controls on the form. –Assign properties to the controls. –Write event procedures for the controls.
Illuminating Computer Science CCIT 4-6Sep Project Files vbp extension = Visual Basic Project – file that tracks all components frm extension = Form file –separate file for each form frx extension = Binary form file –cannot be read by humans vbw extension = Visual Basic workspace
Illuminating Computer Science CCIT 4-6Sep Starting Visual Basic
Illuminating Computer Science CCIT 4-6Sep Choosing Project Type
Illuminating Computer Science CCIT 4-6Sep Placing Controls on the Form: Image
Illuminating Computer Science CCIT 4-6Sep Assigning Properties to Controls
Illuminating Computer Science CCIT 4-6Sep Adding an Image
Illuminating Computer Science CCIT 4-6Sep Browse for an Image
Illuminating Computer Science CCIT 4-6Sep Image Displayed
Illuminating Computer Science CCIT 4-6Sep Buttons and Text Boxes
Illuminating Computer Science CCIT 4-6Sep Changing the Form Title
Illuminating Computer Science CCIT 4-6Sep Writing the Code (Events)
Illuminating Computer Science CCIT 4-6Sep Writing the Code (Events)
Illuminating Computer Science CCIT 4-6Sep Running the Application
Illuminating Computer Science CCIT 4-6Sep Running the Application
Illuminating Computer Science CCIT 4-6Sep More Complex Structures
Illuminating Computer Science CCIT 4-6Sep Syntax Boxes Syntax for a Visual Basic statement is shown in a syntax box Reserved words are shown in bold Programmer named words are shown in italics
Illuminating Computer Science CCIT 4-6Sep The Sub Statement Private Sub controlname_eventname( ) Statements End Sub Where Private is the default procedure type Sub indicates beginning of procedure controlname is name of associated control _ (underscore) required separator eventname is name of corresponding event ( ) set of parentheses is required End Sub indicates end of a procedure
Illuminating Computer Science CCIT 4-6Sep The Sub Statement Example Private Sub cmdCalcTriangle_Click Dim Base As Single Dim Height As Single Area = 1 / 2 * (Base * Height) End Sub
Illuminating Computer Science CCIT 4-6Sep Declarations, Variables, and Constants Variable - a uniquely named storage location that contains data that changes during program execution Constant - a uniquely named storage locations that contains data that does not change during program execution
Illuminating Computer Science CCIT 4-6Sep Declarations, Variables, and Constants Rules for Naming Variables –Must begin with an alphabetic character –Can’t contain a period or type-declaration characters such as %, &, !, or $ –Must be unique with same scope –Must be no longer than 255 characters –Should not reserved word (See Appendix A)
Illuminating Computer Science CCIT 4-6Sep Declaring Variables Declaration statement - nonexecutable code that sets aside storage locations for future use Local variables - declared within a procedure or function Global variables - declared in the general section of the application
Illuminating Computer Science CCIT 4-6Sep Declaring Variables Declare variables using the Dim or Static statements Dim statement - value of variable preserved only until procedure ends Static statement - value of variable preserved the entire time the application is running
Illuminating Computer Science CCIT 4-6Sep The Dim Statement Dim variablename As datatype Where Dim is required variablename should be a descriptive name As is required datatype is one of the following types: Boolean, Byte, Date, Integer, Long, Single, Double, Currency, String, Object or Variant
Illuminating Computer Science CCIT 4-6Sep Declaring Variables Data Types –Boolean - True or false –Date - From Jan 1, 100 to Dec 31, 9999 –Integer - Numbers without a decimal point –Long - Long integer –Single - Numbers with a decimal point –Double - Long Single –Currency - Dollar amounts –String - Character and alphanumeric data –Object - Any object reference such as Word document –Variant - default, can hold any data type
Illuminating Computer Science CCIT 4-6Sep Assigning Values to Variables Variablename = value Where variablename is the descriptive name of the variable = is the assignment operator value is the value the variable will contain Examples: Number1 = 5 FirstName = “Steve” LoanAmount = Length = 17.8 Note: Order is important. Variable name always on the left, and value on the right.
Illuminating Computer Science CCIT 4-6Sep Declaring Constants Const constantname As datatype = value Where Const is required constantname is the descriptive name of the constant As is required datatype is the type of data the constant will contain = is the assignment operator value is the value of the constant Examples: Const Pi As Single Const MaxNumber As Integer = 100
Illuminating Computer Science CCIT 4-6Sep Function - unit of code that returns a value Built-in Functions –Sqr - square root –Rnd - random number generator –Int - returns integer portion of a number –Val - converts a string to a value –Str - converts a value to a string
Illuminating Computer Science CCIT 4-6Sep Application 1: Calculate the area of a circle Inputs: radius of the circle r Output: area of the circle Process: Area=
Illuminating Computer Science CCIT 4-6Sep Application 1: Calculate the area of a circle (Form View)
Illuminating Computer Science CCIT 4-6Sep Application 1: Calculate the area of a circle (Code View)
Illuminating Computer Science CCIT 4-6Sep Application 1: Calculate the area of a circle (Run)
Illuminating Computer Science CCIT 4-6Sep Precedence Table Operator ( ) ^ *, / +,- If precedence of two following operators is equal, then the evaluation starts from left to right.
Illuminating Computer Science CCIT 4-6Sep Simple Examples of Equations Equation in normal form: –Y=3X –Y=X-10+3(X-Z) –Y=X^2*10 –Y= Equation in VB form: –Y=3*X –Y=X-10+3*(X-Z) –Y=X*X*10 –Y=SQR(X)
Illuminating Computer Science CCIT 4-6Sep Application 2: Convert from Fahrenheit Degree to Celsius Original formula: Celsius=5/9(Fahrenheit-32) Visual Basic formula: Celsius=5/9*(Fahrenheit-32) Input: Fahrenheit Output: Celsius Process: Celsius=5/9*(Fahrenheit-32)
Illuminating Computer Science CCIT 4-6Sep Application 2: Convert from Fahrenheit Degree to Celsius (Form View)
Illuminating Computer Science CCIT 4-6Sep Application 2: Convert from Fahrenheit Degree to Celsius (Code View)
Illuminating Computer Science CCIT 4-6Sep Application 2: Convert from Fahrenheit Degree to Celsius (Run)
Illuminating Computer Science CCIT 4-6Sep Application 3: The Wind Chill Application Write a program that calculates the wind chill temperature Inputs: Wind Speed and Temperature Outputs: Wind Chill Temperature
Illuminating Computer Science CCIT 4-6Sep Application 3:The Wind Chill Application Original formula –WC = (3.71(V **0.5) V)(T ) Visual Basic statement –WC = * (3.71 * Sqr(V) (0.25 * V)) * (T ) Output: Wind Chill (WC) Input 1 T: Temperature Input 2 V: Wind Speed
Illuminating Computer Science CCIT 4-6Sep Adding Controls and Changing their Properties
Illuminating Computer Science CCIT 4-6Sep Writing the Code
Illuminating Computer Science CCIT 4-6Sep Running the Application
Illuminating Computer Science CCIT 4-6Sep Control Structures
Illuminating Computer Science CCIT 4-6Sep Controlling your Application Sequence Selection –If...Then...Else statement –Select Case statement Repetition –For...Next Loop statement –For Each...Next statement –Do Loops
Illuminating Computer Science CCIT 4-6Sep Selection If..Then..Else If (condition) Then (statement[s]) [ElseIf (condition) Then (statement[s])] [Else (statement[s])]
Illuminating Computer Science CCIT 4-6Sep Selection Application: Convert from Numerical Grade to Letter Grade Input: a numeric grade between 0 and 100 Output: depends on input InputOutput 0 49F 50 52D 53 56D+ 57 60C- 61 64C 65 69C+ 70 74B- 75 79B 80 84B+ 85 89A- 90 100A
Illuminating Computer Science CCIT 4-6Sep Selection Application: Convert from Numerical Grade to Letter Grade (Form View)
Illuminating Computer Science CCIT 4-6Sep Selection Application: Convert from Numerical Grade to Letter Grade (Code View)
Illuminating Computer Science CCIT 4-6Sep Selection Application: Convert from Numerical Grade to Letter Grade (Run)
Illuminating Computer Science CCIT 4-6Sep For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if less than end. If so, repeat loop again. If not, go to statement after Next.
Illuminating Computer Science CCIT 4-6Sep Do While...Loop Structure Do While condition statements Loop Where The condition is tested, and if true the loop is repeated. When the condition is false, the loop statements are skipped the statement after Loop is executed.
Illuminating Computer Science CCIT 4-6Sep Do Until...Loop Structure Do Until condition statements Loop Where The condition is tested, and if false the loop is repeated. When the condition is true, the loop statements are skipped the statement after Loop is executed.
Illuminating Computer Science CCIT 4-6Sep Loops Application: Factorial Factorial (N)= N*(N-1)*(N-2)……….*(1)
Illuminating Computer Science CCIT 4-6Sep Factorial Flowchart (N>0)
Illuminating Computer Science CCIT 4-6Sep Factorial Application: Form View
Illuminating Computer Science CCIT 4-6Sep Factorial Application: Code View Using For..Next Loop
Illuminating Computer Science CCIT 4-6Sep Factorial Application: Code View Using Do..While..Loop
Illuminating Computer Science CCIT 4-6Sep Factorial Application: Code View Using Do..Until..Loop
Illuminating Computer Science CCIT 4-6Sep Factorial Application: Run
Illuminating Computer Science CCIT 4-6Sep After the completion of this lecture, the audience should be familiar with the visual basic environment and different controls. The audience should be able to write simple programs and generate simple VB applications.