More VB statements - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

More VB statements

Description:

Draw a picture box and Name it picResult. Draw a textbox and name it txtInput ... An apostrophe (') is used to indicate that the remainder of the line is a comment. ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 31
Provided by: dccl9
Category:

less

Transcript and Presenter's Notes

Title: More VB statements


1
More VB statements
  • Week 3

2
Visual Basic Print Statement
  • Print is a method used to display data on the
    screen or printer.
  • Can be used to display values of variables or
    expressions

3
For our F to C conversion program
  • Draw a picture box and Name it picResult
  • Draw a textbox and name it txtInput
  • Draw your two command buttons
  • In the procedure for Command button one
  • Change the caption to C to F
  • In the procedure put in
  • picResult.Print (9/5) txtInput 32
  • In the procedure for command button two
  • Change the caption to F to C
  • In the procedure put in
  • picResult.Print (5/9) (txtInput 32)

4
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.
  • At the end of our Statement
  • picResult.Print (9/5) txtInput 32
  • We could have added a comment
  • picResult.Print (9/5) txtInput 32 C to F

5
Formatting the Output
  • Create easily readable output
  • In the Print method, the spacing of the output is
    controlled by the following devices
  • semicolon
  • comma
  • Tab function

6
Semicolons
  • The next value output is placed in the next
    column position.
  • Example
  • picOutput.Print Lee Helen
  • Output LeeHelen

7
Semicolon
  • Output statement that has added space in the
    second string
  • picOutput.Print Lee Helen
  • Output Screen Lee Helen

8
Semicolon
  • picOutput.Print 100 -200 300
  • Output Screen
  • 100 -200 300

9
Commas
  • A comma in a Print method that causes the next
    value output to be placed in the next available
    print zone.
  • Each print zone is 14 positions wide.

10
Using Commas
  • Example
  • picOutput.Print SEE, YOU, SOON
  • Output Screen
  • SEE YOU SOON

11
Using Commas
  • A print zone can be skipped by typing consecutive
    commas
  • Example
  • picOutput.Print HELEN, , LEE
  • Output Screen
  • HELEN LEE

12
Tab Function
  • Specifies the column where output will start
  • Use only semicolons with the Tab function
  • Can only be used to advance the print position
    (cannot move backwards)

13
Example of Tab Function
  • Example
  • picOutput.Print Tab(3) Hello! Tab(22)
    Bye!
  • Output Screen
  • Hello! Bye!
  • In the above example, the first Tab(3) causes the
    output to be placed in the 3rd space and the
    Tab(22) causes the output string to be placed in
    the 22 space.

14
Assignment Statement
  • The statement var expr assigns the value of the
    expression to the variable. (The variable must be
    on the left and the expression on the right.)
  • ExamplePrivate Sub cmdCompute_Click( ) a
    4 b 3 (2 a)End Sub

15
Valid Assignment Statements
  • count count 1
  • num 5
  • count count num /2

16
Invalid Assignment Statements
  • 10 count
  • count 1 count

17
String Variables
  • A string variable stores a string value.
  • When a string variable is first declared, its
    value is a empty string.
  • The rules for naming string variables are
    identical to those for naming numeric variables.
  • Must begin with a letter 
  • Must contain only letters, numeric digits, and und
    erscores ( _ ) 
  • Can have up to 255 characters 
  • Cannot be a Visual Basic language keyword 

18
String Variable Example
  • Private Sub cmdWords_Click()
  • picOutput.Cls
  • phrase it is not the grade that counts."
  • picOutput.Print "It's learning VBasic " phrase
  • picOutput.Print This is my wish" phrase
  • End Sub

19
Concatenation
  • You can combine two strings by using the
    concatenation operation.
  • The concatenation operator is the ampersand ()
    sign

20
Examples of Concatenation
  • strVar1 Hello
  • strVar2 WorldPicOutput.Print strVar1
    strVar2
  • txtBox.Text 32 Chr(176) Fahrenheit

21
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

22
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

23
Using Text Boxes for Input/Output
  • By default, the contents of a text box are always
    a string.
  • Numbers can be stored in text boxes as strings.

24
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.
  • Rememeber our little add program from the second
    week
  • Val(txtBox.Text) gives the value of a numeric
    string as a number
  • Example Dim numVar as Single numVar
    Val(txtBox.Text)

25
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

26
Files will be covered in lecture in around two
week
  • Files are important to be able to setup record
    keeping systems.
  • Today Ill introduce them in lecture since we
    need to assure that you understand the concepts.
  • Will be very important for your class project.

27
Steps in Reading Data from Files
  • 1. Choose a number to be the reference number for
    the file
  • 2. Execute an Open statement
  • 3. Read the data sequentially using Input
    statements 4.
  • Close the file

28
Example of Reading from a File
  • Open DATA.TXT For Input As 1
  • Input 1, num1
  • Input 1, num2
  • Close 1

29
Example of Reading from a File
  • Open DATA.TXT For Input As 1 Input 1,
    num1, num2 picOutput.Print num1num2
  • Close 1

30
Tutorial
  • Go to the on-line textbook and complete the two
    chapters
  • Text boxes and buttons
  • List, combos and forms.
Write a Comment
User Comments (0)
About PowerShow.com