home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / fileio / textfile / textfile.frm (.txt) next >
Encoding:
Visual Basic Form  |  1998-07-13  |  5.0 KB  |  165 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form TextFileForm 
  4.    Caption         =   "Text File I/O Demo"
  5.    ClientHeight    =   5190
  6.    ClientLeft      =   60
  7.    ClientTop       =   345
  8.    ClientWidth     =   7320
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   5190
  11.    ScaleWidth      =   7320
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin MSComDlg.CommonDialog CommonDialog1 
  14.       Left            =   30
  15.       Top             =   4470
  16.       _ExtentX        =   847
  17.       _ExtentY        =   847
  18.       _Version        =   393216
  19.    End
  20.    Begin VB.CommandButton bttnExit 
  21.       Caption         =   "E X I T"
  22.       Height          =   525
  23.       Left            =   5640
  24.       TabIndex        =   4
  25.       Top             =   4560
  26.       Width           =   1515
  27.    End
  28.    Begin VB.CommandButton bttnFileSaveAs 
  29.       Caption         =   "Save File As"
  30.       BeginProperty Font 
  31.          Name            =   "Verdana"
  32.          Size            =   9
  33.          Charset         =   0
  34.          Weight          =   400
  35.          Underline       =   0   'False
  36.          Italic          =   0   'False
  37.          Strikethrough   =   0   'False
  38.       EndProperty
  39.       Height          =   525
  40.       Left            =   3495
  41.       TabIndex        =   3
  42.       Top             =   4575
  43.       Width           =   1515
  44.    End
  45.    Begin VB.CommandButton bttnFileSave 
  46.       Caption         =   "Save File"
  47.       BeginProperty Font 
  48.          Name            =   "Verdana"
  49.          Size            =   9
  50.          Charset         =   0
  51.          Weight          =   400
  52.          Underline       =   0   'False
  53.          Italic          =   0   'False
  54.          Strikethrough   =   0   'False
  55.       EndProperty
  56.       Height          =   525
  57.       Left            =   1830
  58.       TabIndex        =   2
  59.       Top             =   4575
  60.       Width           =   1515
  61.    End
  62.    Begin VB.CommandButton bttnFileOpen 
  63.       Caption         =   "Open File"
  64.       BeginProperty Font 
  65.          Name            =   "Verdana"
  66.          Size            =   9
  67.          Charset         =   0
  68.          Weight          =   400
  69.          Underline       =   0   'False
  70.          Italic          =   0   'False
  71.          Strikethrough   =   0   'False
  72.       EndProperty
  73.       Height          =   525
  74.       Left            =   135
  75.       TabIndex        =   1
  76.       Top             =   4575
  77.       Width           =   1515
  78.    End
  79.    Begin VB.TextBox Text1 
  80.       BeginProperty Font 
  81.          Name            =   "Verdana"
  82.          Size            =   9
  83.          Charset         =   0
  84.          Weight          =   400
  85.          Underline       =   0   'False
  86.          Italic          =   0   'False
  87.          Strikethrough   =   0   'False
  88.       EndProperty
  89.       Height          =   4305
  90.       Left            =   105
  91.       MultiLine       =   -1  'True
  92.       ScrollBars      =   2  'Vertical
  93.       TabIndex        =   0
  94.       Top             =   120
  95.       Width           =   7065
  96.    End
  97. Attribute VB_Name = "TextFileForm"
  98. Attribute VB_GlobalNameSpace = False
  99. Attribute VB_Creatable = False
  100. Attribute VB_PredeclaredId = True
  101. Attribute VB_Exposed = False
  102. Dim OpenFile As String
  103. Private Sub bttnExit_Click()
  104.     End
  105. End Sub
  106. Private Sub bttnFileOpen_Click()
  107. Dim FNum As Integer
  108. Dim txt As String
  109. On Error GoTo FileError
  110.     CommonDialog1.CancelError = True
  111.     CommonDialog1.Flags = cdlOFNFileMustExist
  112.     CommonDialog1.DefaultExt = "TXT"
  113.     CommonDialog1.Filter = "Text files|*.TXT|All files|*.*"
  114.     CommonDialog1.ShowOpen
  115.     FNum = FreeFile
  116.     Open CommonDialog1.FileName For Input As #1
  117.     txt = Input(LOF(FNum), #FNum)
  118.     Close #FNum
  119.     Text1.Text = txt
  120.     OpenFile = CommonDialog1.FileName
  121.     Exit Sub
  122. FileError:
  123.     If Err.Number = cdlCancel Then Exit Sub
  124.     MsgBox "Unkown error while opening file " & CommonDialog1.FileName
  125.     OpenFile = ""
  126. End Sub
  127. Private Sub bttnFileSave_Click()
  128. Dim FNum As Integer
  129. Dim txt As String
  130.     If OpenFile = "" Then
  131.         bttnFileSaveAs_Click
  132.         Exit Sub
  133.     End If
  134. On Error GoTo FileError
  135.     FNum = FreeFile
  136.     Open OpenFile For Output As #1
  137.     Print #FNum, Text1.Text
  138.     Close #FNum
  139.     Exit Sub
  140. FileError:
  141.     If Err.Number = cdlCancel Then Exit Sub
  142.     MsgBox "Unkown error while saving file " & OpenFile
  143.     OpenFile = ""
  144. End Sub
  145. Private Sub bttnFileSaveAs_Click()
  146. Dim FNum As Integer
  147. Dim txt As String
  148. On Error GoTo FileError
  149.     CommonDialog1.CancelError = True
  150.     CommonDialog1.Flags = cdlOFNOverwritePrompt
  151.     CommonDialog1.DefaultExt = "TXT"
  152.     CommonDialog1.Filter = "Text files|*.TXT|All files|*.*"
  153.     CommonDialog1.ShowSave
  154.     FNum = FreeFile
  155.     Open CommonDialog1.FileName For Output As #1
  156.     Print #FNum, Text1.Text
  157.     Close #FNum
  158.     OpenFile = CommonDialog1.FileName
  159.     Exit Sub
  160. FileError:
  161.     If Err.Number = cdlCancel Then Exit Sub
  162.     MsgBox "Unkown error while saving file " & CommonDialog1.FileName
  163.     OpenFile = ""
  164. End Sub
  165.