Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application."— Presentation transcript:

1 © 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

2 © 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.

3 © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test Driving the Ticket Information Application

4 © 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

5 © 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.

6 © 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

7 © 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)

8 © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Data Hierarchy Figure 24.3 Data hierarchy.

9 © 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.

10 © 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

11 © 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.

12 © 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

13 © 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.

14 © 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.

15 © 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

16 © 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

17 © 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

18 © 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

19 © 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

20 © 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

21 © 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

22 © 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

23 © 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

24 © 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

25 © 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.

26 © 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.

27 © 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

28 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

29 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

30 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

31 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

32 © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application

33 © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application

34 © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application

35 © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure MonthCalendar template applications Form.

36 © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure System.IO namespace imported to FrmEvents.

37 © 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

38 © 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

39 © 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

40 © 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

41 © 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

42 © 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

43 © 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

44 © 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

45 © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Building the Ticket Information Application Figure ExtractData methods variable declarations.

46 © 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

47 © 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

48 © 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

49 © 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.

50 Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 50 TicketInformation-.vb (1 of 6) Importing namespace System.IO

51 Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 51 TicketInformation-.vb (2 of 6)

52 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

53 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

54 Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 54 TicketInformation-.vb (5 of 6)

55 Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 55 TicketInformation-.vb (6 of 6)


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application."

Similar presentations


Ads by Google