1 / 126

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 Objectives. The Initial Visual Basic Screen. Menu bar. Project Explorer window. Toolbar. Toolbox. Properties window.

waugh
Télécharger la présentation

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 Objectives Chapter 3 - Visual Basic Schneider

  3. The Initial Visual Basic Screen Menu bar Project Explorer window Toolbar Toolbox Properties window Description pane Form Project Container window Form Layout window Chapter 3 - Visual Basic Schneider

  4. Steps to Create a Visual Basic Program 1. Create the interface by placing controls on the form 2. Set properties for the controls and the form 3. Write code for event procedures associated with the controls and the form Chapter 3 - Visual Basic Schneider

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

  6. Placing a Text Box on a Form • Double-click on the text box icon in the toolbox 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. Placing a label on a Form Chapter 3 - Visual Basic Schneider

  8. Name - all Caption - 1,3 Text (for Text Boxes) - 2 BorderStyle - 2,3,4 Visible - all BackColor – all For command buttons, change style to graphical Alignment- 2,3,4 Font - all Some Useful Properties of Objects 1. Command Buttons 2. Text Boxes 3. Labels 4. Picture Boxes Chapter 3 - Visual Basic Schneider

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

  10. Naming Objects: Chapter 3 - Visual Basic Schneider

  11. Naming Objects • An Object Name • Must Start with a letter • Can include numbers and underscore (_) • Cannot include punctuation or spaces • Can be a maximum of 40 characters Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  12. Visual Basic Events • Code is a set of statements that instruct the computer to carry out a task. • Code can be associated with events • When an event occurs, the code associated with that event (called an Event Procedure) is executed. Chapter 3 - Visual Basic Schneider

  13. Creating An Event Procedure • Double-click on an object to open a Code window. (The empty default event procedure will appear. Click on the Procedure box if you want to display a different event procedure.) • Write the code for that event procedure. Chapter 3 - Visual Basic Schneider

  14. Example of An Event Procedure Private Sub objectName_event ( ) statements End Sub Private Sub Text2_GotFocus( ) Text2.Font.Size = 12 Text2.Font.Bold = False Text2.forecolor=vbRed End Sub Chapter 3 - Visual Basic Schneider

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

  16. Exercises: What is wrong??! 11. Private Sub cmdButton_Click( ) frmHi = “Hello” End Sub 12. Private Sub cmdButton_Click( ) txtOne.ForeColor= “red” End Sub Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  17. Exercises 13. Private Sub cmdButton_Click( ) txtBox.Caption = “Hello” End Sub 16. Private Sub cmdButton_Click( ) txtOne.MultiLine= True End Sub Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  18. Tips • Most Properties can be set or altered at run time with code. cmdButton.visible = False • The MultiLine property of a text box can only be set from the properties window, also the Name property…. • “” surrounds Caption, Name, Font.Name or strings. Not True for vars or numeric constants Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  19. Color Constants • At design time colors are selected from a palette • At run time the eight most common colors can be assigned with the color constants: Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

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

  21. Constant Can NOT change during the execution of a program. Types of Constants: numeric constants string constants Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  22. Valid Numeric Constants: IntegerReal number -2987 -1900.05 16 0.0185 5 10.56 Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

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

  24. Arithmetic 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 Chapter 3 - Visual Basic Schneider

  25. Examples of Arithmetic Operations • 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

  26. Scientific Notation Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  27. String Constants: A sequence of characters treated as a single item The characters in a string must be surrounded by double quotes (“ ”) Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  28. Valid String Constants “A rose by any other name” “9W” “134.23” “She said, ‘stop , thief!’ ” Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

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

  30. 5 x Variables 2.6 y z 7.6 • A storage location in main memory whose value can be changed 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

  31. Must begin with a letter Must contain only letters, numeric digits, and underscores ( _ ) Can have up to 255 characters Cannot be a Visual Basic language keyword (for example, Sub, End, False) Rules for Naming Variables Chapter 3 - Visual Basic Schneider

  32. Valid Variable Names: timeElapsed a1b2c3 Var_1 n celsius Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  33. Invalid Variable Names: maximum/average 1stChoice square yard Name? Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  34. Keywords Words that have predefined meaning to Visual Basic . Can Not be used as variable names. Examples: End - Print Sub - Let If -Select While -Call Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  35. Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable must be on the left and the expression on the right. Numeric Variables Chapter 3 - Visual Basic Schneider

  36. Assignment Statement: The statement var = expr assigns the value of the expression to the variable tax = 0.02 * (income - 500 * dependents) sum = 2 + x + 4.6 + y Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

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

  38. Invalid Assignment Statements 10 = count count + 1 = count Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  39. String Variables A string variable stores a string. The rules for naming string variables are identical to those for naming numeric variables. When a string variable is first declared, its value is the empty string. Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  40. Print is a method used to display data on the screen or printer. Can be used to display values of variables or expressions Visual Basic Print Statement Chapter 3 - Visual Basic Schneider

  41. Examples 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 3 ^ 2 picResults.Print 2 * (3 + 4) End Sub Chapter 3 - Visual Basic Schneider

  42. speed=3 taxRate=speed+5 total=30 picOutput.Print speed picOutput.Print taxRate picOutput.Print “Class average is”; total/3 Examples of Print Statements 3 8 Class average is 10 Chapter 3 - Visual Basic Schneider

  43. x = 15 y = 5 picOutput.Print (x + y) / 2, x / y Output: 10 3 Examples of Print Statements Chapter 3 - Visual Basic Schneider

  44. String Variable Example 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 Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  45. Concatenation Two strings can be combined by using the concatenation operation. The concatenation operator is the ampersand (&) sign. Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  46. Concatenation, Example Private Sub Command1_Click() Print "cs116" & "cs112" Print "cs" & 116 Print 116 & "cs" Print 116 & 117 Print "cs116" ; "cs112" Print "cs" ; 116 Print 116 ; "cs" Print 116 ; 117 End Sub Chapter 3 - Visual Basic Schneider

  47. Example &: is always used to make concatenation +: makes concatenation when it is used with strings, and as summation with numbers What about “hi”+5 ??? Chapter 3 - Visual Basic Schneider

  48. Examples of Concatenation: Private Sub Command1_Click() strVar1 = "Hello" strVar2 = "World" picOutput.Print strVar1 & strVar2 txtBox.Text = "32" & Chr(176) & " Fahrenheit" End Sub Chr(176) prints º Chapter 3 - Visual Basic Schneider Chapter 3 - Visual Basic Schneider

  49. Chr() & Asc() • The characters have numbers associated with them, these values called ANSI values of characters • Chr(65) = A • Chr(66) = B • Chr(97) = a • Chr(176) = Asc(A) = 65 Asc(B) = 66 Asc(a) = 97 Asc( ) = 176 Chapter 3 - Visual Basic Schneider

  50. Chapter 3 - Visual Basic Schneider

More Related