怎样用VB 逐行循环分类的输入文本

来源:百度知道 编辑:UC知道 时间:2024/05/27 02:21:10
有a.txt文本中有许多行内容,格式如下:
aaaa|bbbb
aa|bb
aa|bb
……(即每行只有a*|b*,a*、b*为任意长度字符串,|为分隔符)
在窗体里做了一个Command1按钮和二个文本框Text1和Text2(不允许
多行输入的)
现在想实现以下功能:
1.点击一下按钮,会逐行读入a.txt文本,并且a*输入到Text1中,b*输入到Text2中(注意是每点一次读一行,而且a*、b*要分别输入两个文本框中)
2.如果读到最后一行,将会返回到a.txt文本中的第一行开始继续.其读入的方法同上(注意同上1点)如此循环,不会结束。
代码务必详细,有效,而且简单又看的明白的,VB初学不久,什么API的,模块的还不太懂,最好不用,谢谢

试试下面的程序看看,自己修改一下路径

Private Sub Command1_Click()
Static i As Integer
i = i + 1
Dim S As String, A
S = ReadLine("C:\a.txt", i)
A = Split(S, "|")
If UBound(A) > 0 Then
Text1.Text = A(0)
Text2.Text = A(1)
End If
End Sub

Function ReadLine(sPath As String, N As Integer) As String
Dim S1 As String, S As String
If Len(Dir(sPath)) > 0 Then
i = 0
Open sPath For Input As #1
While Not EOF(1)
i = i + 1
Line Input #1, S
If i = 1 Then S1 = S
If i = N Then
ReadLine = S
Close #1
Exit Function
End If
Wend
Close #1
If Len(ReadLine) = 0 Then
N = 1
ReadLine = S1
End If