home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Unleashed / Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso / source / chap25 / mdi / filopen.bas next >
Encoding:
BASIC Source File  |  1995-07-26  |  3.4 KB  |  120 lines

  1. Attribute VB_Name = "Module2"
  2.  
  3. Sub FOpenProc()
  4.     Dim RetVal
  5.     On Error Resume Next
  6.     Dim OpenFileName As String
  7.     frmMDI.CMDialog1.Filename = ""
  8.     frmMDI.CMDialog1.ShowOpen
  9.     If Err <> 32755 Then    ' User chose Cancel.
  10.         OpenFileName = frmMDI.CMDialog1.Filename
  11.         ' If the file is larger than 65K, it can't
  12.         ' be opened, so cancel the operation.
  13.         If FileLen(OpenFileName) > 65000 Then
  14.             MsgBox "The file is too large to open."
  15.             Exit Sub
  16.         End If
  17.         
  18.         OpenFile (OpenFileName)
  19.         UpdateFileMenu (OpenFileName)
  20.         ' Show the toolbar if they aren't already visible.
  21.         If gToolsHidden Then
  22.             frmMDI!imgcutbutton.Visible = True
  23.             frmMDI!imgcopybutton.Visible = True
  24.             frmMDI!imgPasteButton.Visible = True
  25.             gToolsHidden = False
  26.         End If
  27.     End If
  28. End Sub
  29.  
  30. Function GetFileName(Filename As Variant)
  31.     ' Display a Save As dialog box and return a filename.
  32.     ' If the user chooses Cancel, return an empty string.
  33.     On Error Resume Next
  34.     frmMDI.CMDialog1.Filename = Filename
  35.     frmMDI.CMDialog1.ShowSave
  36.     If Err <> 32755 Then    ' User chose Cancel.
  37.         GetFileName = frmMDI.CMDialog1.Filename
  38.     Else
  39.         GetFileName = ""
  40.     End If
  41. End Function
  42.  
  43. Function OnRecentFilesList(Filename) As Integer
  44.   Dim i
  45.  
  46.   For i = 1 To 4
  47.     If frmMDI.mnuRecentFile(i).Caption = Filename Then
  48.       OnRecentFilesList = True
  49.       Exit Function
  50.     End If
  51.   Next i
  52.     OnRecentFilesList = False
  53. End Function
  54.  
  55. Sub OpenFile(Filename)
  56.     Dim NL, TextIn, GetLine
  57.     Dim fIndex As Integer
  58.  
  59.     NL = Chr(13) + Chr(10)
  60.     
  61.     On Error Resume Next
  62.     ' Open the selected file.
  63.     Open Filename For Input As #1
  64.     If Err Then
  65.         MsgBox "Can't open file: " + Filename
  66.         Exit Sub
  67.     End If
  68.     ' Change the mouse pointer to an hourglass.
  69.     Screen.MousePointer = 11
  70.     
  71.     ' Change the form's caption and display the new text.
  72.     fIndex = FindFreeIndex()
  73.     Document(fIndex).Tag = fIndex
  74.     Document(fIndex).Caption = UCase(Filename)
  75.     Document(fIndex).Text1.Text = Input(LOF(1), 1)
  76.     FState(fIndex).Dirty = False
  77.     Document(fIndex).Show
  78.     Close #1
  79.     ' Reset the mouse pointer.
  80.     Screen.MousePointer = 0
  81. End Sub
  82.  
  83. Sub SaveFileAs(Filename)
  84. On Error Resume Next
  85.     Dim Contents As String
  86.  
  87.     ' Open the file.
  88.     Open Filename For Output As #1
  89.     ' Place the contents of the notepad into a variable.
  90.     Contents = frmMDI.ActiveForm.Text1.Text
  91.     ' Display the hourglass mouse pointer.
  92.     Screen.MousePointer = 11
  93.     ' Write the variable contents to a saved file.
  94.     Print #1, Contents
  95.     Close #1
  96.     ' Reset the mouse pointer.
  97.     Screen.MousePointer = 0
  98.     ' Set the form's caption.
  99.     If Err Then
  100.         MsgBox Error, 48, App.Title
  101.     Else
  102.         frmMDI.ActiveForm.Caption = UCase(Filename)
  103.         ' Reset the dirty flag.
  104.         FState(frmMDI.ActiveForm.Tag).Dirty = False
  105.     End If
  106. End Sub
  107.  
  108. Sub UpdateFileMenu(Filename)
  109.         Dim RetVal
  110.         ' Check if the open filename is already in the File menu control array.
  111.         RetVal = OnRecentFilesList(Filename)
  112.         If Not RetVal Then
  113.           ' Write open filename to MDINOTEPAD.INI.
  114.           WriteRecentFiles (Filename)
  115.         End If
  116.         ' Update the list of the most recently opened files in the File menu control array.
  117.         GetRecentFiles
  118. End Sub
  119.  
  120.