210 likes | 226 Views
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.
E N D
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 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.
Message Box - Multiple Buttons MessageBoxButtons.YesNo
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
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 FormatHelp
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 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: • Employee identification number • Name • Address • Hourly pay rate • Number of exemptions claimed • Year-to-date earnings • Amount of taxes withheld
Data Hierarchy Sequential Files • 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).
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 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.
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.
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).
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 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.
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.