1 / 179

Chapter One An Introduction to Programming and Visual Basic

Chapter One An Introduction to Programming and Visual Basic. What is Visual BASIC. Visual : Using GUI (Graphic User Interface) Technologies Window Objects & Controls (Forms, Buttons, … BASIC : Programming Language (Beginners All-purpose Symbolic Instruction Code) Different versions of BASIC

eamon
Download Presentation

Chapter One An Introduction to Programming and Visual Basic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter OneAn Introduction to Programming and Visual Basic

  2. What is Visual BASIC • Visual: • Using GUI (Graphic User Interface) Technologies • Window Objects & Controls (Forms, Buttons, … • BASIC: • Programming Language (Beginners All-purpose Symbolic Instruction Code) • Different versions of BASIC • MBASIC - Microsoft BASIC • BASICA - BASIC Advanced • Quick BASIC, QBASIC

  3. Six Basic Computer Operations • Input data • Store data • Process data • Compare values and select among alternatives • Repeat a group of actions • Output the results

  4. Types of Computer Languages • Procedural: Programs that run from start to finish with no intervention from user other than input • Basic, QBasic, QuickBasic • COBOL • FORTRAN • C • Object Oriented /Event Driven (OO/ED): Programs that use objects which respond to events; use small segments of code for each object • Visual Basic • Visual C++

  5. Levels of Computer Languages • Low Level: at the level of the computer, i.e., in binary (0-1) format (machine code) • Computer can only execute the binary form of a program • Intermediate level: close to the computer but uses abbreviated English words e.g., Assembler, that is converted directly into binary • High Level: at the level of the programmer using English like words and clearly defined syntax e.g., Visual Basic; must be converted or translated into binary for computer to implement it, • Need a software program to handle the conversion of high-level into binary

  6. Translating from High-level Language to Binary • Interpreted: each statement translated as it is executed--slow but easy to use (instant results, easy to debug) • Compiled: entire program is converted to binary--executes faster, but more difficult to use (.exe files are compiled programs) • VB is interpreted during creation and debug/test but can then be compiled into an .exe file

  7. Three Basic Control Structures in a Program • Sequence control structure • Decision control structure (Conditions) • Repetition control structure (Loops)

  8. Basic Concepts of Windows Programming Windows:The primary container of programming. Events: Operation activities (mouse click, keyboard press,...). Message: Information about the event. Objects: Identifiable entities (forms, controls, data objects,…) Properties: The attributes associated with an object (name, caption, size, position, …) Methods: A set of predefined activities that the object can carry out

  9. Object Oriented Programming • Procedural Programming: Making the entire program, from the interface appearance to the process codes; May be divided into the modules. • OOP: Only make small pieces of codes into individual event snippets of objects. Not need to code for the interface itself.

  10. OOP Programming Process A six step process for writing an OOP computer program: 1. Define problem. 2. Create interface 3. Develop logic for action objects 4. Write and test code for action objects 5. Test overall project 6. Document project in writing

  11. IPO Table for Calculate Button

  12. Using Pseudocode • An important part of the developing the logic for action objects is generating corresponding pseudocode. • Pseudocode involves actually writing a program in English rather than in a computer language. • When the actual computer program is written, the pseudocode is translated into computer language. • A pseudocode program is useful for two reasons: • The programmer may use it to structure the algorithm's logic in writing. • It provides a relatively direct link between the algorithm and the computer program

  13. Pseudocode for Calculate Button Begin procedure Input Video Price Taxes = 0.07 x Video Price Amount Due = Video Price + Taxes Output Taxes and Amount Due End procedure

  14. Step Six: Documentation Three levels of documentation: • Comments in the code • On-line Help • User’s Guide and Programmer Document

  15. Chapter Two Creating a First Project in Visual Basic

  16. Getting Started with Visual Basic • Double-click VB icon on Desktop or use Start|Programs|Visual Basic to bring up opening dialog box

  17. Basic Components of VB • Project (.vbp) • Forms (.frm) • Controls • Event procedures • Modules (.bas) • procedures • Additional components • Data Environment Designer - DED (.dsr) • Data Report Designer - DRD (.dsr)

  18. VB Development Environment Form Size Toolbar Project Explorer Initial Form Window Toolbox Properties Window Project Window Form Layout Window Sizing Handles

  19. The Toolbox Label Control Textbox control Used to select a control to place on form.

  20. Project Explorer Window View code View Object Change Folders Used to select the code or object or to change folders.

  21. The Properties Window Window Name Object Box Properties List Tabs Scrollable list of properties Description Pane Used to change the properties of controls at design time.

  22. Important Properties • Name: identifies the object (the text seen by VB) • Should start with three letter prefix (see recommendation) • lbl for label • txt for text box • frm for form • Can include letters, numbers, and underscore • Caption: The text (of the object) you see on screen

  23. Adding a Control (Special Object) • Double-click to add control to center of form • Can be moved around from there • Use sizing handles to change size • Single Click and draw on form • Select an exist one and use Copy/Paste methods.

  24. Controls to Add to Form • Label control: provides description of other controls • Image control: holds a graphic image • Stretch property should be set to True • Picture property is set to graphic file • Command button control: can be clicked to provide action • Caption property

  25. Adding Code to Project • Code is organized in Code Window as EVENT PROCEDURE • Event procedure begins with “Private Sub…” and ends with “End Sub” Example: Use Msgbox command to display dialog box with message: Msgbox “Welcome to Vintage Videos”

  26. Design Time and Run Time • The project is in “Design Time” when you are designing the project and adding code. • The project is in “Run Time” when you run the project by clicking the VCR Run icon or Menu Run | Start. • You can stop the project and exit Run time by clicking the VCR Stop Icon or Menu Run | End. The project returns to Design Time. • You can go to “Break Mode” by clicking the VCR Pause Icon or Menu Run | Break.

  27. Difference between End and Break Mode • When the project is ended, all the internal results are lost. It can only be started from beginning. • When project is break, all the internal results are kept in memory. You can check these results to see whether your program is in right direction. It can be re-started just from the break point.

  28. The Toolbar and Menu System • The VB menu system provides access to all commands. • The Toolbar uses icons for more commonly used menu commands (benefit? One click through!).

  29. Files in Visual Basic • All projects in VB have a .vbp (project) file and at least one .frm (form file) file. • Always save .frm files first and then save project files. Use File|Save or File|Save as… commands for this purpose or click Disk icon on Toolbar. • Projects with graphics also have .frx (binary form) files. They are saved automatically. • Module files have a .bas extension and are pure code files.

  30. Visual Basic Help • There are three types of Help: • Help menu option • context-sensitive help • and Auto Help (IntelliSense) • In VB 6, Help uses the familiar Internet Explorer browser interface for the first two types of help. • You can seek help by selecting Contents, Index, or Search from the Help menu item • Pay attention to the selected Active Subset

  31. Context-Sensitive and Auto Help • With context-sensitive help, pressing the F1 key provides help on whatever item the cursor is located. • With Auto Help, VB tries to help you with a code statement by providing: • A list of items to complete the statement • Info on the statement you have started • Tips on the type of data you are working with

  32. Chapter 3Variables, Assignment Statements, and Arithmetic

  33. Variables • Variables are named locations in memory (memory cells) in which we store data that will change at run time • Variable names in VB: • Must begin with letter • can’t include period • can’t be over 255 characters • are not case-sensitive • Example variable names: • VideoPrice for Video Rental Price • Taxes for taxes on this price • AmountDue for sum of price and taxes • YTDEarnings for year to date earnings • EmpLastName for employee’s last name

  34. Name Convention • Use capital letters to distinguish parts in a name. • LastName, StudentID, StreetNum • Use standard prefix for certain type object • txt: Text box, txtName • but( or cmd): Command button, butOK • lst: List box, lstPrices • frm: Form, frmInput • str: String variable, strName • (see the recommendation list)

  35. Data Types I • Two primary types of data: numeric and string • Numeric data • 3.154 2300 -34 .0000354 0.97E-5 are all numeric constants • String data must use “ or ‘ as the delimiters • “Dogs” “123-45-6789” “Dr. Brown” are all string constants • Numeric data types can be subdivided into specific types: • currency $55,567.78 integer 255 • single 567.78 long (integer) 35,455 • double 567.78129086 Boolean True or False

  36. Data Types II Numeric data types can be subdivided into following specific types:

  37. Variant Type • A special data type in VB that can be used to store (reference) any previous data type and even image data.

  38. Declaring Variables • Declare ALL variables with the Dim statement • Syntax • Dim Variable1 as type1, variable2 as type2 • For example, • Dim MyName as string, MyValue as single • Dim Taxes as Currency, Price as Currency • Dim AmountDue as Currency • Dim mVar • Dim mVar, AmountDue as Currency • Two or more variables can be declared with same Dim statement but you must include the variable type • Use variables rather than objects in processing since all text boxes are strings

  39. The Option Explicit Statement • Begin ALL Forms with the Option Explicit command in the declarations procedure of the general object. This forces all variables to be declared • To automatically include Option Explicit, go to the Tools|Options|Editor menu selection and check the box for “Require variable declaration” • If you fail to declare a variable after the Option Explicit statement has been entered, an error occurs at Run time

  40. Variable vs Constant • Variable is a memory unit with a named ID • Constant is a prefixed value in program codes. • Variable can be changed in run time. Constant never changes. • Variable is referenced quicker than constant. Because constant needs to to translated/extracted from codes. • To improve performance, use variable instead of constant if that constant is used frequently in a program.

  41. Assignment Statements • Must convert strings in text boxes to numeric with Val function, eg, • Price = val(txtPrice) • Convert numeric variables to strings before assigning to text box, e.g., • txtAmountDue = str(AmountDue) • Carry out calculations with assignment statements, eg, • Taxes = 0.07*Price • AmountDue = Price + Taxes

  42. Using Functions • Sometimes we want to convert a string to a number or vice versa or to compute a single value • To do this, we use a function that is nothing more than an operation that takes a multiple arguments and generates a single value • variable = functionName(arg1, arg2, …) • Example functions for converting data: • Val to convert a string to a number • Str to convert a number to a string • Ccur to convert a string to a currency data type

  43. Using Assignment Statements for Calculations • Sometimes, an expression will be on right of assignment statement • An expression is a combination of one or more variables and/or constants with operators • Operators are symbols used for carrying out processing

  44. Arithmetic Operators • () for grouping + for addition • ^ for exponentiation - for subtraction • - for negation • * for multiplication • / for division • \ for integer division • mod for modulus

  45. Hierarchy of Operations • Operations within parentheses ( ) • Exponentiation (^) • Negation (-)' • Multiplication and division (*,/) • Integer division (\) • Modulo arithmetic (Mod) • Addition and subtraction (+,-) • String concatenation (&)

  46. Arithmetic Example • 3 * (Salary - Taxes)^2 + Bonus/Months 3 1 2 5 4 (order) • Order 1 Subtract Taxes from Salary 2 Square the result 3 Multiply this result by 3 4 Divide Bonus by Months 5 Subtract result from first expression

  47. Symbolic Constants • We can assign a name to a constant with the Const statement const constant name as variable type = value • Example Const IntRate as single = 0.07

  48. Comments • To explain the purpose of a statement, a comment statement is added • Any statement beginning with an apostrophe or REM is a comment • Comments can be added to end of statements using apostrophe

  49. Formatting Data • To display information in a well designed form, we can use the Format function: variable = Format(variable, format expression) • Where the format expressions are in quotes and include; • Currency • Fixed • Standard • Percent • Scientific • Example: txtTaxes.text = Format(Taxes, “currency”)

More Related