VB6.0可以用一次读取文本文件全部文本内容,然后使用Split函数来实现一行一行提取文本框行内容。
Split函数,返回一个下标从零开始的一维数组,它包含指定数目的子字符串。
以下代码是可以提取文本文件任何一行内容的:
Private Sub Command1_Click()
Dim strWj As String
Dim strj() As String
Dim aryContent() As Byte
Dim i As Long
Dim j As Long
CommonDialog1.CancelError = True ' 设置“CancelError”为 True
On Error GoTo ErrHandler
CommonDialog1.Flags = cdlOFNHideReadOnly ' 设置标志
' 设置过滤器
CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files" & "(*.txt)|*.txt|Batch Files (*.bat)|*.bat"
CommonDialog1.FilterIndex = 2 ' 指定缺省的过滤器
CommonDialog1.ShowOpen ' 显示“打开”对话框
' 显示选定文件的名字
'MsgBox CommonDialog1.FileName
Open CommonDialog1.FileName For Binary As #1
ReDim aryContent(LOF(1) - 1)
Get #1, , aryContent
Close #1
strWj = StrConv(aryContent, vbUnicode)
RichTextBox1 = strWj
Text1 = strWj
strj = Split(strWj, vbCrLf)
i = UBound(strj)
Text2 = i + 1
j = InputBox("输入需要显第几句", j)
j = j - 1
Label1.Caption = j + 1 & ":" & strj(j)
Exit Sub
ErrHandler:
' 用户按了“取消”按钮
Exit Sub
End Sub
Line Input 可以读一行文本:
使用格式:
dim s as string
Open "c:\1.txt" for input as #1
line input #1,s
text1.text = s
close #1
另外还可以用 EOF 判断是否到行尾
Dim a() as String
Open "XXX.txt" for input as #1
Do While Not EOF(1)
i=i+1
redim preserve a(i)
line input #1,a(i)
loop
close #1
数组a()就是所需数组