Saving and Loading Simple Text Files A sequential

  • Slides: 16
Download presentation
Saving and Loading Simple Text Files A sequential file stores characters one after the

Saving and Loading Simple Text Files A sequential file stores characters one after the other like songs on a cassette tape. New characters are added to the end of the file. When a file is opened for reading, the characters are read one by one from the beginning of the file to its end. A random access file stores information in pieces called records. Each record is exactly the same length. If a record is 50 bytes long, the fourth record of the file is easily found by jumping over 150 bytes, the length of three records combined.

System. IO The first step in reading a simple text file and copying its

System. IO The first step in reading a simple text file and copying its contents into the text box on the form is to add an Imports System. IO statement to the top of the form class definition. This statement makes the Input/Output classes available to the project.

Step-by-Step 5. 8 Sub Load. File() Dim f As File = New File(Path. Name)

Step-by-Step 5. 8 Sub Load. File() Dim f As File = New File(Path. Name) Dim fs As Stream. Reader fs = f. Open. Text() txt. Content. Text = fs. Read. To. End fs. close() End Sub

Did You Know? You can get a device for your computer that acts like

Did You Know? You can get a device for your computer that acts like a remote keyboard. It looks like a simple laptop with a small LCD screen that displays several lines of text. While it doesn't run any programs and cannot handle graphics, it has enough memory to store many pages of text. It allows you to key and save the information to upload to your computer at a later time. When you are ready to transfer the data to your computer, it communicates with your computer through an infrared link connected to your keyboard cable. You open a word processor on your computer and spill the contents of your portable computer through the infrared link to the keyboard cable as if you were keying the text at that moment.

Step-by-Step 5. 8 'Message. Box. Show("Call the Open File dialog box. ") With Open.

Step-by-Step 5. 8 'Message. Box. Show("Call the Open File dialog box. ") With Open. File. Dialog 1. Default. Ext = "txt". Initial. Directory = "c: My Documents". Filter = "Text files | *. txt". Show. Dialog() Path. Name =. File. Name End With Menu. Item 3. Text = Menu. Item 2. Text Menu. Item 2. text = Path. Name Load. File()

Step-by-Step 5. 8 'Message. Box. Show("Call the Save File dialog box. ") With Save.

Step-by-Step 5. 8 'Message. Box. Show("Call the Save File dialog box. ") With Save. File. Dialog 1. Initial. Directory = "c: My Documents". Filter = "Text files | *. txt". Show. Dialog() Path. Name =. File. Name End With

Step-by-Step 5. 8 Dim f As File = New File(Path. Name) Dim fs As

Step-by-Step 5. 8 Dim f As File = New File(Path. Name) Dim fs As Stream. Writer Dim str. Content As String = txt. Content. Text fs = f. Create. Text fs. Write. Line(str. Content) fs. Close()

Step-by-Step 5. 8 'Message. Box. Show("Call the Font dialog box. ") With Font. Dialog

Step-by-Step 5. 8 'Message. Box. Show("Call the Font dialog box. ") With Font. Dialog 1. Show. Dialog() txt. Content. Font =. Font End With

Step-by-Step 5. 8 'Message. Box. Show("Create a new document. ") txt. Content. Reset. Text()

Step-by-Step 5. 8 'Message. Box. Show("Create a new document. ") txt. Content. Reset. Text() Path. Name = Menu. Item 2. Text Loadfile() Path. Name = Menu. Item 3. Text Loadfile()

Did You Know? In the earliest forms of BASIC, every assignment statement had to

Did You Know? In the earliest forms of BASIC, every assignment statement had to be preceded by the word Let. This was how the BASIC interpreter identified an assignment statement.

Important Comments at the beginning of the routine document what the routine does and

Important Comments at the beginning of the routine document what the routine does and what other routines call it. Comments start with a single quote and can appear anywhere on the line. Despite the fact that Bill Gates claims to know sections of the code from his first version of BASIC by heart, most programmers forget the code they have written shortly after it is written. Internal documentation is very important.

Summary l l l The Message. Box. Show statement is used to communicate a

Summary l l l The Message. Box. Show statement is used to communicate a short message to the user. The Message. Box. Show function is used when the response from the user can be limited to a button click. The syntax for Message. Box. Show function is variable = Message. Box. Show(text [, caption ] [, buttons ] [ , icon ] [default. Button ] [, options ] ) Visual Basic provides many constants to simplify supplying parameter values to functions and procedures. The constants are stored as properties of the object to which they apply. The Input. Box function gathers up to 255 characters from the user. The syntax for the Input. Box function is variable = Input. Box (prompt [ , title] [ , default] [, xpos] [, ypos])

Summary l l A run-time error occurs when a program cannot perform a task

Summary l l A run-time error occurs when a program cannot perform a task that it expects to be able to perform. The Val( ) function is one of several functions used to convert the string entered with an Input. Box function to a value. A pop-up context menu appears when the user right-clicks the mouse button. It provides commonly used commands in a convenient package to facilitate user interaction with the program. There may be several context menus in an application, each linked to a different control. Every event procedure passes two parameters. The Sender parameter identifies the object in which the event was initiated. The e parameter contains information about the event raised. It is tailored to each event and contains just the information relevant to the event.

Summary l l The Image. List control is used to store images for the

Summary l l The Image. List control is used to store images for the Toolbar control. Once the image list is loaded with images, it can be attached to a toolbar where the images are used to label buttons. The images occupy the Images collection of the Image. List control. The toolbar contains buttons labeled with images from the Image. List control that provide commonly used commands to the user. Often the toolbar buttons mirror menu items. The buttons of the toolbar are contained in the toolbar's Buttons collection. The Visible property of a menu item is used to enable or disable the display of the menu item. Always supply Tool. Tip text for each button of a toolbar.

Summary l l A toolbar button may have a drop-down list attached. The list

Summary l l A toolbar button may have a drop-down list attached. The list may be preloaded with options at design time or items may be added at run time. All buttons share the same event procedure, Tool. Bar 1_Button. Click. Individual buttons are passed to the event procedure in the e parameter. Buttons are differentiated by their Text property. The Resize event of a form fires whenever the form is resized. This is the place to put code that resizes or repositions controls on the form. A sequential file stores characters one after the other like songs on a cassette tape. New characters are added to the end of the file.

Summary l l l Dialog controls are used to provide a project with a

Summary l l l Dialog controls are used to provide a project with a number of common Windows dialog boxes. These are dialog boxes with which any Windows user is familiar: Open, Save, Font, Printer, and Color. Text files are easily created and accessed through Stream. Reader and Stream. Writer classes used in conjunction with the File class. The With statement lets you go "one layer" into a control. This eliminates the need to retype the name of the control each time its properties or methods are accessed.