Input and Output.

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

Chapter 11 Data Files Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
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:
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Fundamentals of Programming in Visual Basic
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
Additional loop presentation
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 31 Fundamentals of Programming in Visual Basic (Continue VI) String Properties and Methods: "Visual".Length is 6. "Visual".ToUpper is VISUAL. "123.
Input/Output CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
CS0004: Introduction to Programming Input and Output.
Intrinsic Functions Pre-coded Functions Used to improve developer productivity Broad Range of Activities Math calculations Time/Date functions String.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Using Arrays and File Handling
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.
Chapter 3 - VB 2005 by Schneider1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers.
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.
Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.
Creating Sequential Files, Adding Data, & Deleting Data.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Microsoft Visual Basic 2008 CHAPTER TWELVE Cell Phone Applications and Web Services.
Input Textboxes Input Boxes Different than textboxes Good for small amount of input (form full of textboxes is not nice) X = Inputbox(“prompt message”,
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
Numbers continued The Integer Data Type Multiple Declarations Parentheses Three Types of Errors.
Chapter 8 - Visual Basic Schneider
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
Introduction to Programming Fundamentals of Programming in Visual Basic.
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.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 3: Chapter 3: Slide 1 Unit 3 Formatting Chapter 3 Input, Variables, Constants,
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Introduction to VB programming Dr. John P. Abraham UTPA Chapters 2 & 3.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
1 Displaying Dialog Boxes Kashef Mughal. 2 Midterm Stats Here we go  Average was  Low was 116  High was 184  Mid Quarter Grade - check any.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Visual Basic Fundamental Concepts
A variable is a name for a value stored in memory.
Chapter 3 – Fundamentals of Programming in Visual Basic
Chapter 3 – Variables, Input, and Output
Apply Procedures to Develop Message, Input, and Dialog Boxes
Introduction to VB programming
Files and Streams.
Chapter 8 – Sequential Files
Variables and Arithmetic Operations
Visual Basic..
Boolean Expressions and If statements
WEB PROGRAMMING JavaScript.
Fundamentals of Programming in VB.NET
Strings(Part 1) String Literal and String Variable
String Variable, Methods and Properties
Section 3.3 Numbers Arithmetic Operations Variables
Variable Review & IO User 12/26/2018.
Chapter 3.5 Input and Output
String Variable, Methods and Properties
Prepared By: Deborah Becker
Additional Topics in VB.NET
CIS16 Application Development and Programming using Visual Basic.net
String Variable, Methods and Properties
Introduction to Programming
String Variable, Methods and Properties
Input and Output.
Files and Streams.
Chapter 11 Saving Data and Objects In Files
Input and Output Chapter 3.5
Presentation transcript:

Input and Output

Input and Output Input Output Reading Data from Files Getting Input from an Input Dialog Box Formatting Output with Format Functions Formatting Output with Zones Using a Message Dialog Box for Output 1/2/2019 Input and Output

Input

Input data from a file Establish a communication link between computer and the disk drive for reading data from disk Dim readerVar As IO.StreamReader = _ IO.File.OpenText(filespec) Read data in order, one at a time, from the file with the ReadLine method assuming the file contains one item of data per line. strVar = readerVar.ReadLine terminate the communications link readerVar.Close() 1. Execute a statement of the form Dim readerVar As IO.StreamReader A StreamReader is an object from the Input/Output class that can read a stream of characters coming from a disk or coming over the Internet. The Dim statement declares the variable readerVar to be of type StreamReader. 2. Execute a statement of the form readerVar = IO.File.OpenText(filespec) where filespec identifies the file to be read. This statement establishes a communi-cations link between the computer and the disk drive for reading data from the disk. Data then can be input from the specified file and assigned to variables in the pro-gram. This assignment statement is said to “open the file for input.” Just as with other variables, the declaration and assignment statements in Steps 2 and 3 can be combined into the single statement Dim readerVar As IO.StreamReader = IO.File.OpenText(filespec) 3. Read items of data in order, one at a time, from the file with the ReadLine method. Each datum is retrieved as a string. A statement of the form strVar = readerVar.ReadLine causes the program to look in the file for the next unread line of data and assign it to the variable strVar. The data can be assigned to a numeric variable if it is first converted to a numeric type with a statement such as numVar = CDbl(readerVar.ReadLine) Note: If all the data in a file have been read by ReadLine statements and another item is requested by a ReadLine statement, the item retrieved will have the value Nothing. 4. After the desired items have been read from the file, terminate the communications link set in Step 3 with the statement readerVar.Close() 1/2/2019 Input and Output

Input Data From A File filespec – the path to find the file. A:\Grades.txt, C:\Grades.txt Default folder is bin folder Each datum is retrieved as a String The item retrieved will have value Nothing if it is the end of the file The data can be assigned to a numeric variable if converted into a numeric data type 1/2/2019 Input and Output

Code 1/2/2019 Input and Output

Code ' Read data from a file and display in a list box Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click ' Establish a communication link Dim sr As IO.StreamReader = IO.File.OpenText("Grades.txt") ' Read data line by line and display them lstDisplay.Items.Clear() lstDisplay.Items.Add(sr.ReadLine()) ' Terminate the communication link sr.Close() End Sub 1/2/2019 Input and Output

Input Dialog Box strVar = InputBox(prompt, title) 1/2/2019 Input and Output

Code 1/2/2019 Input and Output ' Read data from a file ans display in a list box Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click Dim fileName As String Dim sr As IO.StreamReader ' Get the file name from user fileName = InputBox("Enter the file name", "Enter file name") ' establish a communication link sr = IO.File.OpenText(fileName) ' Read data line by line, display them and the average lstDisplay.Items.Clear() lstDisplay.Items.Add(sr.ReadLine()) ' Terminate the communication link sr.Close() End Sub 1/2/2019 Input and Output

Output

Format Functions FormatNumber(n,r) FormatCurrency(n,r) FormatPercent(n,r) return a string value n - a number, an numeric expression or a string to be formatted r - the number of decimal places default value is 2 1/2/2019 Input and Output

Formatting Output with Format Functions String Value FormatNumber(12345.628,1) FormatNumber(1 + 2) 12,345.6 3.00 FormatCurrency(12345.628,2) FormatCurrency(-100) $12,345.63 ($100.00) FormatPercent(0.185,2) FormatPercent(“0.07”) 18.50% 7.00% 1/2/2019 Input and Output

Code 1/2/2019 Input and Output

Code ' Format data using format functions Private Sub btnFormat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFormat.Click lstDisplay.Items.Add(FormatNumber(12345.628, 1)) lstDisplay.Items.Add(FormatNumber(1 + 2)) lstDisplay.Items.Add(FormatCurrency(12345.628,2)) lstDisplay.Items.Add(FormatCurrency(-100)) lstDisplay.Items.Add(FormatPercent(0.185,2)) lstDisplay.Items.Add(FormatPercent("0.07")) End Sub 1/2/2019 Input and Output

Formatting Output with Zones Line up items in columns Use a fixed-width font such as Courier New Divide the characters into zones with a format string. 1/2/2019 Input and Output

Formatting output with zones Zone width Dim fmtStr As String = "{0, 15}{1, 10}{2, 8}“ lstOutput.Items.Add(String.Format(fmtStr, _ data0, data1, data2)) Zone number 1/2/2019 Input and Output

Formatting output with zones A colon and formatting symbol after width to specially format numeric data Zone Format Term Number to be formatted Number displayed {1,12:N3} 1234.5679 1234.568 {1,12:N0} 34.6 34 {1,12:C1} 1234.567 $1234.6 {1,-12:P} 0.569 56.90% 1/2/2019 Input and Output

Formatting Output with Zones Zone width left adjusted if preceded with minus sign, right adjusted otherwise Spaces between the successive pairs of brackets will be displayed in the corresponding zones in the output. 1/2/2019 Input and Output

Code 1/2/2019 Input and Output

Code 1/2/2019 Input and Output ' Read data from a file ans display in a list box Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click Dim fileName As String Dim sr As IO.StreamReader Dim fmtStr As String = "{0,-15}{1,15}{2,15}" ' Get the file name from user fileName = InputBox("Enter the file name", "Enter file name") ' Establish a communication link sr = IO.File.OpenText(fileName) ' Read data line by line, display them and the average lstDisplay.Items.Clear() lstDisplay.Items.Add(String.Format(fmtStr, "First Grade", "Second Grade", "Third Grade")) lstDisplay.Items.Add(String.Format(fmtStr, sr.ReadLine(), sr.ReadLine(), sr.ReadLine())) ' Terminate the communication link sr.Close() End Sub 1/2/2019 Input and Output

Using a Message Dialog Box for Output MsgBox(prompt,title) MsgBox(prompt, , title) is executed, where prompt and title are strings, a message dialog box appears with prompt displayed and the title bar caption title and stays on the screen until the user presses Enter, clicks on the box in the upper-right corner, or clicks OK. For instance, the state-ment MsgBox("Nice try, but no cigar.", , "Consolation") 1/2/2019 Input and Output

Code 'Message are sent before clearing the items in the list box Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox(" All items will be deleted", , "Delete items") lstDisplay.Items.Clear() End Sub 1/2/2019 Input and Output