Fundamentals of Programming in Visual Basic

Slides:



Advertisements
Similar presentations
Working with Intrinsic Controls and ActiveX Controls
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
VB Numbers and Strings School of Business Eastern Illinois University (Week 4, Monday 2/03/2003) © Abdou Illia, Spring 2003.
Introduction to Visual Basic Programming. Lecture Outline History What is Visual Basic First Look at the VB 6.0 Environment Some VB Terminology Our first.
Fundamentals of Programming in Visual Basic
Input Dialog Box An input dialog box can be used to obtain a single item of input from the user Presents a window (dialog box) requesting input Syntax:
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
VB Built-in Functions School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 5, Monday 2/10/03)
The Initial Visual Basic Screen
Chapter 4 - Visual Basic Schneider
Introduction to Computing Dr. Nadeem A Khan. Lecture 11.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Fundamentals of Programming in Visual Basic
Introduction to scripting
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Variables & Math Operators CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Chapter 31 Fundamentals of Programming in Visual Basic (Continue VI) String Properties and Methods: "Visual".Length is 6. "Visual".ToUpper is VISUAL. "123.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Programming Practice in Visual Basic The Initial Visual Basic Screen Toolbox Project Explorer window Properties window Form Menu bar Description pane.
Fundamentals of Programming in Visual Basic
Database Management Review for Final (Part 1) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Wednesday 4/30/2003)
Visual Basic Chapter 1 Mr. Wangler.
A First Book of ANSI C Fourth Edition
Variables and Constants
Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Outline Software and Programming Program Structure Tools for Designing Software Programming Languages Introduction to Visual Basic (VBA)
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)
Introduction to Programming with RAPTOR
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 2 - VB 2008 by Schneider1 Chapter 2 –Visual Basic, Controls, and Events 2.1 An Introduction to Visual Basic 2.2 Visual Basic Controls 2.3 Visual.
Numbers continued The Integer Data Type Multiple Declarations Parentheses Three Types of Errors.
Chapter 8 - Visual Basic Schneider
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Introduction to Programming Fundamentals of Programming in Visual Basic.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Visual Basic.NET Programming for the Rest of Us Keith Mulbery Utah Valley State College.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
Chapter 3 - Visual Basic Schneider. Private Sub cmdEvaluate_Click() Dim n As Single, root As Single n = 6.76 root = Sqr(n) picResults.Print root; Int(n);
Chapter 6 JavaScript: Introduction to Scripting
Computing Fundamentals
2.5 Another Java Application: Adding Integers
Variables and Arithmetic Operations
Fundamentals of Programming in Visual Basic
Introduction to C++ Programming
WEB PROGRAMMING JavaScript.
Fundamentals of Programming in Visual Basic
Section 3.3 Numbers Arithmetic Operations Variables
Additional Topics in VB.NET
Introduction to Programming
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
Presentation transcript:

Fundamentals of Programming in Visual Basic Chapter 3 Fundamentals of Programming in Visual Basic Chapter 3 - Visual Basic Schneider

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

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

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 hhhhhhh

Four most useful Visual Basic Controls Text Boxes Labels Command Buttons Picture Boxes The icons in the Tool Box represent objects that can be placed on the form. The four objects discussed today are: text boxes….. Text Box: you use a text box primarily to get information, referred to as input , from the user Label is placed next to text box to tell the user what type of information to enter into the to he text box Command button : The user clicks a command button to initiate an action Picture Boxes: You use a picture box to display text or graphics Chapter 3 - Visual Basic Schneider hhhhhhh

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 Go over resizing and dragging the text box The first line of the properties window (called the object box) reads “Text1 TextBox” . Text1 is the current name of the textbox. The two Tab permit you to view the list of properties either alphabetically or grouped by categories.. We discuss four properties : The Text property determines the words in the text box. Press Shift+Ctrl+F to move to the first property beginning with F . Move to the property ForeColor. The foregroung color is the color of the text.. Click on Palette tab to display a selection of colors. Highlight the Font property and change it to Italic Chapter 3 - Visual Basic Schneider hhhhhhh

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

Some Useful Properties: Name Caption Border style Visible Back Color 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 Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Naming Objects: Use the Property window 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 hhhhhhh

Chapter 3 - Visual Basic Schneider Naming Objects: Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider 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”. When a VB program is run, a form and its controls appear on the screen. Normally, nothing happens until the user takes an action, such as clicking a control or pressing the Tab key. Such an action is called event. Chapter 3 - Visual Basic Schneider hhhhhhh

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

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. Object Property Setting frmWalkthrough Caption Demonstration txtPhrase Text (blank) cmdBold Caption Make Phrase Bold Object Box Procedure Box: Contains a list of all possible event procedures associated with the text box Choose txtPhrase_LostFocus() txtPhrase.Font.Size = 12 End Sub Choose GotFocus and type: txtPhrase.Font.Size = 8 txtPhrase.Font.Bold = False Go to command Button and choose cmdBold_Click and type txtPhrase.Font.Bold = True Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Example of An Event Private Sub objectName_event ( ) statements End Sub Private Sub txtOne_GotFocus( ) txtOne.Font.Size = 12 txtOne.Font.Bold = False The word Sub signals the beginning of the event procedure, and also identifies the Object and the event occurring to that object The word private indicates that the event procedure cannot be invoked by an event from another procedure. The word Sub means subprogram Example: the event procedure private Sub cmdButton_Click () txtBox.Text = “” End Sub The event clicking cmdButton is different from the event clicking picBox_Click Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider More Example Private Sub cmdButton_Click( ) txtBox.ForeColor = vbRed txtBox.Font.Size = 24 txtBox.Text = “Hello” End Sub Each color can be identified by a sequence of digits and letter beginning with &H. &HFF& = Red &HFF00& = Green &HFF000& = Blue Chapter 3 - Visual Basic Schneider hhhhhhh

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

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

Rules for Creating Variable Names Must begin with a letter. Can contain letters, numeric digits. Can have up to 255 characters. Can Not be restricted keyword. 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 Chapter 3 - Visual Basic Schneider hhhhhhh

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

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

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

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

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

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

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

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

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 hhhhhhh

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

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 hhhhhhh

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

Chapter 3 - Visual Basic Schneider Keywords Words that have predefined meaning to Visual Basic . Can Not be used as variable names. Example: Print Cls If While The VB editor automatically capitalizes the first letter of reserved word Chapter 3 - Visual Basic Schneider hhhhhhh

Visual Basic Print Statement 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 . Chapter 3 - Visual Basic Schneider hhhhhhh

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 hhhhhhh

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

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

Chapter 3 - Visual Basic Schneider Output 10 3 Chapter 3 - Visual Basic Schneider hhhhhhh

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 hhhhhhh

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 hhhhhhh

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

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

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

Chapter 3 - Visual Basic Schneider String Variables 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). The value of string variable is assigned or altered with Let statements and displayed with Print methods just like the Chapter 3 - Visual Basic Schneider hhhhhhh

Example of String Variable 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 hhhhhhh

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

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

Example of Concatenation txtBox.Text = “32” & CHR(176) & “ Fahrenheit” Displays 32 Fahrenheit in the text box Chapter 3 - Visual Basic Schneider hhhhhhh

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

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 From now on we will declare all variables. Declaring variables is regarded as good programming practice. Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider 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 The default data type is a single precision numeric variable Therefore Number and Number! Are the same Double precision variable is used to store number with many digits A single precision variable is accurate to about seven decimal points where double precision is accurate to about 15 decimal points Is used when a high degree of accuracy is needed. If you try to assign a real number , the number would be cut off and an integer would be assigned to it. Declaring variables is regarded as good programming practice Example: Dim variableName As String Dim variableName As Single Type Declaration Tags Chapter 3 - Visual Basic Schneider hhhhhhh

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 hhhhhhh

Using Text Boxes for Input/Output Therefore, the contents of a text box should be changed to a number before being assigned to a numeric variable. Val (txtBox.Text) changes the input string into a number. Example: numVar = Val (txtBox.Text) Function Input Output Str number string Val string number If str is a string representation of a number, then Val(str) is that number. Conversely, if num is a number, then Str(num) is a string representation of the number. Therefore statements such as numVar = Val(txtBox.Text) and txtBox.Text = Str(numVar) Chapter 3 - Visual Basic Schneider hhhhhhh

Example (convert miles to furlong 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)) Chapter 3 - Visual Basic Schneider hhhhhhh

The KeyPress Event Procedure Private Sub txtCharacter_KeyPress(KeyAscii As Integer) txtCharacter.Text = "" picOutput.Cls picOutput.Print Chr(KeyAscii); " has ANSI value"; KeyAscii End Sub Chapter 3 - Visual Basic Schneider

Reading Data from Files 1. Choose a number to be the reference number to the file. 2. Set the mode in which the file is to be used: Input Output Append 3. Read the data sequentially using Input statement. 4. Close the file . 1. The name of the file: follows the same rules for naming program files. Use invest.bas and invest.dat 2. The mode in which the file is to be used , can be output, input, append . Output: indicates data is written to the file from the program input: the contents of the file are being read into the program append :allows new records to be added to an existing file. A butter is a reserved part of the primary storage unit used a s temporary storage area for the data that is being written or read from a file Chapter 3 - Visual Basic Schneider hhhhhhh

Example of Reading from a File: Open the file Open “DATA.TXT” for Input As #1 Input #1, num1 Input #1, num2 picOutput.Print num1+num2 Close #1 Reference number Read from the file Read the data and assign it to num1 A file can have either one item per line or many items (separated by commas) can be listed on the same line the items of data will be assigned to variables one at the time in the order they appear in the file. 1. Choose a number from 1 to 255 to be the reference number for the file 2. Open the file for input 3. Read items of data in order, one at the time, from the file with Input sattement Show Examples: 3.5.4 Path is C;\F98 CSE181\Examples\Ch3\Data.txt The response typed into an input box is treated as a single string value, no matter what is typed. Quotation marks are not needed, and if included, are considered as part of the string. Numeric data typed into n input box should be converted to a number with Val before it is assigned to a numeric variable or used in calculation. Close the file Chapter 3 - Visual Basic Schneider hhhhhhh

Example of Reading from a File: Open “Data.txt” for Input As #1 Input #1,num1, num2 picOutput.Print num1+num2 Close #1 Example #1: create a file named Student.dat Prepare the file to receive data If a file already exists, it is destroyed and a new file is created. Associated with the file Student.date with buffer #1. As long as the file is open. Buffer #1 is used temporarily before the data is written to the file on disk Other statement in the program use this number to identify the file.. Chapter 3 - Visual Basic Schneider hhhhhhh

Input from an Input Box: Use Text Box to obtain input. For one piece of input use input box instead of a text box Input Box is a predefined dialog box. After users types in a response into the rectangle at the bottom of the screen and press Enter, the response is assigned to the string variable. Normally, text box is used to obtain input, sometimes we want just one piece of input and would rather not have a text box and label stay on the screen for ever. Example 3.5.4 Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Syntax for an Input Box stringVar = InputBox (prompt, title) Where the prompt is the message you want displayed inside the dialog box, and title is the text you want displayed in the dialog box’s title bar. Both the prompt and the tittle must be enclosed in quotation marks. Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Example of Input Box Private Sub cmdDisplay_Click() Dim fileName As String, prompt As String, title As String Dim houseNumber As Single, street As String prompt = "Enter the name of the file containing the information." title = "Name of File" fileName = InputBox(prompt, title) Open fileName For Input As #1 Input #1, houseNumber Input #1, street picAddress.Print "The White House is at"; houseNumber; street Close #1 End Sub After executing an input box would pop up Chapter 3 - Visual Basic Schneider

Using Message Box for Output: Use message box to get the user’s attention. Message box is a predefined dialog box too. Chapter 3 - Visual Basic Schneider

Chapter 3 - Visual Basic Schneider Syntax for Message Box MsgBox prompt, , title When a statement of the form above is executed , where prompt and title are strings, message box with prompt displayed and the title bar caption title appears., and stays on the screen until the user presses Enter or clicks K. Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Example of Message Box MsgBox “Nice try, but no cigar”, , “Consolation” Stays on the screen until the user presses OK Chapter 3 - Visual Basic Schneider

Formatting the Output: Create user friendly output. In the Print method, control of the spacing of the output is controlled by the following devices. Chapter 3 - Visual Basic Schneider hhhhhhh

Formatting the Output: Semicolon Comma Tab Function Chapter 3 - Visual Basic Schneider

Chapter 3 - Visual Basic Schneider Semicolons The next value output is placed in the next column position. Example: picOutput.Print “Patrick”; ”Jon” Output Screen: PatrickJon Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Example of Semicolon picOutput.Print “Patrick”; ” Jon” Output Screen: Patrick Jon Space here Space here Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Example of Semicolon picOutput.Print 100; -200; 300 Output Screen: 100 -200 300 Two spaces One space Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Commas The next value output is placed in the next available print zone. Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Print Zones Each print zone is 14 positions wide. Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Example of Print Zone Example: picOutput.Print “SEE”, ”YOU”, ”SOON” Output Screen: SEE YOU SOON See in column 1 Y in column 15 and S in column 29 Column 29 Column 15 Column 1 Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Example of Commas A print zone can be skipped by typing consecutive commas Example: picOutput.Print “HOURLY”, , “PAY” Output Screen: HOURLY PAY Example 3.5.5 Column 29 Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Tab Function Starts output in the specified column. It provides more flexibility in formatting. Only use Semicolons with the Tab function. Only can be used to advance the print position. Chapter 3 - Visual Basic Schneider hhhhhhh

Example of Tab Function picOutput.Print Tab(3); “Hi there!” ;TAB(25) ;“Bye!” Output Screen: Hi there! Bye! Example 3.5.6 Organizes data into columns of a table Column 25 Column 3 Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Example of Tab Example: picOutput.Print TAB(25); 5; TAB(15); 4; TAB(5); 3 Output Screen: 5 4 3 Column 25 Column 15 Column 5 Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Functions: What is a function? What are advantages of using functions? How do you use a function? Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider What is a function A sub program designed to perform a specific task. A sub program designed to return a single value to the calling program. Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Types of Functions Built-In functions (library) User-defined functions Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Example x = Sqr(225) y = Int (2.7) str1 = Left (“John Smith”, 4) number = Rnd Chapter 3 - Visual Basic Schneider hhhhhhh

Types of Standard Functions Numeric Functions (manipulate numbers) String Functions (manipulate strings) Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Numeric Functions Example 3.6.2 for Int function and Sqr function Example 3.6.4 : rounds a positive number to the nearest integer.. 3.6 -- 4 45.8 - 46 6.1 - 6 6.499 - 6 6.5 - 7 To round a number t two decimal places r = Int (100*n + 0.5) / 100 Chapter 3 - Visual Basic Schneider hhhhhhh

Example of Numeric Functions Private Sub cmdEvaluate_Click() Dim n As Single, root As Single picResults.Cls n = 6.76 root = Sqr(n) picResults.Print root; Int(n); Round(n,1) End Sub Chapter 3 - Visual Basic Schneider

Commonly-Used String Functions Function: Left (“Penguin”,4) Purpose: Returns the number of specified characters, starting at the beginning of the string. Chapter 3 - Visual Basic Schneider hhhhhhh

Commonly-Used String Functions Function: Right (“Gotham City” , 4) Purpose: Returns the number of specified characters from the end of the string . Chapter 3 - Visual Basic Schneider hhhhhhh

Commonly-Used String Functions Function: Mid (“Commissioner” , 4, 3) Purpose: Returns the character string starting at the position indicated by the first number and continuing for the length specified by the second number. Chapter 3 - Visual Basic Schneider hhhhhhh

Commonly-Used String Functions Function: UCase (“Yes”) Purpose: Converts any lowercase letters in string to uppercase. Go over Example 6 page 121 Chapter 3 - Visual Basic Schneider hhhhhhh

String-Related Numeric Functions Function: InStr (“John Smith”, ” “) Purpose: Searches for the first occurrence of one string in another and gives the position at which the string is found. Chapter 3 - Visual Basic Schneider hhhhhhh

String-Related Numeric Function Function: Len (“John Smith”) Purpose: Returns the number of characters in the string. Is 10 Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Format Function The format functions provide detailed control of how numbers, dates, and strings are displayed. Numbers can be made to line up uniformly and be displayed with dollar signs, commas and a specified number of decimal places. Dates can be converted to a long or medium form. Strings can be right justified. Chapter 3 - Visual Basic Schneider hhhhhhh

Examples of Format Functions FormatNumber (12345.678, 1) 12,345.6 FormatCurrency (12345.678, 2) $12,345.68 FormatPercent (.185, 2) 18.50% FormatNumber (1 + Sqr(2), 3) 2.414 Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Format Function Format (expr, “@……..@”) Purpose: The value of this function contains the string right justified in a field of n spaces. Where n is a string of n @ symbols. If num is number , numeric expression , or a string representation of a number, and fmt is a s string of n @ symbols, then Format (num,fmt) is asserting of character with the number right justified in a field of n spaces. If num is any string,produced by a Format function , then the value of Format (num,fmt) contains the string right-justified in a field of n spaces. Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Examples: Format (12345, “@@@@@”) 12345 Format (123, “@@@@@”) 123 Format (“123.4”, “@@@@@”) 123.4 Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Example FormatDateTime (“9-15-99”, vbLongDate) Output: Wednesday, September 15, 1999 String Value: Sunday, September20, 1998 09-Sep-98 Chapter 3 - Visual Basic Schneider hhhhhhh

Chapter 3 - Visual Basic Schneider Rnd Function Returns a random number from 0 up to 1. (excluding 1). Example: Displays a random integer from 1 through 6. picBox.Print Int(6 * Rnd) + 1 Chapter 3 - Visual Basic Schneider

Examples of Using Rnd Function: An integer from 1 through 100? A number from 2 through 4 (excluding 4)? An even integer from 2 through 100 ? Either 0 or 1? Int(100* Rnd) + 1 @ * Rnd + 2 (Int (50 * Rnd) + 1) *2 Int( 2* Rnd) Chapter 3 - Visual Basic Schneider hhhhhhh