VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 用TextBox读入TXT文件/写入TXT文件
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:14985, 日期:2001-04-09
Public Function LoadFileToTB(TxtBox As Object, FilePath As _
   String, Optional Append As Boolean = False) As Boolean
   
`PURPOSE: Loads file specified by FilePath into textcontrol
`(e.g., Text Box, Rich Text Box) specified by TxtBox

`If Append = true, then loaded text is appended to existing
` contents else existing contents are overwritten

`Returns: True if Successful, false otherwise

Dim iFile As Integer
Dim s As String

If Dir(FilePath) = "" Then Exit Function

On Error GoTo ErrorHandler:
s = TxtBox.Text

iFile = FreeFile
Open FilePath For Input As #iFile
s = Input(LOF(iFile), #iFile)
If Append Then
    TxtBox.Text = TxtBox.Text & s
Else
    TxtBox.Text = s
End If

LoadFileToTB = True

ErrorHandler:
If iFile > 0 Then Close #iFile

End Function

Public Function SaveFileFromTB(TxtBox As Object, _
   FilePath As String, Optional Append As Boolean = False) _
   As Boolean
  
`PURPOSE: Saves contents of text control (e.g., Text Box,
`Rich Text Box) specified by TxtBox to file specified by FilePath

`If Append = true, then TxtBox`s contents are
`appended to existing file contents
`else existing file is overwritten

`Returns: True if Successful, false otherwise

Dim iFile As Integer

iFile = FreeFile
If Append Then
    Open FilePath For Append As #iFile
Else
    Open FilePath For Output As #iFile
End If

Print #iFile, TxtBox.Text
SaveFileFromTB = True

ErrorHandler:

Close #iFile
End Function