vb.net How to place information from a txt file into a .text

  • mcavady
  • Newbie
  • Newbie
  • mcavady
  • Posts: 11
  • Loc: uk

Post 3+ Months Ago

ok I have a txt file that is in the debug folder, I have managed to get the information to the .text box's on my form but I now would like to add a previous and next button to step from one line in the text file to the next and vice versa. I have become a little stuck as I have only been using vb for a short time. Although the msdn libary has been very usfull I have become a little stuck and would like some help.

Code: [ Select ]
Private Sub ReadTextFile()
    Using TxtFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(FILE_PATH) 'for the reading of the txtfile
      TxtFileReader.TextFieldType = FileIO.FieldType.Delimited ' says that the file is deliminated
      TxtFileReader.SetDelimiters(",") ' denotes the seperator to the function
      Dim currentRow As String()
      Dim currentField As String
      Dim fieldCount As Integer 'counts the fileds so that they can be placed in the txt boxes
      Dim pass As Boolean = False
      Dim lineCount As Integer

      While Not TxtFileReader.EndOfData ' reads util the end of the data in the txtfile
        Try
          currentRow = TxtFileReader.ReadFields()

          If pass = False Then 'to stop the loop

            For Each currentField In currentRow 'if statments to assign the txt data to that from the file
              fieldCount = fieldCount + 1
              If fieldCount = 1 Then
                TxtBoxTitle.Text = currentField
              End If
              If fieldCount = 2 Then
                TxtBoxDirector.Text = currentField
              End If
              If fieldCount = 3 Then
                TxtYearOfRelease.Text = currentField
              End If
              If fieldCount = 4 Then
                TxtFilmFormat.Text = currentField
              End If
              If fieldCount = 5 Then
                TxtFilmRating.Text = currentField
              End If
              MsgBox(currentField)
            Next
            pass = True
          End If
          lineCount = lineCount + 1
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException 'catchs any erros and show a message to the user
          MsgBox("Line " & ex.Message & "is not valid and will be skipped!")
        End Try

      End While

    End Using

    'add routen to work with te next button ! and not a loop that goes though all the dvd info text file
    ' Read and display the lines from the file until the end
    ' of the file is reached.

  End Sub
  1. Private Sub ReadTextFile()
  2.     Using TxtFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(FILE_PATH) 'for the reading of the txtfile
  3.       TxtFileReader.TextFieldType = FileIO.FieldType.Delimited ' says that the file is deliminated
  4.       TxtFileReader.SetDelimiters(",") ' denotes the seperator to the function
  5.       Dim currentRow As String()
  6.       Dim currentField As String
  7.       Dim fieldCount As Integer 'counts the fileds so that they can be placed in the txt boxes
  8.       Dim pass As Boolean = False
  9.       Dim lineCount As Integer
  10.       While Not TxtFileReader.EndOfData ' reads util the end of the data in the txtfile
  11.         Try
  12.           currentRow = TxtFileReader.ReadFields()
  13.           If pass = False Then 'to stop the loop
  14.             For Each currentField In currentRow 'if statments to assign the txt data to that from the file
  15.               fieldCount = fieldCount + 1
  16.               If fieldCount = 1 Then
  17.                 TxtBoxTitle.Text = currentField
  18.               End If
  19.               If fieldCount = 2 Then
  20.                 TxtBoxDirector.Text = currentField
  21.               End If
  22.               If fieldCount = 3 Then
  23.                 TxtYearOfRelease.Text = currentField
  24.               End If
  25.               If fieldCount = 4 Then
  26.                 TxtFilmFormat.Text = currentField
  27.               End If
  28.               If fieldCount = 5 Then
  29.                 TxtFilmRating.Text = currentField
  30.               End If
  31.               MsgBox(currentField)
  32.             Next
  33.             pass = True
  34.           End If
  35.           lineCount = lineCount + 1
  36.         Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException 'catchs any erros and show a message to the user
  37.           MsgBox("Line " & ex.Message & "is not valid and will be skipped!")
  38.         End Try
  39.       End While
  40.     End Using
  41.     'add routen to work with te next button ! and not a loop that goes though all the dvd info text file
  42.     ' Read and display the lines from the file until the end
  43.     ' of the file is reached.
  44.   End Sub

This is the code I have so far I hope that some one can help, Thanks for your time James (Degree student)
  • mcavady
  • Newbie
  • Newbie
  • mcavady
  • Posts: 11
  • Loc: uk

Post 3+ Months Ago

ok I want to insert a if statment to this and get the "currentLine" to be "currentLine + 1" How ever I cannot seem to do this due to the current line being a string and not and integer.

what I would like to insert

Code: [ Select ]
  MsgBox(currentLine) ' for testing purpose
      [color=#FF0000]If pass = True Then
        currentRow = currentRow + 1
      End If[/color]
      Try
        currentRow = TxtFileReader.ReadFields()
        If pass = False Then 'to stop the loop
          'in here use the line txtFileReader.linenumber to go tothe line the is next from the current
          For Each currentField In currentRow  'if statments to assign the txt data to that from the file
            fieldCount = fieldCount + 1
            If fieldCount = 1 Then
              TxtBoxTitle.Text = currentField
            End If
  1.   MsgBox(currentLine) ' for testing purpose
  2.       [color=#FF0000]If pass = True Then
  3.         currentRow = currentRow + 1
  4.       End If[/color]
  5.       Try
  6.         currentRow = TxtFileReader.ReadFields()
  7.         If pass = False Then 'to stop the loop
  8.           'in here use the line txtFileReader.linenumber to go tothe line the is next from the current
  9.           For Each currentField In currentRow  'if statments to assign the txt data to that from the file
  10.             fieldCount = fieldCount + 1
  11.             If fieldCount = 1 Then
  12.               TxtBoxTitle.Text = currentField
  13.             End If


I think that this is a fix to my problem but I am still having a bit of difficulty with this. Does anybody have any suggestions?
  • mcavady
  • Newbie
  • Newbie
  • mcavady
  • Posts: 11
  • Loc: uk

Post 3+ Months Ago

ok Here is the solution that I have writen, however there are still some little bugs to be fixed.
I first placed all the informtion in to an array with the following code.
Code: [ Select ]
Public Sub UpdateTmpArray()

    Using TxtFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(FILE_PATH) 'for the reading of the txtfile
      TxtFileReader.TextFieldType = FileIO.FieldType.Delimited ' says that the file is deliminated
      TxtFileReader.SetDelimiters(",") ' denotes the seperator to the function
      Dim currentRow As String() 'to go to the next array in the txt file
      Dim currentField As String 'for the string of the current field to be used
      Dim fieldCount As Integer 'counts the fileds so that they can be placed in the txt boxes
      Dim linecount As Integer 'counts the number of lines in the txt file
      Dim TmpCount As Integer 'to count the number in the array
      'this should fill an array and then the array should be then used for the search function and the next function and the sort function.

      Do Until TxtFileReader.LineNumber = -1
        Try
          ' MsgBox(TxtFileReader.LineNumber) ' for testing
          'MsgBox(TmpCount) 'for testing, shows the number of the last record in the array, should increment up by 6

          currentRow = TxtFileReader.ReadFields()
          For Each currentField In currentRow  'if statments to assign the txt data to that from the file for the current field
            fieldCount = fieldCount + 1 'film tmpArray
            If fieldCount = 1 Then
              'Film ID
              tmpArrayFilmList(TmpCount) = currentField 'sets location in the array to the current field
              TmpCount = TmpCount + 1 'for counting the place in the array
            End If
            If fieldCount = 2 Then
              ' Film Title
              tmpArrayFilmList(TmpCount) = currentField
              TmpCount = TmpCount + 1
            End If
            If fieldCount = 3 Then
              'Film YearOfRelease
              tmpArrayFilmList(TmpCount) = currentField
              TmpCount = TmpCount + 1
            End If
            If fieldCount = 4 Then
              'Film Director
              tmpArrayFilmList(TmpCount) = currentField
              TmpCount = TmpCount + 1
            End If
            If fieldCount = 5 Then
              'Film Format
              tmpArrayFilmList(TmpCount) = currentField
              TmpCount = TmpCount + 1
            End If

            If fieldCount = 6 Then
              'Film Rating
              tmpArrayFilmList(TmpCount) = currentField
              TmpCount = TmpCount + 1

            End If

          Next
          fieldCount = 0 ' needs this to move onto the next film
          linecount = linecount + 1
          NumDvdsPreNext = linecount
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException 'catchs any erros and shows a message to the user
          MsgBox("Line " & ex.Message & "is not valid and will be skipped!")
        Finally
          'enything esle that is needed

        End Try

      Loop

    End Using

  End Sub
  1. Public Sub UpdateTmpArray()
  2.     Using TxtFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(FILE_PATH) 'for the reading of the txtfile
  3.       TxtFileReader.TextFieldType = FileIO.FieldType.Delimited ' says that the file is deliminated
  4.       TxtFileReader.SetDelimiters(",") ' denotes the seperator to the function
  5.       Dim currentRow As String() 'to go to the next array in the txt file
  6.       Dim currentField As String 'for the string of the current field to be used
  7.       Dim fieldCount As Integer 'counts the fileds so that they can be placed in the txt boxes
  8.       Dim linecount As Integer 'counts the number of lines in the txt file
  9.       Dim TmpCount As Integer 'to count the number in the array
  10.       'this should fill an array and then the array should be then used for the search function and the next function and the sort function.
  11.       Do Until TxtFileReader.LineNumber = -1
  12.         Try
  13.           ' MsgBox(TxtFileReader.LineNumber) ' for testing
  14.           'MsgBox(TmpCount) 'for testing, shows the number of the last record in the array, should increment up by 6
  15.           currentRow = TxtFileReader.ReadFields()
  16.           For Each currentField In currentRow  'if statments to assign the txt data to that from the file for the current field
  17.             fieldCount = fieldCount + 1 'film tmpArray
  18.             If fieldCount = 1 Then
  19.               'Film ID
  20.               tmpArrayFilmList(TmpCount) = currentField 'sets location in the array to the current field
  21.               TmpCount = TmpCount + 1 'for counting the place in the array
  22.             End If
  23.             If fieldCount = 2 Then
  24.               ' Film Title
  25.               tmpArrayFilmList(TmpCount) = currentField
  26.               TmpCount = TmpCount + 1
  27.             End If
  28.             If fieldCount = 3 Then
  29.               'Film YearOfRelease
  30.               tmpArrayFilmList(TmpCount) = currentField
  31.               TmpCount = TmpCount + 1
  32.             End If
  33.             If fieldCount = 4 Then
  34.               'Film Director
  35.               tmpArrayFilmList(TmpCount) = currentField
  36.               TmpCount = TmpCount + 1
  37.             End If
  38.             If fieldCount = 5 Then
  39.               'Film Format
  40.               tmpArrayFilmList(TmpCount) = currentField
  41.               TmpCount = TmpCount + 1
  42.             End If
  43.             If fieldCount = 6 Then
  44.               'Film Rating
  45.               tmpArrayFilmList(TmpCount) = currentField
  46.               TmpCount = TmpCount + 1
  47.             End If
  48.           Next
  49.           fieldCount = 0 ' needs this to move onto the next film
  50.           linecount = linecount + 1
  51.           NumDvdsPreNext = linecount
  52.         Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException 'catchs any erros and shows a message to the user
  53.           MsgBox("Line " & ex.Message & "is not valid and will be skipped!")
  54.         Finally
  55.           'enything esle that is needed
  56.         End Try
  57.       Loop
  58.     End Using
  59.   End Sub


I then used two loops for getting the information out of the array to the forms. with the following code.

Code: [ Select ]
Private Sub nextDvdBtn() 'to navigate to the next record in the array and show on the main GUI form
    Dim i As Integer 'for counting the number of times the loop repeats
     'if statement for the cyling throught the array

    If tmpArrayFilmList(count) = Nothing Then ' this is the make sure that the fields are placed in the correct place it is 5 as the count starts @ 0

      count = 0 'there are 6 records for each film so this has to be six
      ' MsgBox("You have reached the end of the list")'for testing
    End If
    ' MsgBox(count)'for testing to show the conts for working out the reset of count to 0 for the next button to loop round the records
    For i = 1 To 1
      TxtBoxFilmId.Text = tmpArrayFilmList(count) 'takes the field stored in the array and places into the text box
      count = count + 1 'used to count the location in the array
      TxtBoxTitle.Text = tmpArrayFilmList(count)
      count = count + 1
      TxtYearOfRelease.Text = tmpArrayFilmList(count)
      count = count + 1
      TxtBoxDirector.Text = tmpArrayFilmList(count)
      count = count + 1
      TxtFilmFormat.Text = tmpArrayFilmList(count)
      count = count + 1
      TxtFilmRating.Text = tmpArrayFilmList(count)
      count = count + 1
    Next

  End Sub
  1. Private Sub nextDvdBtn() 'to navigate to the next record in the array and show on the main GUI form
  2.     Dim i As Integer 'for counting the number of times the loop repeats
  3.      'if statement for the cyling throught the array
  4.     If tmpArrayFilmList(count) = Nothing Then ' this is the make sure that the fields are placed in the correct place it is 5 as the count starts @ 0
  5.       count = 0 'there are 6 records for each film so this has to be six
  6.       ' MsgBox("You have reached the end of the list")'for testing
  7.     End If
  8.     ' MsgBox(count)'for testing to show the conts for working out the reset of count to 0 for the next button to loop round the records
  9.     For i = 1 To 1
  10.       TxtBoxFilmId.Text = tmpArrayFilmList(count) 'takes the field stored in the array and places into the text box
  11.       count = count + 1 'used to count the location in the array
  12.       TxtBoxTitle.Text = tmpArrayFilmList(count)
  13.       count = count + 1
  14.       TxtYearOfRelease.Text = tmpArrayFilmList(count)
  15.       count = count + 1
  16.       TxtBoxDirector.Text = tmpArrayFilmList(count)
  17.       count = count + 1
  18.       TxtFilmFormat.Text = tmpArrayFilmList(count)
  19.       count = count + 1
  20.       TxtFilmRating.Text = tmpArrayFilmList(count)
  21.       count = count + 1
  22.     Next
  23.   End Sub


Code: [ Select ]
Private Sub previousDvdBtn() 'to navigate to the previous record in the array and display the result on the mainGui form
    Dim i As Integer 'to count the number of times the loop repeats
    'if statement for the cyling throught the array
    If count <= 5 Then ' this is the make sure that the fields are placed in the correct place it is 5 as the count starts @ 0
      count = (NumDvdsPreNext * 6) 'there are 6 records for each film so this has to be six
      ' MsgBox("You have reached the end of the list")'for testing
    End If
    For i = 1 To 1
      count = count - 1
      TxtFilmRating.Text = tmpArrayFilmList(count)
      count = count - 1
      TxtFilmFormat.Text = tmpArrayFilmList(count)
      count = count - 1
      TxtBoxDirector.Text = tmpArrayFilmList(count)
      count = count - 1
      TxtYearOfRelease.Text = tmpArrayFilmList(count)
      count = count - 1
      TxtBoxTitle.Text = tmpArrayFilmList(count)
      count = count - 1
      TxtBoxFilmId.Text = tmpArrayFilmList(count)

    Next

  End Sub
  1. Private Sub previousDvdBtn() 'to navigate to the previous record in the array and display the result on the mainGui form
  2.     Dim i As Integer 'to count the number of times the loop repeats
  3.     'if statement for the cyling throught the array
  4.     If count <= 5 Then ' this is the make sure that the fields are placed in the correct place it is 5 as the count starts @ 0
  5.       count = (NumDvdsPreNext * 6) 'there are 6 records for each film so this has to be six
  6.       ' MsgBox("You have reached the end of the list")'for testing
  7.     End If
  8.     For i = 1 To 1
  9.       count = count - 1
  10.       TxtFilmRating.Text = tmpArrayFilmList(count)
  11.       count = count - 1
  12.       TxtFilmFormat.Text = tmpArrayFilmList(count)
  13.       count = count - 1
  14.       TxtBoxDirector.Text = tmpArrayFilmList(count)
  15.       count = count - 1
  16.       TxtYearOfRelease.Text = tmpArrayFilmList(count)
  17.       count = count - 1
  18.       TxtBoxTitle.Text = tmpArrayFilmList(count)
  19.       count = count - 1
  20.       TxtBoxFilmId.Text = tmpArrayFilmList(count)
  21.     Next
  22.   End Sub


I hope this helps people, and if you have any suggestions on how I may make this code more tidy I would like to c your suggestions.

Thanks

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 61 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.