从文本文件填充 vb.net 中的多个文本字段
Populating multiple text fields in vb.net from a text file
我正在尝试使用 VB.net 从文本文件中填充表单上的 5 个框。文本文件是发送给我的报告,其中包含未知行数,因为每天的事件都不同。在文本文件中,每行包含 5 个项目,用“~”分隔。
即:
账号~姓名~Phone~邮箱~数据
我设置的表单只是一个带有 5 个文本框和 2 个按钮的简单表单。 (除了文件菜单打开txt文件)。
这 2 个按钮用于“上一条记录”和“下一条记录”功能。他们会做显而易见的事情。
这是我目前的代码。这一切都在文件 > 打开菜单项中(这本身可能是错误的)所以我包括了整个设置。
我让它一次为每行中的每个项目弹出一个消息框。此外,它通过从 0 计数到 4 进行跟踪,所以我知道它何时回到第一项。在测试中,这有效。
我需要弄清楚如何让第一行的所有 5 项都显示在文本框中,然后使“下一页”和“上一页”按钮转到下一行或上一行并填充文本那些盒子。我尝试过的每个过程都惨遭失败。
如有任何帮助,我们将不胜感激。
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
Dim ofd As OpenFileDialog = New OpenFileDialog
ofd.DefaultExt = "txt"
ofd.FileName = "defaultname"
ofd.InitialDirectory = "C:\users\%username%\desktop\"
ofd.Filter = "Text files|*.txt"
ofd.Title = "Select file"
If ofd.ShowDialog() <> DialogResult.Cancel Then
Using myreader As New Microsoft.VisualBasic.FileIO.TextFieldParser(ofd.FileName)
myreader.TextFieldType = FileIO.FieldType.Delimited
myreader.SetDelimiters("~")
Dim currentrow As String()
While Not myreader.EndOfData
Try
currentrow = myreader.ReadFields()
Dim currentfield As String
For Each currentfield In currentrow
Dim count As Integer
If count = 4 Then
count = 0
Else
' populate form
MsgBox(currentfield & " - " & count)
count = count + 1
End If
Next
Catch ex As Microsoft.VisualBasic.
fileio.MalformedLineException
MsgBox("line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
End Using
End If
End Sub
将其插入您的代码中以获得正确的字段! !
If ofd.ShowDialog() <> DialogResult.Cancel Then
Using myreader As New Microsoft.VisualBasic.FileIO.TextFieldParser(ofd.FileName)
myreader.TextFieldType = FileIO.FieldType.Delimited
myreader.SetDelimiters("~")
Dim x As Short
Dim currentrow As String()
While Not myreader.EndOfData
Try
currentrow = myreader.ReadFields()
Dim Count As Short = 0
For x = 0 To currentrow.Count - 1
Count = Count + 1
MsgBox(currentrow(x) & " - " & Count)
Next
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
End Using
End If
你也可以这样做:-
您也可以使用我上面显示的逻辑,但首先将所有行放入一个数组中 - 然后处理每一行:-
Dim Lines = File.ReadAllLines(ofd.FileName)
Dim Lp As Single
Dim X As Short
For Lp = 0 To Lines.Length - 1
Dim items() As String = Split(Lines(Lp), "~")
Dim Count As Short = 0
For x = 0 To items.Count - 1
Count = Count + 1
MsgBox(items(X) & " - " & Count)
Next
Next
您需要将每一行的所有字符串数组存储在导入方法外部的结构中,以便您可以向前and/or向后移动它们。
这里我将它们存储在 List(Of String())
:
Public Class Form1
Private _dataIndex As Integer = -1
Public Property DataIndex As Integer
Get
Return _dataIndex
End Get
Set(value As Integer)
If value >= 0 AndAlso value < data.Count Then
_dataIndex = value
UpdateCurrentRecord()
End If
End Set
End Property
Private TextBoxes() As TextBox
Private data As New List(Of String())
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' ...change the names of the textboxes below...
TextBoxes = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5}
End Sub
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
Dim ofd As OpenFileDialog = New OpenFileDialog
ofd.DefaultExt = "txt"
ofd.FileName = "defaultname"
ofd.InitialDirectory = "C:\users\%username%\desktop\"
ofd.Filter = "Text files|*.txt"
ofd.Title = "Select file"
If ofd.ShowDialog() = DialogResult.OK Then
_dataIndex = -1
data.Clear()
UpdateCurrentRecord()
Using myreader As New Microsoft.VisualBasic.FileIO.TextFieldParser(ofd.FileName)
myreader.TextFieldType = FileIO.FieldType.Delimited
myreader.SetDelimiters("~")
Dim currentrow As String()
While Not myreader.EndOfData
Try
currentrow = myreader.ReadFields()
If currentrow.Length = 5 Then
data.Add(currentrow)
End If
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MessageBox.Show("Line " & ex.Message & " is not valid and will be skipped.")
End Try
End While
If data.Count > 0 Then
DataIndex = 0
End If
End Using
End If
End Sub
Private Sub UpdateCurrentRecord()
If _dataIndex >= 0 AndAlso _dataIndex < data.Count Then
Dim row() As String = data(_dataIndex)
If row.Length = 5 Then
For i As Integer = 0 To 4
TextBoxes(i).Text = row(i)
Next
End If
Else
For Each tb As TextBox In TextBoxes
tb.Clear()
Next
End If
End Sub
Private Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click
DataIndex = (DataIndex - 1)
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
DataIndex = (DataIndex + 1)
End Sub
End Class
以下代码适合我。
Private curIndex As Integer = -1
Private maxIndex As Integer = 0
Private dic As Dictionary(Of Integer, String()) = New Dictionary(Of Integer, String())
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim lines = File.ReadAllLines(filePath).Where(Function(x) Not String.IsNullOrWhiteSpace(x))
maxIndex = lines.Count - 1
For i As Integer = 0 To maxIndex
dic.Add(i, lines(i).Split("~"c).Where(Function(x) Not String.IsNullOrWhiteSpace(x)).ToArray())
Next
Label1.Text = "0"
curIndex = 0
TextBox1.Text = dic(0)(curIndex)
TextBox2.Text = dic(0)(curIndex + 1)
TextBox3.Text = dic(0)(curIndex + 2)
TextBox4.Text = dic(0)(curIndex + 3)
TextBox5.Text = dic(0)(curIndex + 4)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not curIndex - 1 < 0 Then
curIndex -= 1
Label1.Text = curIndex.ToString
TextBox1.Text = dic(curIndex)(0)
TextBox2.Text = dic(curIndex)(1)
TextBox3.Text = dic(curIndex)(2)
TextBox4.Text = dic(curIndex)(3)
TextBox5.Text = dic(curIndex)(4)
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Not curIndex + 1 > maxIndex Then
curIndex += 1
Label1.Text = curIndex.ToString
TextBox1.Text = dic(curIndex)(0)
TextBox2.Text = dic(curIndex)(1)
TextBox3.Text = dic(curIndex)(2)
TextBox4.Text = dic(curIndex)(3)
TextBox5.Text = dic(curIndex)(4)
End If
End Sub
您可以使用字典来保存行号和值。
我正在尝试使用 VB.net 从文本文件中填充表单上的 5 个框。文本文件是发送给我的报告,其中包含未知行数,因为每天的事件都不同。在文本文件中,每行包含 5 个项目,用“~”分隔。
即: 账号~姓名~Phone~邮箱~数据
我设置的表单只是一个带有 5 个文本框和 2 个按钮的简单表单。 (除了文件菜单打开txt文件)。
这 2 个按钮用于“上一条记录”和“下一条记录”功能。他们会做显而易见的事情。
这是我目前的代码。这一切都在文件 > 打开菜单项中(这本身可能是错误的)所以我包括了整个设置。
我让它一次为每行中的每个项目弹出一个消息框。此外,它通过从 0 计数到 4 进行跟踪,所以我知道它何时回到第一项。在测试中,这有效。
我需要弄清楚如何让第一行的所有 5 项都显示在文本框中,然后使“下一页”和“上一页”按钮转到下一行或上一行并填充文本那些盒子。我尝试过的每个过程都惨遭失败。
如有任何帮助,我们将不胜感激。
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
Dim ofd As OpenFileDialog = New OpenFileDialog
ofd.DefaultExt = "txt"
ofd.FileName = "defaultname"
ofd.InitialDirectory = "C:\users\%username%\desktop\"
ofd.Filter = "Text files|*.txt"
ofd.Title = "Select file"
If ofd.ShowDialog() <> DialogResult.Cancel Then
Using myreader As New Microsoft.VisualBasic.FileIO.TextFieldParser(ofd.FileName)
myreader.TextFieldType = FileIO.FieldType.Delimited
myreader.SetDelimiters("~")
Dim currentrow As String()
While Not myreader.EndOfData
Try
currentrow = myreader.ReadFields()
Dim currentfield As String
For Each currentfield In currentrow
Dim count As Integer
If count = 4 Then
count = 0
Else
' populate form
MsgBox(currentfield & " - " & count)
count = count + 1
End If
Next
Catch ex As Microsoft.VisualBasic.
fileio.MalformedLineException
MsgBox("line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
End Using
End If
End Sub
将其插入您的代码中以获得正确的字段! !
If ofd.ShowDialog() <> DialogResult.Cancel Then
Using myreader As New Microsoft.VisualBasic.FileIO.TextFieldParser(ofd.FileName)
myreader.TextFieldType = FileIO.FieldType.Delimited
myreader.SetDelimiters("~")
Dim x As Short
Dim currentrow As String()
While Not myreader.EndOfData
Try
currentrow = myreader.ReadFields()
Dim Count As Short = 0
For x = 0 To currentrow.Count - 1
Count = Count + 1
MsgBox(currentrow(x) & " - " & Count)
Next
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
End Using
End If
你也可以这样做:-
您也可以使用我上面显示的逻辑,但首先将所有行放入一个数组中 - 然后处理每一行:-
Dim Lines = File.ReadAllLines(ofd.FileName)
Dim Lp As Single
Dim X As Short
For Lp = 0 To Lines.Length - 1
Dim items() As String = Split(Lines(Lp), "~")
Dim Count As Short = 0
For x = 0 To items.Count - 1
Count = Count + 1
MsgBox(items(X) & " - " & Count)
Next
Next
您需要将每一行的所有字符串数组存储在导入方法外部的结构中,以便您可以向前and/or向后移动它们。
这里我将它们存储在 List(Of String())
:
Public Class Form1
Private _dataIndex As Integer = -1
Public Property DataIndex As Integer
Get
Return _dataIndex
End Get
Set(value As Integer)
If value >= 0 AndAlso value < data.Count Then
_dataIndex = value
UpdateCurrentRecord()
End If
End Set
End Property
Private TextBoxes() As TextBox
Private data As New List(Of String())
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' ...change the names of the textboxes below...
TextBoxes = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5}
End Sub
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
Dim ofd As OpenFileDialog = New OpenFileDialog
ofd.DefaultExt = "txt"
ofd.FileName = "defaultname"
ofd.InitialDirectory = "C:\users\%username%\desktop\"
ofd.Filter = "Text files|*.txt"
ofd.Title = "Select file"
If ofd.ShowDialog() = DialogResult.OK Then
_dataIndex = -1
data.Clear()
UpdateCurrentRecord()
Using myreader As New Microsoft.VisualBasic.FileIO.TextFieldParser(ofd.FileName)
myreader.TextFieldType = FileIO.FieldType.Delimited
myreader.SetDelimiters("~")
Dim currentrow As String()
While Not myreader.EndOfData
Try
currentrow = myreader.ReadFields()
If currentrow.Length = 5 Then
data.Add(currentrow)
End If
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MessageBox.Show("Line " & ex.Message & " is not valid and will be skipped.")
End Try
End While
If data.Count > 0 Then
DataIndex = 0
End If
End Using
End If
End Sub
Private Sub UpdateCurrentRecord()
If _dataIndex >= 0 AndAlso _dataIndex < data.Count Then
Dim row() As String = data(_dataIndex)
If row.Length = 5 Then
For i As Integer = 0 To 4
TextBoxes(i).Text = row(i)
Next
End If
Else
For Each tb As TextBox In TextBoxes
tb.Clear()
Next
End If
End Sub
Private Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click
DataIndex = (DataIndex - 1)
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
DataIndex = (DataIndex + 1)
End Sub
End Class
以下代码适合我。
Private curIndex As Integer = -1
Private maxIndex As Integer = 0
Private dic As Dictionary(Of Integer, String()) = New Dictionary(Of Integer, String())
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim lines = File.ReadAllLines(filePath).Where(Function(x) Not String.IsNullOrWhiteSpace(x))
maxIndex = lines.Count - 1
For i As Integer = 0 To maxIndex
dic.Add(i, lines(i).Split("~"c).Where(Function(x) Not String.IsNullOrWhiteSpace(x)).ToArray())
Next
Label1.Text = "0"
curIndex = 0
TextBox1.Text = dic(0)(curIndex)
TextBox2.Text = dic(0)(curIndex + 1)
TextBox3.Text = dic(0)(curIndex + 2)
TextBox4.Text = dic(0)(curIndex + 3)
TextBox5.Text = dic(0)(curIndex + 4)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not curIndex - 1 < 0 Then
curIndex -= 1
Label1.Text = curIndex.ToString
TextBox1.Text = dic(curIndex)(0)
TextBox2.Text = dic(curIndex)(1)
TextBox3.Text = dic(curIndex)(2)
TextBox4.Text = dic(curIndex)(3)
TextBox5.Text = dic(curIndex)(4)
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Not curIndex + 1 > maxIndex Then
curIndex += 1
Label1.Text = curIndex.ToString
TextBox1.Text = dic(curIndex)(0)
TextBox2.Text = dic(curIndex)(1)
TextBox3.Text = dic(curIndex)(2)
TextBox4.Text = dic(curIndex)(3)
TextBox5.Text = dic(curIndex)(4)
End If
End Sub
您可以使用字典来保存行号和值。