© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application 24.2 Data Hierarchy 24.3 Files and Streams 24.4 Writing to a File: Creating the Write Event Application 24.5 Building the Ticket Information Application 24.6 Wrap-Up Tutorial 24 – Ticket Information Application Introducing Sequential-Access Files
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Create, read from, write to and update files. –Understand a computers data hierarchy. –Become familiar with sequential-access file processing. –Use StreamReader and StreamWriter classes to read from, and write to, sequential-access files. –Add and configure a MonthCalendar control.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test Driving the Ticket Information Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test Driving the Ticket Information Application Figure 24.1 Ticket Information applications GUI. Arrow buttons allow user to scroll through months MonthCalendar control ComboBox lists any events TextBox displays event details
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test Driving the Ticket Information Application 18th day of the month selected No events displayed 19th day of the month selected Event information displayed Figure 24.2 Ticket Information application displaying event information.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Data Hierarchy Data –Data has many forms, such as letters –Letters, numbers, and some special symbols are characters –The set of all characters that represent data is known as the character set –Data is broken down by a computer into bits, or binary digits –Binary digits can either be 1 or 0 –Visual Basic.NET uses Unicode characters, which means every character is 2 bytes (16 bits) –Fields are composed of characters –Records are groups of related fields –Files are groups of related records
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Data Hierarchy Data (continued) –Record keys distinguish a given record from all other records –Records are often stored in order of their record keys in a file known as a sequential file –Related files are often stored in databases –Databases are managed by programs in a database management system (DBMS)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Data Hierarchy Figure 24.3 Data hierarchy.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Files and Streams n-1... end-of-file marker Figure 24.4 Visual Basic.NETs conceptual view of an n-byte file.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Adding a dialog to open or create a file –Select OpenFileDialog in the Windows Forms tab of the Toolbox –The property FileName is the file the application reads information from –If CheckFileExists is False, the user is allowed to specify a file name –A new file is created and opened if a user specifies a file name that does not exist
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application OpenFileDialog control Figure 24.5 OpenFileDialog added and renamed.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Determining if a file name is valid –Method CheckValidity receives a file name and returns True if it is valid –Method EndsWith receives a filename and returns True if it ends with a.txt extension –If the file name is valid, the Enter and Close File Button s will be enabled
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application CheckValidity Function procedure header Figure 24.6 Method CheckValidity header.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Displaying error message if incorrect file type is provided Figure 24.7 Displaying an error message indicating an invalid file name.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Figure 24.8 Changing the GUIs appearance if a valid file name is entered. Enabling and disabling Button s
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application StreamWriter –Used to create objects for writing text to a file –DialogResult determines if Cancel Button was clicked and the method should therefore exit. –StreamWriter takes two arguments Name of the file to write to A Boolean value to determine if StreamWriter will append information on the end of the file
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Figure 24.9 System.IO namespace imported into FrmWriteEvent class. Importing namespace System.IO
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Figure Declaring a StreamWriter object. Declaring StreamWriter
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Figure Write Event application Form in design view. Open File… Button
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Figure Displaying the Open dialog and retrieving the result. Displaying Open dialog If the user clicks Cancel the event handler exits Setting variable to user- specified file name
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Figure Validating the file name and initializing a StreamWriter object. Check for valid file name Create StreamWriter object
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Figure Clearing user input. Clearing user input
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Figure StreamWriter writing to a file. Writing information to file
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Figure Closing the StreamWriter. Closing StreamWriter object
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Figure Write Event application executing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Figure Open dialog displaying contents of the template Ticket Information applications bin folder.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Writing to a File: Creating the Write Event Application Figure Sequential-access file generated by Write Event application. Day and time of event, ticket price, event name and description
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28 WriteEvent.vb (1 of 4) Importing a namespace System.IO StreamWriter that will write text to a file
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 WriteEvent.vb (2 of 4) Retrieve user input from Open dialog Storing filename entered by user
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30 WriteEvent.vb (3 of 4) Create StreamWriter object to associate a stream with the user-specified text file
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31 WriteEvent.vb (4 of 4) Append data to end of file Closing the files associated stream
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure MonthCalendar template applications Form.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure System.IO namespace imported to FrmEvents.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Building the Ticket Information application –Array m_strData stores the event information read from the file –m_intNumberOfEvents stores the number of events for a given day
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure Instance variables declared in the Ticket Information application. Creating an array of String s
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure Load event handler calling method CreateEventList. You will add code to CreateEventList later
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure MonthCalendar s DateChanged event handler. Calling the CreateEventList method
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application CreateEventList –ExtractData is passed the selected date to do two actions Store event information in array m_strData assign the number of events scheduled for the specified date to m_intNumberOfEvents –Informs user whether there are events scheduled for a given day
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure CreateEventList modified to call method ExtractData and clear the ComboBox. You will add code to ExtractData in the next box
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure Displaying the events scheduled for the specified day. Extracting event name from array and displaying it in the ComboBox Indicating that events are scheduled for the day Indicating that no events are scheduled for the day
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application ExtractData used to read from a sequential file –A Do While…Loop is used to ensure that there are still characters on the file –For every entry in the file, the date is read by StreamReader and then converted to the Integer intFileDay –If the date read from the file is the same as the specified date, then the rest of the event information will be read
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure ExtractData methods variable declarations.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure Using StreamReader to read data from a sequential-access file. Creating a StreamReader object to read the calendar.txt file
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure Extracting the day from an event entry in the file. Verify that end of file has not been reached and less than 10 events are stored in the array
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure Sequentially reading event entries from the file. Store event information in a row of the array
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure cboEvent_SelectedIndexChanged defined to display event information. Displaying event information in the TextBox.
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 50 TicketInformation-.vb (1 of 6) Importing namespace System.IO
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 51 TicketInformation-.vb (2 of 6)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 52 TicketInformation-.vb (3 of 6) Creating StreamReader object Using method ReadLine to read the first line of the file Ensuring that the end of the file has not been reached
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 53 TicketInformation-.vb (4 of 6) Reading information from a file and storing the data in an array Using method ReadLine to skip to the next event in the file Using method ReadLine to read the day of the next event
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 54 TicketInformation-.vb (5 of 6)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 55 TicketInformation-.vb (6 of 6)