VB怎么将文本框的内容保存到txt

2024-11-22 07:56:03
推荐回答(3个)
回答1:

教你一个既可以保存也可以打开的方法:
先加CommonDialog控件
方法:
1、工具栏空白处右击选择“部件”,在控件中选中microsoft common Dialog control 6.0
2、回到工具栏,上面就会多出一个CommonDialog控件,你把它加在窗口上就好了。
代码:

'保存:
Private Sub command1_Click()
With CommonDialog1
.DialogTitle = "保存文件"
.Filter = "文本文件 (*.txt)|*.txt|Word(*.doc)|*.doc|RTF(*.rtf)|*.rtf|所以文件(*.*)|*.*"
.FilterIndex = 0
.ShowSave
Open .FileName For Output As #2
Print #2, text1.Text
Close #2
End With
End Sub
'打开:
Private Sub command2_Click()
With CommonDialog1
.DialogTitle = "打开文件"
.Filter = "文本文件 (*.txt)|*.txt|Word(*.doc)|*.doc|RTF(*.rtf)|*.rtf|所以文件(*.*)|*.*"
.FilterIndex = 0
.ShowOpen
Dim tmpLoadStr As String
Open .FileName For Input As #1
Do While Not EOF(1)
Line Input #1, tmpLoadStr
text1.Text = text1.Text & tmpLoadStr & vbCrLf
Loop
Close #1
End With
End Sub

回答2:

Private Sub cmd1_click()
Open App.path & "\1.txt" for Append as #1
Print #1,Text1.Text
Close #1
End Sub

以上代码可以实现你的要求,
Append是追加,如果只要一次性写入覆盖前面的内容就用Output.

回答3:

Private
Sub
Text1_Change()
Open
"d:\data.txt"
For
Output
As
#1
Print
#1,
Text1
Close
#1
End
Sub
’这样还是最简单。你删除文本里的内容的话,data也随着删除,只要文本内容改变,data.txt也随着改变