1 / 74

Programming Practice in Visual Basic

Programming Practice in Visual Basic. The Initial Visual Basic Screen. Menu bar. Project Explorer window. Toolbar. Toolbox. Properties window. Description pane. Form. Project Container window. Form Layout window. Steps to Create a Visual Basic Program.

topper
Télécharger la présentation

Programming Practice in 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. Programming Practice in Visual Basic

  2. The Initial Visual Basic Screen Menu bar Project Explorer window Toolbar Toolbox Properties window Description pane Form Project Container window Form Layout window

  3. 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

  4. Four Useful Visual Basic Controls • Text Boxes • Labels • Command Buttons • Picture Boxes

  5. 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

  6. Placing a Text Box on a Form Text box

  7. Some Useful Properties of Objects • Name • Caption • Text (for Text Boxes) • BorderStyle • Visible • BackColor • Alignment • Font Border Style: Setting the BorderStyle to 0-None removes the border from an object Visible: Setting the property to false hides an object when the program run. The object can be set to reappear with code BackColor: Specifies the background color for text box, label, picture box or form.. Also specific background color for a command button having Style set to “1-Graphical” BackStyle: The background of a label is opaque by default. Setting the background style of a label to transparent causes whatever is behind the label remain visible.; the background color of the label essentially becomes “see through” Font: Two unusual fonts are Symbols and Wingdings> For instance with the windingsfonts , changing the text to % & ‘ and J yields a bell, a book, a candle and a smiling face

  8. Example

  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.

  10. Naming Objects:

  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

  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.

  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.

  14. Example of An Event Procedure Private Sub objectName_event ( ) ‘Here are your statements’ End Sub Private Sub txtOne_GotFocus( ) txtOne.Font.Size = 12 txtOne.Font.Bold = False End Sub

  15. More Examples Private Sub cmdButton_Click( ) txtBox.ForeColor = vbRed txtBox.Font.Size = 24 txtBox.Text = “Hello” End Sub

  16. Exercises 11. Private Sub cmdButton_Click( ) txtOne.text= “Hello Doctor” End Sub 12. Private Sub cmdButton_Click( ) txtOne.ForeColor = “red” End Sub

  17. Exercises 13. Private Sub cmdButton_Click( ) txtBox.Caption = “Hello” End Sub 16. Private Sub cmdButton_Click( ) txtOne.MultiLine= True End Sub

  18. Tips • Most Properties can be set or altered at run time with code. cmdButton.visible = False • The BorderStyle and MultiLine properties of a text box can only be set from the properties window • “” surrounds Caption, Name, Font.Name or strings not True, vars or numeric constants

  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:

  20. Components of Visual Basic Statements • Constants • Variables • Keywords (reserved words)

  21. Constant • Can NOT change during the execution of a program. • Types of Constants: • numeric constants • string constants

  22. Valid Numeric Constants: IntegerReal number -2987 -1900.05 16 0.0185 5 10.56

  23. Invalid Numeric Constants: 14,005.5 6.8% 33- $190.04 15 78 3.5&

  24. String Constants: • A sequence of characters treated as a single item • The characters in a string must be surrounded by double quotes (“ ”)

  25. Valid String Constants “A rose by any other name” “9W” “134.23” “She said, ‘stop , thief!’ ”

  26. Invalid String Constants ‘Down by the Seashore’ “134.24 “She said, “Stop, thief!””

  27. Arithmetic Operations Operator Operation Basic expression ^ Exponentiation A ^ B * Multiplication A * B / Division A / B + Addition A + B - Subtraction A - B

  28. Variables • 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

  29. 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 VB does not distinguish between uppercase and lowercase letters. Example: numberOfCars, tax_Rate_1994 Let statement assigns values to variables and Print method displays the values of variable.

  30. 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

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

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

  33. 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. String Variables

  34. 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 String Variable Example

  35. Two strings can be combined by using the concatenation operation. The concatenation operator is the ampersand (&) sign. Concatenation

  36. strVar1 = “Hello”strVar2 = “World”picOutput.Print strVar1& strVar2 txtBox.Text = “32” & Chr(176) & “ Fahrenheit” Examples of Concatenation:

  37. Assignment Statement: • The statementvar = exprassigns the value of the expression to the variable • tax = 0.02 * (income - 500 * dependents) • sum = 2 + x + 4.6 + y

  38. Valid Assignment Statements • count = count + 1 • num = 5 • count = count + num /2

  39. 10 = count count + 1 = count Invalid Assignment Statements

  40. Words that have predefined meaning to Visual Basic . Can Not be used as variable names. Examples: End - Print Sub - Let If -Select While -Call Keywords The VB editor automatically capitalizes the first letter of reserved word

  41. Printis 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 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

  42. x = 15 y = 5 picOutput.Print (x + y) / 2, x / y Output: 10 3 Examples of Print Statements

  43. Declaring Variable Types • Use the Dim statement to declare the type of a variable. • Examples: Dim number As Integer Dim flower As String Dim interestRate As Single From now on we will declare all variables. Declaring variables is regarded as good programming practice.

  44. Data Types : • Single: a numeric variable that stores real numbers • Integer: a numeric variable that stores integer numbers (from -32768 to 32767) • String: a variable that stores a sequence of characters

  45. Using Text Boxes for Input/Output • The contents of a text box are always a string. • Numbers can be stored in text boxes as strings.

  46. Using Text Boxes for Input/Output • The contents of a text box should be converted to a number before being assigned to a numeric variable. • Val(txtBox.Text) gives the value of a numeric string as a number • Example: Dim numVar as Single numVar = Val(txtBox.Text)

  47. Example (convert miles to furlongs and vice versa) • Example 1 xString=“528” xValue=Val(xString)  xValue=528 • Example 2 yValue=428 yString=Str(yValue)  yString=“428”

  48. Example (convert miles to furlongs and vice versa) Private Sub txtFurlong_LostFocus() txtMile.Text = Str(Val(txtFurlong.Text / 8)) End Sub Private Sub txtMile_LostFocus() txtFurlong.Text = Str(8 * Val(txtMile.Text)) End Sub

  49. Program Documentation • An apostrophe (') is used to indicate that the remainder of the line is a comment. (Comments are ignored by Visual Basic.) • Remarks can appear on a separate line or following a Visual Basic statement.

  50. Using Message Dialog Box for Output • The message dialog box is used to present a pop-up window containing information for the user • Syntax: MsgBox prompt, , title

More Related