18. Button Click Event Procedure The user enters data into the text boxes and clicks the Save button, which writes the data from the screen to the StreamWrite object and clears the screen
19.
20. Closing a File - Example Private Sub exitButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles exitButton.Click ' Close the file and the form. phoneStreamWriter.Close( ) Me.Close( ) End Sub
31. Open File to Read Dialog Box Files of Type Determined by Filter Property
32.
33. Using Nothing Keyword ' Is the file already open? If Not phoneStreamWriter IsNot Nothing Then phoneStreamWriter . Close( ) End If Must use Keyword Is rather than equal sign
34.
35.
36.
37.
38.
39.
40.
41.
Editor's Notes
Brief description on how to navigate within this presentation (ppt) The first time a Key Term from the chapter is used in the ppt it will display in blue Gold colored text boxes display coding examples Slides will be numbered (# of #) when multiple slides on same topic (Slide title) Speaker notes are included where appropriate for slides (*)Denotes either a comment for page reference to textbook or slide reference in ppt
Projects contain many objects such as forms and controls
Declaring an object variable does not create the object-it only creates the identifier (name) and object type
Data can be read and written to a disk—user entering data into text boxes that will be stored in a file is called writing or output; retrieving data from the file is reading or input In VB, the simplest way to read and write small amounts of data is to the StreamReader and StreamWriter objects Usually the StreamWriter code is written first to create the data file and then the StreamReader code to read the file that has been created
Declaring a new StreamWriter object opens the file—the file must be open before it can written to—if a file does not exist a new one is created If a full path is not used in the filename, then instantiate the StreamWriter object in a Try/Catch block in case the path does not exist
*The differences between Write and WriteLine methods are discussed in the next slide
If inputting data from the user and writing in a file, generally the WriteLine is placed in a button click event procedure so that one record at a time can be written
When finished writing data in a file the file must be closed
After running a project it can be viewed--
StreamReader Class is used to read the data from a file that is created with a StreamWriter
Use the StreamReader’s Peek method to tell when there is no more data in a file The ReadLine method does not throw an exception when you attempt to read past the end of a file
*The previous data file examples included hard-coded file names (and paths) *Refer to the table on p. 432 for the OpenFileDialog Component Properties
The user may click on the Cancel button of the Open File dialog box—check the DialogResult for Cancel If the user does click Cancel, that presents one more tasks for the program: A StreamWriter object that isn’t open cannot be closed
*The next slide provides an example of using the correct syntax
Beginning with an empty list is a good way to crate the file in the first place
Standard practice in programming is to refer to the data as “dirty” if changes have been made—thus the name for the variable is DirtyBoolean Just before the project ends, check the value of isDirtyBoolean; if True, ask the user if he or she wants to save; if False, just exit without a message