在VB里用浏览选择的TXT方法~~

bb524 2008-03-19 07:01:46
在VB里用浏览选择的TXT方法~~选择到一个TXT文件~~并将文件全部内容写到TEXT文本~
然后再问一下在TEXT文本改写了怎么保存回原来的文件~

谢谢~~
...全文
37 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
cbm6666 2008-03-19
'添加 Command1 Command2 Text1 CommonDialog1
'Text1的MultiLine 要设为True 文档太大的话可考虑改用 RichTextBox

Dim fname$,FileOS As Boolean
Private Sub Form_Load()
Command1.Caption = "打 开"
Command2.Caption = "保 存"
End Sub

Private Sub Command1_Click()
FileOS = True
Call FOSprog
End Sub

Private Sub Command2_Click()
FileOS = False
Call FOSprog
End Sub

Private Sub FOSprog()
On Error GoTo errhandler
With CommonDialog1
.CancelError = True
.InitDir = App.Path '予设存档路径
.Filter = "文档文件(*.txt)|*.txt"
If FileOS Then
.ShowOpen '打开文件
Else
.ShowSave '保存文件
End If
End With
fname = CommonDialog1.FileName
'从上面打开的记事本文件读入资料
If FileOS Then
Open fname For Input As #1
Text1.Text = StrConv(InputB(LOF(1), 1), vbUnicode)
Else
Open fname For Output As #1
Print #1, Text1.Text
End If
Close #1
errhandler:
If Err = 32755 Then Exit Sub '选择了取消
End Sub

回复
Option Explicit

Private Sub loadfiletotext(ByVal s As String, t As TextBox)
Dim l As Long
l = FileLen(s)
ReDim a(0 To l - 1) As Byte
Open s For Binary As #1
Get #1, , a
Close #1
t.Text = StrConv(a, vbUnicode)
End Sub

Private Sub savetexttofile(t As TextBox, ByVal s As String)
Open s For Output As #1
Print #1, t.Text
Close #1
End Sub
Private Sub Command1_Click()
loadfiletotext "d:\temp001.txt", Text1
End Sub

Private Sub Command2_Click()
savetexttofile Text1, "d:\temp001.txt"
End Sub
回复
API GetOpenFileName
回复
bb524 2008-03-19
还有一个问题就是TXT的文件比较大~~~~~
各位大侠帮帮忙~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~谢谢 ~~
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7517

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2008-03-19 07:01
社区公告
暂无公告