vb怎样查找文本文件每一行的第一个字符

来源:百度知道 编辑:UC知道 时间:2024/05/04 19:17:58
比如:
文本文件1.txt 的内容是:
a 12345
b 23456
c 47859
我在输入框text1.text输入:a
按确定
那么输入框text2.text输出:12345
我在输入框text1.text输入:b
按确定
那么输入框text2.text输出:23456
要怎么实现?

Sub FindKeyword(ByVal kw As String)
Dim Stream$, filepath$ '建立变量
filepath = IIf(Right$(App.Path, 1) <> "\" And Right$(App.Path, 1) <> "/", App.Path & "\answer.txt", App.Path & "answer.txt")
'生成读入文本路径
Open filepath For Input As #1 '读取
Do While Not EOF(1) '直到文件尾部
Line Input #1, Stream '行模式读取
If Not EOF(1) Then '判断是否到最后一行
Line Input #1, Stream '读取下一行
If Mid$(Stream, 1, 1) = Text1.Text Then
Text2.Text = Mid$(Stream, 2) '输出结果
End If
Else
Exit Do '直接退出do循环
End If
Stream = "" '初始化变量 : 清空
Loop
Close #1
End Sub

对于你这道题,我觉得最好还是用读写INI文件来实现,要比你这个容易多了
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA