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


- Joined: Jun 04, 2009
- Posts: 11
- Loc: uk
- Status: Offline
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.
This is the code I have so far I hope that some one can help, Thanks for your time James (Degree student)
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
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
- 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
This is the code I have so far I hope that some one can help, Thanks for your time James (Degree student)
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
March 12th, 2010, 8:53 am
- mcavady
- Newbie


- Joined: Jun 04, 2009
- Posts: 11
- Loc: uk
- Status: Offline
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
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?
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
[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
- 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
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


- Joined: Jun 04, 2009
- Posts: 11
- Loc: uk
- Status: Offline
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.
I then used two loops for getting the information out of the array to the forms. with the following code.
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
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
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
- 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
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
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
- 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
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
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
- 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
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
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 3 posts
- Users browsing this forum: No registered users and 139 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
