Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Access, Dialog Boxes, Error Handling, and Menus Chapter 5.

Similar presentations


Presentation on theme: "File Access, Dialog Boxes, Error Handling, and Menus Chapter 5."— Presentation transcript:

1 File Access, Dialog Boxes, Error Handling, and Menus Chapter 5

2 FileOpen Function Opens a sequential file. FileOpen(Filenumber, Filename, Mode) Filenumber – assign an integer for identification while it is open. Filename – complete path to the document “c:\folder\folder\file.ext”. Mode – Input, Output, Append

3 Code to display MyFile.txt in textbox FileOpen(1, “A:\MyFile.txt”, OpenMode.Input) txtFile.Text = InputString(1, LOF(1)) FileClose(1) InputString(filenumber,number of characters in string) LOF – length of file

4 Code to save the textbox text to MyFile.txt FileOpen(1, “A:\MyFile.txt”, OpenMode.Output) Print(1, txtFile.text) FileClose(1) Print (filenumber, source expression) Source expression – location of information to write

5 User-Defined Data Type Record Structure Structure Employee Public intNumber as Integer Public strEmpID as String Public strLast as String Public strFirst as String Public strAddress as String Public strCity as String Public strState as String Public strZip as String Public strPhone as String Public sngPayRate as Single End Structure Begins the record definition. Ends the record definition.

6 Variable Sizes Boolean, Char, Short – 2 bytes Integer, Single – 4 bytes Double, Long, Date/Time – 8 bytes Decimal – 16 bytes Calculate fixed size of record? Len(udtEmployees)

7

8 Declare a Structure Variable Dim udtEmployees as Employee Assign values to a Structure Variable udtEmployees.strEmpID = txtEmpID.Text udtEmployees.strLast = txtLast.text udtEmployees.strFirst = txtFirst.text

9 Open a Random Access File FileOpen function FileOpen(1, “path and file name”, OpenMode.Random, OpenAccess.ReadWrite, OpenShare.Shared, Len(udtEmployees)) Share restrictions – Shared (default), Lock Read, Lock Write, Lock Read Write RecordLength = max 32,767

10 Reading a Random Access File Input the random record number that you want to read. Check to see if the record is within the limits of the total number of records. Open the file as random. FileGet(1, udtEmployees, intCurrentRecord) Close the file.

11 Writing a Random Access File Fill in the text boxes that relate to fields. Check to see if adding new or editing existing. Update current record accordingly. Assign the text fields to the record structure. Open the file as random. FilePut(1, udtEmployees, intCurrentRecord) Calculate new length = LOF(1) / Len(udtEmployees) Close the file.

12 I/O Errors - Exceptions Invalid Drive. File does not exist. Drive is empty. Disk not formatted. An exception is any error condition or unexpected behavior that occurs during runtime. If a procedure has an error, it is said to throw an exception.

13 I/O Errors - Exceptions Try –FileOpen(1, “filepath”,OpenMode.Input) –txtFile.Text = InputString(1,LOF(1)) Catch Err As System.IO.IOException –MsgBox(“File not in drive”) Finally –FileClose(1) End Try

14 Common Dialog Boxes Dialog Box is a window that appears on the screen. Common Dialog Box is predefined for useful functions like file open and save, formatting fonts, and printing. Common Dialog Control is added to a form to display a common dialog box. All functionality of the box is inherited.

15 Common Dialog Controls Table 5.1 ColorDialog FontDialog OpenFileDialog PageSetupDialog PrintDialog PrintPreviewDialog SaveFile Dialog

16 OpenFileDialog Control Uses the StreamRead object to get a stream of characters sequentially from a file. Dim objStreamReader as Object. Must include Imports System.IO namespace. Dialog controls are saved in the components tray and not on the form.

17 OpenFileDialog Control OpenFileDialog1.Filter = “txt files (*.txt) | *.txt” Filters out all but txt files OpenFileDialog1.InitialDirectory = “A:\”

18 OpenFileDialog Control If OpenFileDialog1.ShowDialog = DialogResult.OK Then –ShowDialog – Displays the dialog box –DialogResult determines if a file has been selected Dim strStream as Stream strStream = OpenFileDialog1.OpenFile –Gets Stream to read from dialog box Dim strReader as New StreamReader(strStream) –Declares the object that actually reads the stream txtFile.Text = strReader.ReadToEnd –Puts all the data into the text box txtFile.SelectionLength = 0 –Moves the cursor to the beginning of the text box strReader.Close End IF

19 SaveFileDialog Control SaveFileDialog1.Filter = “txt files (*.txt) | *.txt” –Filters out all but txt files SaveFileDialog1.FileName = “Text1” –Sets the default filename SaveFileDialog1.OverwritePrompt = True –Displays a warning message if the file already exists

20 SaveFileDialog Control If SaveFileDialog1.ShowDialog = DialogResult.OK Then –ShowDialog – Displays the dialog box –DialogResult determines if a file has been selected strFileName = SaveFileDialog1.FileName –Captures the filename to be saved Dim strStream as New FileStream(strFileName, OpenMode.Output) –sets a new fileStream to write Dim strWriter as New StreamWriter(strStream) –Declares the object that actually writes the stream strWriter.Write (txtFile.Text) –Writes all the data from the text box strWriter.Close End IF

21 FontDialog Control If FontDialog1.ShowDialog <> DialogResult.Cancel Then txtText.Font = FontDialog1.Font End If If they didn’t press cancel, then the font property of the text box is set to the font property of the font dialog box.

22 Benefits of Common Dialog Boxes Program development is easier because of pre-existing functionality. Dialog boxes are consistent and familiar. Reuse pre-configured components. Have pre-existing error handling.

23 RichTextBox Control Reads, Writes, and Displays formatted text. Control prefix is rtxt Open filter is “RTF files (*.rtf) | *.rtf” Sequentially reads the rich text file into the control. rtxtFile.LoadFile(OpenFileDialog1.FileName, RichTextBoxStreamType.RichText)

24 RichTextBox Control Save filter is “RTF files (*.rtf) | *.rtf” Sequentially writes the rich text file to disk from the control. rtxtFile.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.RichText)

25 Changing Fonts in RichTextBox Font, Style, Size, Color, Effects FontDialog1.ShowColor = True If FontDialog1.ShowDialog <> DialogResult.Cancel Then –rtxtFile.SelectionFont = FontDialog1.Font –rtxtFile.SelectionColor = FontDialog1.Color End If

26 Menus A menu is a graphical element that contains associated commands. Menu items are grouped by a common theme.

27 Menu Designer 1.Add a MainMenu control to the form. 2.Add menu items to the MainMenu control. 3.Write code to handle each menu item’s MenuItem_Click event.

28 Menu Designer Drag a MainMenu control to the form. It is placed in the component tray. Click on the form itself and change the Menu property to MainMenu1 Add menu items by typing in the ares marked Type Here. Create an accelerator key for the menu by typing an & before a letter.

29 Menu Designer Change the name property of each menu item using mnu prefix. Each level of a menu item should include all parent levels. mnuFile mnuFileOpen Separator lines organize the menu. Select an item, right click, Insert Separator.

30 Context Menus ContextMenu controls create shortcut menus. Drag the ContextMenu control to the form and it is placed in the component tray. Select the form and change its ContextMenu property to the name of the contextmenu control. The context menu will display when the user right clicks on the form.


Download ppt "File Access, Dialog Boxes, Error Handling, and Menus Chapter 5."

Similar presentations


Ads by Google