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 1992-2004 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 1992-2004 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 24.1Test Driving the Ticket Information Application

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 24.1Test 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 24.1Test 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 24.2Data 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 24.2Data 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 24.2Data Hierarchy Figure 24.3 Data hierarchy.

9 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 24.3Files and Streams 0123456789n-1... end-of-file marker Figure 24.4 Visual Basic.NETs conceptual view of an n-byte file.

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 24.4Writing 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 24.4Writing to a File: Creating the Write Event Application OpenFileDialog control Figure 24.5 OpenFileDialog added and renamed.

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 24.4Writing 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 24.4Writing to a File: Creating the Write Event Application CheckValidity Function procedure header Figure 24.6 Method CheckValidity header.

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 24.4Writing 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 24.4Writing 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 24.4Writing 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 24.4Writing to a File: Creating the Write Event Application Figure 24.9 System.IO namespace imported into FrmWriteEvent class. Importing namespace System.IO

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 24.4Writing to a File: Creating the Write Event Application Figure 24.10 Declaring a StreamWriter object. Declaring StreamWriter

19 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 24.4Writing to a File: Creating the Write Event Application Figure 24.11 Write Event application Form in design view. Open File… Button

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 24.4Writing to a File: Creating the Write Event Application Figure 24.12 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 24.4Writing to a File: Creating the Write Event Application Figure 24.13 Validating the file name and initializing a StreamWriter object. Check for valid file name Create StreamWriter object

22 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 24.4Writing to a File: Creating the Write Event Application Figure 24.14 Clearing user input. Clearing user input

23 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 24.4Writing to a File: Creating the Write Event Application Figure 24.15 StreamWriter writing to a file. Writing information to file

24 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 24.4Writing to a File: Creating the Write Event Application Figure 24.16 Closing the StreamWriter. Closing StreamWriter object

25 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 24.4Writing to a File: Creating the Write Event Application Figure 24.17 Write Event application executing.

26 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 24.4Writing to a File: Creating the Write Event Application Figure 24.18 Open dialog displaying contents of the template Ticket Information applications bin folder.

27 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27 24.4Writing to a File: Creating the Write Event Application Figure 24.19 Sequential-access file generated by Write Event application. Day and time of event, ticket price, event name and description

28 Outline © Copyright 1992-2004 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 1992-2004 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 1992-2004 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 1992-2004 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32 24.5Building the Ticket Information Application

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

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

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

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

37 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 37 24.5Building 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38 24.5Building the Ticket Information Application Figure 24.24 Instance variables declared in the Ticket Information application. Creating an array of String s

39 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 24.5Building the Ticket Information Application Figure 24.25 Load event handler calling method CreateEventList. You will add code to CreateEventList later

40 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 24.5Building the Ticket Information Application Figure 24.26 MonthCalendar s DateChanged event handler. Calling the CreateEventList method

41 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 41 24.5Building 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 42 24.5Building the Ticket Information Application Figure 24.27 CreateEventList modified to call method ExtractData and clear the ComboBox. You will add code to ExtractData in the next box

43 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 43 24.5Building the Ticket Information Application Figure 24.28 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 44 24.5Building 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 45 24.5Building the Ticket Information Application Figure 24.29 ExtractData methods variable declarations.

46 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 46 24.5Building the Ticket Information Application Figure 24.30 Using StreamReader to read data from a sequential-access file. Creating a StreamReader object to read the calendar.txt file

47 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 47 24.5Building the Ticket Information Application Figure 24.31 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 48 24.5Building the Ticket Information Application Figure 24.32 Sequentially reading event entries from the file. Store event information in a row of the array

49 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 49 24.5Building the Ticket Information Application Figure 24.33 cboEvent_SelectedIndexChanged defined to display event information. Displaying event information in the TextBox.

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

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

52 Outline © Copyright 1992-2004 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 1992-2004 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 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 54 TicketInformation-.vb (5 of 6)

55 Outline © Copyright 1992-2004 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