我试图从txt文件中读取,然后在相应的文本框中显示文件的内容,这些字段用逗号分隔。但是,它只是读取文件的第一行,我不知道如何纠正这一点。这是我的vb.net代码:
FileOpen(1, "MembersArea.txt", OpenMode.Input) 'OPEN FILE FOR READING'
Dim crosscheck As String
Dim flag As Boolean
crosscheck = Val(txtCrosscheck.Text)
flag = False
While Not EOF(1) 'OPEN FILE AT BEGINNING'
Input(1, Member_ID) 'READ LINE
Input(1, Forename)
Input(1, Surname)
Input(1, Addressline1)
Input(1, Addressline2)
Input(1, Town)
Input(1, County)
Input(1, Postcode)
If Member_ID = crosscheck Then
flag = True
Else
End If
Exit While
End While
If flag = True Then
txtCrosscheck.Enabled = False
btncrosscheck.Enabled = False
btnaddlog.Enabled = True
cmbevent.Enabled = True
dtpevent.Enabled = True
txtheartratebefore.Enabled = True
txtheartrateafter.Enabled = True
cmbevent.Focus()
Else
MsgBox("Member ID does not exist. Please enter an existing Member ID to add Training Log")
End If
nameconcat = Forename & " " & Surname
FileClose(1)
谢谢你!
答案 0 :(得分:0)
我不明白你的问题。 我想我明白文件的第一行包含由","分隔的各种信息。像这样:"一,二,三,四,五"并且您想要提取这些字段并将它们放在单独的文本框中。 将这些字段分开是足够的拆分命令
Dim riga As String = "one,two,three,four,five"
Dim fields() As String = riga.Split(",")
现在:
fields(0)="one"
fields(1)="two"
fields(2)="three"
fields(3)="four"
fields(4)="five"
这样:
textbox1.text=fields(0)
etc.
答案 1 :(得分:-1)
正如John Koerner所写,你正在调用Exit While,它将在第一次迭代后退出。
另外,在VB.NET中,我认为最好为此目的使用streamreader。 编辑:注意,您需要为System.IO添加导入才能使用streamreader。
您可以在代码顶部使用此行添加该导入:
Imports System.IO
我在编写和阅读CSV方面没有太多经验,所以我创建了一个函数来读取直到下一个逗号。 使用Private声明一个streamreader。
Private streamMembersArea As StreamReader
Private Function ReadInfo()
Dim strField As String = ""
Do
strField += streamMembersArea.Read()
Loop Until strField.Contains(",")
strField = strField.Remove(strField.Length - 1, 1)
Return strField
End Function
这实际上应该不需要你打电话给Not EOF,但是如果你不确定所有字段都会输入到文件中,我建议你检查它......
你可以通过致电:
来做到这一点While streamMembersArea.Peek <> -1 'Checks whether there is another character available, if not (like at the end of a file), it returns -1
... ' Place the following code here
...
End While
您可以通过以下方式执行此操作:
'Declare variables
Dim crosscheck As String
Dim flag As Boolean
'Removed Dim for streamReader, now a private, above
'crosscheck
crosscheck = Val(txtCrosscheck.Text)
'flag
flag = False
'Open file for input
streamMembersArea = IO.File.OpenText("MembersArea.txt")
'Read info into variables
Member_ID = ReadInfo()
Forename = ReadInfo()
Surname = ReadInfo()
Addressline1 = ReadInfo()
Addressline2 = ReadInfo()
Town = ReadInfo()
County = ReadInfo()
Postcode = ReadInfo()
'Close stream
streamMembersArea.Close() ' Closes the file, make sure you include this.
'Validate Member ID
If Member_ID = crosscheck Then
flag = True
Else ' Not needed, but fine either way
End If
'Exit While <- Commented out, no more 'While' loop, unnecessary now
'End While <- Not used anymore either
If flag = True Then
txtCrosscheck.Enabled = False
btncrosscheck.Enabled = False
btnaddlog.Enabled = True
cmbevent.Enabled = True
dtpevent.Enabled = True
txtheartratebefore.Enabled = True
txtheartrateafter.Enabled = True
cmbevent.Focus()
Else
MsgBox("Member ID does not exist. Please enter an existing Member ID to add Training Log")
End If
nameconcat = Forename & " " & Surname
'FileClose(1) <- No longer needed, closed stream earlier
如果您需要任何其他帮助,请与我们联系。