繁体   English   中英

VB.NET将.Txt文件读取到多个文本框中

[英]VB.NET Read .Txt File Into Multiple Text Boxes

我有一个程序,可以在文本框中输入名称和整数值,然后将其保存到文件中。 那工作得很好,但是我需要帮助的部分是文件的读取。

数据被保存到文件中,就像csv文件一样。 例:

Test, 5, 5, 5
dadea, 5, 5, 5
das, 5, 5, 5
asd, 5, 5, 5
dsadasd, 5, 5, 5

我的问题是,如何将其读入表单上的多个文本框中? 名称(每行的第一个值)应进入其相应的名称文本框(即txtName0,txtName1,txtName2等),整数值也应进入其相应的文本框(txtCut1,txtColour1等)。

我花了过去的2个小时试图弄清楚这一点,但我不能。

我需要帮助的部分是最后一种方法。

Imports System.IO

Public Class Form1
    Private aPricesGrid(,) As TextBox
    Private aAverages() As TextBox
    Private aNames() As TextBox

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        aPricesGrid = {{txtCut1, txtColour1, txtUpDo1, txtHighlight1, txtExtensions1},
                       {txtCut2, txtColour2, txtUpDo2, txtHighlight2, txtExtensions2},
                       {txtCut3, txtColour3, txtUpDo3, txtHighlight3, txtExtensions3},
                       {txtCut4, txtColour4, txtUpDo4, txtBHighlight4, txtExtensions4},
                       {txtCut5, txtColour5, txtUpDo5, txtHighlight5, txtExtensions5}}
        aAverages = {txtAvg0, txtAvg1, txtAvg2, txtAvg3, txtAvg4}
        aNames = {txtName0, txtName1, txtName2, txtName3, txtName4}
    End Sub

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        For nCol As Integer = 0 To 4
            Dim nColTotal As Integer = 0
            Dim nColItems As Integer = 0
            For nRow As Integer = 0 To 2
                Try
                    nColTotal += aPricesGrid(nRow, nCol).Text
                    nColItems += 1
                Catch ex As Exception

                End Try
            Next
            aAverages(nCol).Text = (nColTotal / nColItems).ToString("c")

        Next
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        For Each txt As Control In Me.Controls
            If TypeOf txt Is TextBox Then
                txt.Text = String.Empty
            End If
        Next
    End Sub

    Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
        Dim oSaveFileDialog As New SaveFileDialog()
        'Display the Common file Dialopg to user 
        oSaveFileDialog.Filter = "Text Files (*.txt)|*.txt"
        If oSaveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then

            ' Retrieve Name and open it 
            Dim fileName As String = oSaveFileDialog.FileName
            Dim outputFile As StreamWriter = File.CreateText(fileName)

            For nRow As Integer = 0 To 4
                outputFile.Write(aNames(nRow).Text)
                For nCol As Integer = 0 To 2
                    outputFile.Write(", " & aPricesGrid(nRow, nCol).Text)
                Next
                outputFile.WriteLine()
            Next
            outputFile.Close()
        End If
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub ReadToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ReadToolStripMenuItem.Click
        Dim oOpenFileDialog As New OpenFileDialog()
        'Display the Common file Dialopg to user 
        oOpenFileDialog.Filter = "Text Files (*.txt)|*.txt"

        If oOpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then

            Dim fileName As String = oOpenFileDialog.FileName
            Dim OpenFile As StreamReader = File.OpenText(fileName)

        End If

    End Sub
End Class

试试这个方法。

        Dim mindex as integer
        Dim string1 as string()
        Dim OpenFile As StreamReader = File.OpenText(fileName)
         While OpenFile .Peek <> -1
                string1= OpenFile .ReadLine.Split(",")
                If mindex = 0 Then
                    txtname1.text=string1(0)
                    txtcut1.text=string1(1)
                    txtcolour1.text=string1(2)
                    'add other textboxes
                ElseIf mindex = 1 Then
                    txtname2.text=string1(0)
                    txtcut2.text=string1(1)
                    txtcolour2.text=string1(2)
                    'add other textboxes
                ElseIf mindex = 2 Then
                    txtname3.text=string1(0)
                    txtcut3.text=string1(1)
                    txtcolour3.text=string1(2)
                    'add other textboxes
                ElseIf mindex = 3 Then
                  'your code
                ElseIf mindex = 4 Then
                   'your code
                End If
                mindex += 1
            End While
            OpenFile .Close()

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM