Presentation is loading. Please wait.

Presentation is loading. Please wait.

Files.

Similar presentations


Presentation on theme: "Files."— Presentation transcript:

1 Files

2 Modal versus Modeless Windows
A dialog box is said to be modal when it stays on top of the application and must be responded to. Use the ShowDialog method to display a dialog box — it is a window displayed modally. Modeless windows do not demand that there is a response. Use the Show method to display a modeless window. A modeless window can be ignored by the user. You have probably noticed that when you display a Windows dialog box, it remains on top until you respond to it. But, in many applications, you can display additional windows and switch back and forth between windows.

3 Displaying Multiple Buttons
Use MessageBoxButtons constants to display more than one button in the Message Box. Message Box's Show method returns a DialogResult object that can be checked to see which button the user clicked. Declare a variable to hold an instance of the DialogResult type to capture the outcome of the Show method.

4 Message Box - Multiple Buttons
Display Yes and No buttons on a message box using MessageBoxButtons. YesNo constant. The Show method returns a DialogResult object that you can check to see which button the user clicked. MessageBoxButtons.YesNo

5 Declaring an Object Variable for the Method Return
Dim whichButtonDialogResult As DialogResult whichButtonDialogResult = MessageBox.Show _ ("Clear the current order figures?", "Clear Order", _ MessageBoxButtons.YesNo, MessageBoxIcon.Question) If whichButtonDialogResult = DialogResult.Yes Then ' Code to clear the order. End If To capture the information about the outcome of a method, variables must be declared so it can hold an instance of the DialogResult type.

6 Menus Menu Bar Contains menus which drop down to display list of menu items Can be used in place of or in addition to buttons to execute a procedure Menu items are controls with properties and events. Easy to create menus for a Windows form using the Visual Studio environment’s Menu Designer Menus will look and behave like standard Windows menus. Menus are used quite extensively while working with the computer.

7 Defining Menus (1 of 2) MenuStrip component is added to a form.
MenuStrip is a container to which ToolStripMenuItems, ToolStripComboBoxes, ToolStripSeparators, and ToolStripTextBoxes can be added.

8 Defining Menus (2 of 2) The MenuStrip component
appears in the component tray below the form and the Menu Designer allows you to begin typing the text for the menu items. To create the menus, simply type where the words "Type Here" appear at the top of the form. Each time the text for a new menu is entered, a ToolStripMenuItem is automatically added to the MenuStrip’s Items collection.

9 Standards for Windows Menus
Follow Windows standards for applications. Include keyboard access keys. Use standards for shortcut keys, if used. Place the File menu at left end of menu bar and end File menu with the Exit command. Help, if included, is placed at right end of menu bar. When applications are written to run under Windows, programs should follow the Windows standards. Plan menus so that they look like other Windows programs. Menu example File Edit View Format Help

10 Introduction Variables and arrays offer only temporary storage of data in memory— the data is lost, for example, when a local variable “goes out of scope” or when the app terminates. By contrast, files are used for long-term retention of large (and often vast) amounts of data, even after the app that created the data terminates, so data maintained in files is often called persistent data.

11 Data Hierarchy Data items processed by computers form a data hierarchy in which data items become larger and more complex in structure as we progress up the hierarchy from bits to characters to fields to larger data aggregates.

12 Data Hierarchy Records
Typically, a record is composed of several related fields. In a payroll system, for example, a record for a particular employee might include the following fields: Employee identification number Name Address Hourly pay rate Number of exemptions claimed Year-to-date earnings Amount of taxes withheld

13 Data Hierarchy Sequential Files Databases
A common organization is called a sequential file in which records typically are stored in order by a record-key field. In a payroll file, records usually are placed in order by employee identification number. Databases A company might have payroll files, accounts receivable files (listing money due from clients), accounts payable files (listing money due to suppliers), inventory files (listing facts about all the items handled by the business) and many other files. Related files often are stored in a database. A collection of programs designed to create and manage databases is called a database management system (DBMS).

14 File I/O Reading and writing data in a disk file Writing = Output
Reading = Input 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.

15 Files and Streams Visual Basic views a file simply as a sequential stream of bytes. Depending on the operating system, each file ends either with an end- of-file marker or at a specific byte number that’s recorded in a system- maintained administrative data structure for the file.

16 Writing Data Sequentially to a Text File
Before we can implement the Credit Inquiry app, we must create the file from which that app will read records. Our first program builds the sequential file containing the account information for the company’s clients.

17 Class CreateAccounts Framework Class Library classes are grouped by functionality into namespaces, which make it easier for you to find the classes needed to perform particular tasks. Imports statement indicates that we’re using classes from the System.IO namespace. This namespace contains stream classes such as StreamWriter (for text output) and StreamReader (for text input).

18 Managing Resources with the Using Statement
The Using statement, can simplify writing code in which you obtain, use and release a resource. In this case, the resource is a SaveFileDialog. Windows and dialogs are limited system resources that occupy memory and should be returned to the system (to free up that memory) as soon as they’re no longer needed. In a long-running app, if resources are not returned to the system when they’re no longer needed, a resource leak occurs and the resources are not available for use in this or other app.

19

20 Displaying the Records
To access the record’s data, we need to break the String into its separate fields. This is known as tokenizing the String. The String.Split method breaks the line of text into fields using a delimiter as an argument. In this case, the delimiter is the character literal ","c—indicating that the delimiter is a comma. A character literal looks like a String literal that contains one character and is followed immediately by the letter c. Method Split returns an array of Strings representing the tokens, which we assign to array variable fields.

21 Displaying the Records
This Finally block is an ideal location to place resource release code for resources that are acquired and manipulated in the corresponding Try block (such as files). By placing the statement that closes the StreamReader in a Finally block, we ensure that the file will always be closed properly. Local variables in a Try block cannot be accessed in the corresponding Finally block. For this reason, variables that must be accessed in both a Try block and its corresponding Finally block should be declared before the Try block, as we did with the StreamReader variable.


Download ppt "Files."

Similar presentations


Ads by Google