Splitting Data in a File and Implementing Next Button
Splitting Data in a File and Implementing Next Button
My question is in two parts.
I can output data from a text file, but it outputs as a continuous stream(each line in the text file until the end). I need to split each line by spaces so they are more legible. They are separated in the text file with vbTab
Second instead of outputting everything in the file, is there a way I can use a next button to cycle through each line.
I can't write the code right now but I have images of it.
Here is the code:
Public Class frmDisplay
Dim contact As String = ""
Private Sub frmDisplay_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim display As StreamReader
display = New StreamReader("contacts.txt")
Do While display.Peek <> -1
contact = contact & display.ReadLine & vbNewLine
lblName.Text = contact
Loop
display.Close()
Call splitDetails()
End Sub
Private Sub SplitDetails()
Dim contactInfo As String = lblName.Text
Dim allContacts() As String = contactInfo.Split(vbTab) 'splits everytime it encouters a space
Dim contactItem As String
For Each contactItem In allContacts ' each wprd in the array word
lblName.Text = contactItem
Next
End Sub
End Class
I apologize for adding images earlier
"split each entry"...into what? Separate lines? Or by some other character? You can google how to do either of those when reading from a stream. or you can output the text file as a single string, instead, and work on that. Again you can google easily for a code sample. Once you've split your text into whatever sections you're intending, you can use a loop to iterate through them and decide which ones to output.
– ADyson
Aug 28 at 14:24
Please don't post images of code, we can't use them to reproduce a problem or copy to make an example for you. Additionally, no-one can search in future and discover your code is similar to theirs. If you can see it on screen, you can copy/paste it into StackOverlfow just as easily (or perhaps more easily!)
– ADyson
Aug 28 at 14:25
You'll need to change the scope of the
allContacts
array so that it is a field of the whole class (i.e. a form-level variable) rather than a local variable of the method. Then, in the btnNext_Click
method, you'll be able to read the appropriate item out of that array and display it.– Steven Doggart
Aug 28 at 14:30
allContacts
btnNext_Click
So declare allContacts at the beginning?
– Dante
Aug 28 at 15:48
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you reading your file line by line? Have you looked at a Split method? Could you read all the lines into an array and implement your own "paging" solution by parsing/looping through the array?
– Andrew Mortimer
Aug 28 at 14:24