Presentation is loading. Please wait.

Presentation is loading. Please wait.

Input and Output.

Similar presentations


Presentation on theme: "Input and Output."— Presentation transcript:

1 Input and Output

2 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

3 Input

4 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

5 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

6 Code 1/2/2019 Input and Output

7 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

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

9 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

10 Output

11 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

12 Formatting Output with Format Functions
String Value FormatNumber( ,1) FormatNumber(1 + 2) 12,345.6 3.00 FormatCurrency( ,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

13 Code 1/2/2019 Input and Output

14 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( , 1)) lstDisplay.Items.Add(FormatNumber(1 + 2)) lstDisplay.Items.Add(FormatCurrency( ,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

15 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

16 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

17 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} {1,12:N0} 34.6 34 {1,12:C1} $1234.6 {1,-12:P} 0.569 56.90% 1/2/2019 Input and Output

18 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

19 Code 1/2/2019 Input and Output

20 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

21 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

22 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


Download ppt "Input and Output."

Similar presentations


Ads by Google