1 / 94

Chapter 3

Chapter 3. Fundamentals of Programming in Visual Basic. Visual Basic Objects Visual Basic Events. Numbers Strings Input/Output Built-In Functions . Outline and Objective. The initial Visual Basic screen. Menu bar. Toolbar. Project Explorer window. Toolbox. Properties window.

ssallie
Download Presentation

Chapter 3

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 3 Fundamentals of Programming in Visual Basic Chapter 3 - Visual Basic Schneider

  2. Visual Basic Objects Visual Basic Events Numbers Strings Input/Output Built-In Functions Outline and Objective Chapter 3 - Visual Basic Schneider

  3. The initial Visual Basic screen Menu bar Toolbar Project Explorer window Toolbox Properties window Form Chapter 3 - Visual Basic Schneider

  4. Steps to Create a Visual Basic Program 1. Create the Objects 2. Set Properties 3. Write the Code for each Event Chapter 3 - Visual Basic Schneider

  5. Four most useful Visual Basic Controls • Text Boxes • Labels • Command Buttons • Picture Boxes Chapter 3 - Visual Basic Schneider

  6. A Text Box Walkthrough: • Double-click on Text Box to add a Text Box to your form • Activate the Properties window (Press F4) • Set values of Properties for Text Box Chapter 3 - Visual Basic Schneider

  7. A Text Box Walkthrough Text box Chapter 3 - Visual Basic Schneider

  8. Name Caption Border style Visible Back Color Alignment Font Some Useful Properties: Chapter 3 - Visual Basic Schneider

  9. Naming Objects: • Use the Propertywindow to change the Name property of an object • Good Programming habit is that each name begins with three letter prefix that identifies the type of control. Chapter 3 - Visual Basic Schneider

  10. Naming Objects: Chapter 3 - Visual Basic Schneider

  11. Visual Basic Events • Code is a set of statements that will be executed when you run a program. • Write Code for each Event. • Most Events are associated with Objects. • The code for each event is called an “Event Procedure”. Chapter 3 - Visual Basic Schneider

  12. The steps for creating a VB program: • Create the Interface. • Set Properties for the objects. • Write the code that executes when event occur. Chapter 3 - Visual Basic Schneider

  13. An Event Procedure Walkthrough • Create the interface. • Set Properties. • Double click on the object to open the Code window. • Click on the Procedure box to find the event • Write the code for that event. Chapter 3 - Visual Basic Schneider

  14. Example of An Event Private Sub objectName_event ( ) statements End Sub Private Sub txtOne_GotFocus( ) txtOne.Font.Size = 12 txtOne.Font.Bold = False End Sub Chapter 3 - Visual Basic Schneider

  15. More Example Private Sub cmdButton_Click( ) txtBox.ForeColor = vbRed txtBox.Font.Size = 24 txtBox.Text = “Hello” End Sub Chapter 3 - Visual Basic Schneider

  16. Components of Visual BASIC Statements • Variables • Keywords (reserved words) • Constants Chapter 3 - Visual Basic Schneider

  17. Variables • A storage location in main memory whose value can change during program execution. • These storage locations can be referred to by their names. • Every variable has three properties: a Name, a Value, and a Data Type. • Types of variables: Numeric and String Chapter 3 - Visual Basic Schneider

  18. Must begin with a letter. Can contain letters, numeric digits. Can have up to 255 characters. Can Not be restricted keyword. Rules for Creating Variable Names Chapter 3 - Visual Basic Schneider

  19. Used to store Numbers . The value is assigned either by the programmer or by calculation. Numeric Variables Chapter 3 - Visual Basic Schneider

  20. timeElapsed taxRate speed n celsius Valid Numeric Variable Names: Chapter 3 - Visual Basic Schneider

  21. maximum/average 1stChoice square yard Invalid Numeric Variable Names: Chapter 3 - Visual Basic Schneider

  22. Constant • Similar to a variable, but can NOT change during the execution of a program. • Types of Constants: • numeric constants • string constants Chapter 3 - Visual Basic Schneider

  23. Valid Numeric Constants: Integer Real number -2987 -1900.05 +16 0.0185 5 10.56 Chapter 3 - Visual Basic Schneider

  24. Invalid Numeric Constants: 14,005.5 6.8% 33- $190.04 15 78 3.5& Chapter 3 - Visual Basic Schneider

  25. Numeric Constants in a Statement: tax = 0.02 * (income - 500 * dependence) sum = 2 + x + 4.6 + y Chapter 3 - Visual Basic Schneider

  26. String Constants: • A group of alphanumeric data consisting of any type of symbols. Chapter 3 - Visual Basic Schneider

  27. Valid String Constants “A rose by any other name” “Down By the Sea Shore” “134.23” “She said, ‘stop , thief!’” Chapter 3 - Visual Basic Schneider

  28. Invalid String Constants ‘Down by the Seashore’ “134.24 “She said, “Stop, thief!”” Chapter 3 - Visual Basic Schneider

  29. Arithmetic Operations & Hierarchy of Operations Operator operation Basic expression ^ Exponentiation A ^ B * Multiplication A * B / Division A / B + Addition A + B - Subtraction A - B Chapter 3 - Visual Basic Schneider

  30. Examples Evaluate the following expressions: x = 3 * 6 - 12 / 3 x = 4 ^ (8 / 4) y = 12 + 6 / (3 * (10 - 9)) z = 5 + 4 ^ 2 m = 6 / 3 + 3 Chapter 3 - Visual Basic Schneider

  31. Words that have predefined meaning to Visual Basic . Can Not be used as variable names. Example: Print Cls If While Keywords Chapter 3 - Visual Basic Schneider

  32. Print: Is a method used to display data on the screen or printer. Can be used to print value of variables. Can be used to print value of arithmetic expressions . Visual Basic Print Statement Chapter 3 - Visual Basic Schneider

  33. Example of Print Statements Private Sub cmdCompute_Click() picResults.Print 3 - 2 picResults.Print 3 * 2 picResults.Print 3 / 2 picResults.Print 3 ^ 2 picResults.Print 2 * (3 + 4) End Sub Chapter 3 - Visual Basic Schneider

  34. picOutput.Print speed picOutput.Print taxRate picOutput.Print “Class average is”; total / 3 Example of Print Statement Chapter 3 - Visual Basic Schneider

  35. x = 15 y = 5 picOutput.Print (x + y) / 2, x / y Example Chapter 3 - Visual Basic Schneider

  36. 10 3 Output Chapter 3 - Visual Basic Schneider

  37. Internal Documentation • An apostrophe (‘) can be used to indicate comments; comments are ignored by Visual Basic. • The keyword Rem can also be used instead of an apostrophe for comments. • Remarks can also be placed after program statement too. Chapter 3 - Visual Basic Schneider

  38. Visual Basic Assignment Statement • The statement var = expr assigns the value of the expression to the variable. • Assigns the value of the expression on the right to the variable on the left. Chapter 3 - Visual Basic Schneider

  39. Example Private Sub cmdCompute_Click( ) picResults.Cls a = 5 b = 4 c = a * (2 + b) picResults.Print c End Sub Chapter 3 - Visual Basic Schneider

  40. Valid Assignment Statement count = count + 1 num = 5 count = count + num /2 Chapter 3 - Visual Basic Schneider

  41. 10 = count count + 1 = count Invalid Assignments Chapter 3 - Visual Basic Schneider

  42. A String variable stores character strings. The rules for naming string variables are identical to those of numeric variables. When a String variable is first declared, its value is the null string. (that is, the empty string). String Variables Chapter 3 - Visual Basic Schneider

  43. Private Sub cmdShow_Click() picOutput.Cls phrase = "win or lose that counts." picOutput.Print "It's not whether you "; phrase picOutput.Print "It's whether I "; phrase End Sub Example of String Variable Chapter 3 - Visual Basic Schneider

  44. Two string can be combined with the concatenation operation. Concatenation is represented with the ampersand ( & ) sign. Concatenation Chapter 3 - Visual Basic Schneider

  45. strVar1 = “Hello” strVar2 = “World” picOutput.Print strVar1& strVar2 Example of Concatenation: Chapter 3 - Visual Basic Schneider

  46. txtBox.Text = “32” & CHR(176) & “ Fahrenheit” Example of Concatenation Chapter 3 - Visual Basic Schneider

  47. Data Types • Each variable in the program is assigned to a data type. Chapter 3 - Visual Basic Schneider

  48. Declaring Variable Types • Use the Dim statement to Declare the type of a variable. Example: Dim number As Integer Dim flower As String Dim interestRate As Single Chapter 3 - Visual Basic Schneider

  49. Data Types : • Single-precision numeric variable: Stores real numbers • Double-precision numeric variable: Stores real numbers with many digits • Integer: Stores integers • Long integer: Stores integers with many digits Chapter 3 - Visual Basic Schneider

  50. Using Text Boxes for Input/Output • The contents of a text box are always a string. • Numbers are also stored in text boxes as strings. Chapter 3 - Visual Basic Schneider

More Related