Transcript Files

Files

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.

Displaying Multiple Buttons

• Use MessageBoxButtons constants to display more than one button in the Message Box.

• Message Box's Show method returns a DialogResult can be checked to see which button the user clicked.

object that • Declare a variable to hold an instance of the DialogResult capture the outcome of the Show method.

type to

Message Box - Multiple Buttons

MessageBoxButtons.YesNo

Declaring an Object Variable for the Method Return

Dim

whichButtonDialogResult

As DialogResult

whichButtonDialogResult

= MessageBox.Show _ If ("Clear the current order figures?", "Clear Order", _ MessageBoxButtons.YesNo, MessageBoxIcon.Question)

whichButtonDialogResult

' Code to clear the order.

= DialogResult.Yes Then End If

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 .

Data Hierarchy

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.

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: 1.

Employee identification number 2.

3.

4.

5.

Name Address Hourly pay rate Number of exemptions claimed 6.

7.

Year-to-date earnings Amount of taxes withheld

Data Hierarchy

Sequential Files

A common organization is called a sequential file are stored in order by a record-key field.

in which records typically In a payroll file, records usually are placed in order by employee identification number.

Databases

or example, 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) .

File I/O

• Reading and writing data in a disk file • Writing =

Output

• Reading =

Input

Files and Streams

Visual Basic views a file simply as a sequential stream bytes.

of Depending on the operating system, each file ends either with an end-of-file marker structure for the file.

or at a specific byte number that’s recorded in a system-maintained administrative data

Writing Data Sequentially to a Text File

Before we can implement the Credit Inquiry from which that app will read records.

app, we must create the file Our first program builds the sequential file containing the account information for the company’s clients.

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.

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.

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.

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.

File Edit View Format Help

Building Menus with the Windows Forms Designer

Most menus and menu items provide access shortcuts (or keyboard shortcuts ) that allow users to open a menu or select a menu item by using the keyboard.

For example, most apps allow you to open the File

F.

menu by typing

Alt +

The letter that’s used as the shortcut is underlined in the GUI when you press the

Alt key.

To specify the shortcut key, type an ampersand ( & ) before the character to be underlined—so &File underlines the F in File .

Building Menus with the Windows Forms Designer

Creating Event Handlers for the Menu Items

Like Button s, menu items have Click events that notify the program when an item is selected.

To create the event handler for a menu item so the app can respond when the menu item is selected, double click the menu item in the Windows Forms Designer then insert your event handling code in the new method’s body.

In fact, the same event handler method can be used for Button s and menu items that perform the same task.

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 the System.IO

indicates that we’re using classes from namespace .

This namespace contains stream classes such as StreamWriter (for text output) and StreamReader text input).

(for

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.

Displaying the Records

To access the record’s data, we need to break the String its separate fields.

into 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 String s representing the tokens, which we assign to array variable fields .

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 Finally in a block, we ensure that the file will always be closed properly. Local variables in a Try corresponding Finally block cannot be accessed in the 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 StreamReader block, as we did with the variable.

Displaying the Records

Relationship Between the

Using

and

Try

Statements

The Using a Try statement is actually a shorthand notation for statement with a Finally block.

For example, the Using following code Dim fileChooser As New statement is equivalent to the OpenFileDialog() Try result = fileChooser.ShowDialog() ' get specified file name fileName = fileChooser.FileName Finally fileChooser.Dispose() End Try