home *** CD-ROM | disk | FTP | other *** search
/ Master 95 #1 / MASTER95_1.iso / microsof / vbasic4 / vb4-6.cab / textedit.bas < prev    next >
Encoding:
BASIC Source File  |  1995-07-26  |  2.9 KB  |  70 lines

  1. Attribute VB_Name = "Module1"
  2. Dim ArrayNum As Integer     ' Index value for the menu control array mnuFileArray.
  3. Public Filename As String   ' This variable keeps track of the filename information for opening and closing files.
  4.  
  5. Sub CloseFile(Filename As String)
  6. Dim F As Integer
  7. On Error GoTo CloseError    ' If there is an error, display the error message below.
  8.     
  9.     If Dir(Filename) <> "" Then         ' File already exists, so ask if the user wants to overwrite the file.
  10.         response = MsgBox("Overwrite existing file?", vbYesNo + vbQuestion + vbDefaultButton2)
  11.         If response = vbNo Then Exit Sub
  12.     End If
  13.     F = FreeFile
  14.     Open Filename For Output As F       ' Otherwise, open the filename for output.
  15.     Print #F, frmEditor!txtEdit.Text    ' Print the current text to the opened file.
  16.     Close F                             ' Close the file.
  17.     Filename = "Untitled"               ' Reset the caption of the main form.
  18.     Exit Sub
  19. CloseError:
  20.     MsgBox "Error occurred while trying to close file, please retry.", 48
  21.     Exit Sub
  22. End Sub
  23.  
  24. Sub DoUnLoadPreCheck(UnloadMode As Integer)
  25.     If UnloadMode = 0 Or UnloadMode = 3 Then
  26.             Unload frmAbout
  27.             Unload frmEditor
  28.             End
  29.     End If
  30. End Sub
  31.  
  32. Sub OpenFile(Filename As String)
  33. Dim F As Integer
  34.     If "Text Editor: " + Filename = frmEditor.Caption Then  ' Avoid opening a file if it is already loaded.
  35.         Exit Sub
  36.     Else
  37.         On Error GoTo errhandler
  38.             F = FreeFile
  39.             Open Filename For Input As F                    ' Open the file selected in the File Open About dialog box.
  40.             frmEditor!txtEdit.Text = Input(LOF(F), F)
  41.             Close F                                         ' Close the file.
  42.             ' frmEditor.mnuFileItem(3).Enabled = True         ' Enable the Close command on the File menu.
  43.             UpdateMenu
  44.             frmEditor.Caption = "Text Editor: " + Filename
  45.             Exit Sub
  46.     End If
  47. errhandler:
  48.         MsgBox "Error encountered while trying to open file, please retry.", 48, "Text Editor"
  49.         Close F
  50.         Exit Sub
  51. End Sub
  52.  
  53. Sub UpdateMenu()
  54.     frmEditor.mnuFileArray(0).Visible = True            ' Make the initial element visible and display separator bar.
  55.     ArrayNum = ArrayNum + 1                             ' Increment the Index property of the menu control array.
  56.     ' Check to see if Filename is already on the menu list.
  57.     For i = 0 To ArrayNum - 1
  58.         If frmEditor.mnuFileArray(i).Caption = Filename Then
  59.             ArrayNum = ArrayNum - 1
  60.             Exit Sub
  61.         End If
  62.     Next i
  63.     
  64.     ' If filename is not on the menu list, add the menu item.
  65.     Load frmEditor.mnuFileArray(ArrayNum)               ' Create a new menu control.
  66.     frmEditor.mnuFileArray(ArrayNum).Caption = Filename ' Set the caption of the new menu item.
  67.     frmEditor.mnuFileArray(ArrayNum).Visible = True     ' Make the new menu item visible.
  68. End Sub
  69.  
  70.