用vb编程

来源:百度知道 编辑:UC知道 时间:2024/06/14 19:54:31
创建一个文本文档,在文本文档中输入内容,并显示在text中,清空text中的内容。

准备工作:
1:在C:\下新建一个文本文件,名为Mytxt.txt,内容随便输入
2:在窗体上添加一个命令按钮,一个文本框,设置文本框的Multiline属性为True,ScrollBars属性为2
添加如下代码,F5运行后,点击命令按钮

Private Sub Command1_Click()
'读取文本文件的内容并显示在文本框中
Dim S as string,S1 as String
Open "C:\MyTxt.txt" For Input as #1
While Not Eof(1)
Line Input #1,S
S1=S1 & S & IIf(Eof(1),"",vbCrlf)
Wend
Close #1
Text1.Text=S1

'清空文本文件的内容
Open "C:\MyTxt.Txt" For output As #1
Print #1,
Close #1
End Sub

设置Timer1的interval为1000
在1.txt中输入字符,保存后,text1的内容即刷新为新内容。
Private Sub Timer1_Timer()
Dim s As String
Open "c:\1.txt" For Input As #1
Line Input #1, s
Text1.Text = s
Close
End Sub

'在文本框输入内容,回车后将内容保存在c:\1.txt并清空文本框
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Open "c:\1.txt" For Append As #1<