首先运行VB,新建一个标准EXE工程,在窗件上添加一个Text控件,两个CommandButton控件和一个CommonDialog控件(文件对话框控件),再将Text1控件的MultiLine属性设为True,ScrollBars属性设 为3,其它的属性采用默认的就可以了,然后打开代码窗口加入下面的代码:
Option Explicit
Private Sub Form_Load()
CommonDialog1.CancelError = True
CommonDialog1.Filter = "网页|*.htm;*.html"
Command1.Caption = "打开"
Command2.Caption = "保存"
End Sub
Private Sub Command1_Click()
On Error Resume Next
Dim TextLine As String
CommonDialog1.ShowOpen
If Err <> 32755 Then
Text1 = ""
'打开文件
Open CommonDialog1.filename For Input As #1
Do While Not EOF(1)
Line Input #1, TextLine
'去掉左边和右边的空格,再换行
Text1 = Text1 & Trim(TextLine) & vbCrLf
'若上面这句换成:
' Text1 = Text1 & Trim(TextLine)
'即去掉空格但不换行,这样,压缩率更大,但是它的可读性就差多了
Loop Close #1
End If
End Sub
Private Sub Command2_Click()
On Error Resume Next
CommonDialog1.ShowSave
If Err <> 32755 Then
'保存文件
Open CommonDialog1.filename For Output As #1
Print #1, Text1
Close #1
End If
End Sub
文章来源于领测软件测试网 https://www.ltesting.net/