2015-11-27 2 views
0

У меня есть программа, в которую я могу ввести имена и целочисленные значения в текстовые поля, а затем сохранить их в файл. Это работает отлично, но мне нужна помощь, это чтение файла.VB.NET Читайте .Txt-файл в несколько текстовых полей

Данные сохраняются в файле, как файл csv. пример:

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

Моя проблема в том, как я могу это прочитать в нескольких текстовых полях на моей форме? Имена (первое значение каждой строки) должны входить в соответствующее текстовое поле Name (т. Е. 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 

ответ

1

Попробуйте этот метод.

 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() 

надеюсь, что это поможет.

+0

Спасибо! Это сработало – Jim

Смежные вопросы