Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 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.

3 Step-by-Step 5.8 Sub LoadFile() Dim f As File = New File(PathName) Dim fs As StreamReader fs = f.OpenText() txtContent.Text = fs.ReadToEnd fs.close() End Sub

4 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.

5 Step-by-Step 5.8 'MessageBox.Show("Call the Open File dialog box.") With OpenFileDialog1.DefaultExt = "txt".InitialDirectory = "c:\My Documents".Filter = "Text files | *.txt".ShowDialog() PathName =.FileName End With MenuItem3.Text = MenuItem2.Text MenuItem2.text = PathName LoadFile()

6 Step-by-Step 5.8 'MessageBox.Show("Call the Save File dialog box.") With SaveFileDialog1.InitialDirectory = "c:\My Documents".Filter = "Text files | *.txt".ShowDialog() PathName =.FileName End With

7 Step-by-Step 5.8 Dim f As File = New File(PathName) Dim fs As StreamWriter Dim strContent As String = txtContent.Text fs = f.CreateText fs.WriteLine(strContent) fs.Close()

8 Step-by-Step 5.8 'MessageBox.Show("Call the Font dialog box.") With FontDialog1.ShowDialog() txtContent.Font =.Font End With

9 Step-by-Step 5.8 'MessageBox.Show("Create a new document.") txtContent.ResetText() PathName = MenuItem2.Text Loadfile() PathName = MenuItem3.Text Loadfile()

10 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.

11 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.

12 Summary The MessageBox.Show statement is used to communicate a short message to the user. The MessageBox.Show function is used when the response from the user can be limited to a button click. The syntax for MessageBox.Show function is variable = MessageBox.Show(text [, caption ] [, buttons ] [, icon ] [defaultButton ] [, 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 InputBox function gathers up to 255 characters from the user. The syntax for the InputBox function is variable = InputBox (prompt [, title] [, default] [, xpos] [, ypos])

13 Summary 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 InputBox 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.

14 Summary The ImageList 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 ImageList control. The toolbar contains buttons labeled with images from the ImageList 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 ToolTip text for each button of a toolbar.

15 Summary 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, ToolBar1_ButtonClick. 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.

16 Summary 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 StreamReader and StreamWriter 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.


Download ppt "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."

Similar presentations


Ads by Google