一个VB类问题,高手请进

2024-12-12 14:51:28
推荐回答(5个)
回答1:

呵呵,我把你给的链接里面的代码做了下小小的修改。。测试可用。。
Private Sub Form_Load()
Dim path As String
path = App.path & IIf(Right(App.path, 1) = "\", "", "\")
Me.Label1.Caption = "222f"
If Dir(path & "1.txt") <> "" Then
Open path & "1.txt" For Input As #1
Dim arr, i As Long
arr = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbNewLine)
Close #1
For i = 0 To UBound(arr)
If Len(arr(i)) > 0 Then
If Split(arr(i), " ")(1) = Label1 Then
Label2 = Split(arr(i), " ")(0)
Exit Sub
End If
End If
Next
MsgBox "文件中未找到有关 " & Label1 & " 的内容! "
Else
MsgBox "未找到文件:" & path & "1.txt"
Unload Me
End If
End Sub

回答2:

Option ExplicitPrivate Type KeyValue
Key As String
Value As String
End Type
Dim Lists() As KeyValue, ListCount As Integer
Private Sub Form_Load()
End Sub
Function OpenFile(FilePath As String) As Boolean
If Len(Dir(FilePath)) = 0 Then Exit Function
Dim ss() As String, a As String
Open FilePath For Input As #1
Do While EOF(1)
Line Input #1, a
ss = Split(a, " ")
If UBound(ss) = 1 Then
Dim s As KeyValue
s.Key = ss(1)
s.Value = ss(0)
ReDim Preserve Lists(ListCount)
Lists(ListCount) = s
ListCount = ListCount + 1
End If
Loop
Close #1
OpenFile = True
End Function
Function GetValue(Key As String) As String
On Error GoTo E
For Each i In Lists
If i.Key = Key Then
GetValue = i.Value
Exit Function
End If
Next
Exit Function
E:
End Function

试下可以用不.....

回答3:

添加2个标签和2个按钮:
代码如下:
Option Explicit
Private d1() As String
Private d2() As String

'按钮1完成读取文本文件存入数组
Private Sub Command1_Click()
Dim LinStr As String
Dim a() As String
Dim n As Integer
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'读取文本文件存放到数组
n = 0
Open "c:\md.txt" For Input As #1
Do While Not EOF(1)
n = n + 1
ReDim Preserve d1(1 To n) As String
ReDim Preserve d2(1 To n) As String
Line Input #1, LinStr
a = Split(LinStr, " ")
d1(n) = a(0)
d2(n) = a(1)
Loop
Close #1
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
End Sub

'按钮2完成判断是否匹配,并在Label2里显示
Private Sub Command2_Click()
Dim i As Integer
For i = 1 To UBound(d2)
If Label1.Caption = d2(i) Then
Label2.Caption = d1(i)
Exit For
End If
Next i
End Sub

回答4:

可以用下面的代码实现

Dim a1(10), a2(10) As String
Dim index As Integer

Private Sub Command1_Click()
index = 1
Open "md.txt" For Input As 1
Do While Not EOF(1)
Line Input #1, myline
a1(index) = Left(myline, 3): a2(index) = Right(myline, 4)
index = index + 1
Loop
Close #1
For i = 1 To index
If a(i) = Label1 Then Label2 = a2(i): Exit For
Next i
End Sub

大致可以这么实现吧

回答5:

这个没法实现,除非你让一行里只有一个单词。