|
-
Jul 28th, 2011, 04:32 PM
#1
Thread Starter
Lively Member
[RESOLVED] Help! Vb.net Reading a text file line by line
ok so i got vb to read the text file but it reads the whole thing and displays it on a text line,
how can i make it so it reads it like 1 line at a time after every click,
so if my text file is like this
hello
goodbye
then i dont want it to display both, i want it to display hello and if i click a button again then it displays goodbye,
Thanks in advance
-
Jul 28th, 2011, 04:52 PM
#2
Re: Help! Vb.net Reading a text file line by line
here's how. on load it reads your text file into a string array, then displays the lines 1 by 1 as you click the button:
vb Code:
Public Class Form1
Dim lines() As String
Dim index As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lines = IO.File.ReadAllLines("filename.txt")
End Sub
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
TextBox1.Text &= lines(index) & Environment.NewLine
index += 1
If index > lines.GetUpperBound(0) Then button1.Enabled = False
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 28th, 2011, 05:01 PM
#3
Re: Help! Vb.net Reading a text file line by line
vb Code:
' Public Class Form1 Private sr As IO.StreamReader Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Debug.Assert(Not IsNothing(sr)) ' checking whether we're not at the end yet If Not (sr.EndOfStream) Then ' Read the next line and display in in the texbox TextBox1.Text = sr.ReadLine() Else MessageBox.Show("Reached the end of file") End If End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing sr.Close() sr.Dispose() End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Using dlg As New OpenFileDialog With {.CheckFileExists = True} Dim ret As DialogResult = dlg.ShowDialog(Me) If ret = Windows.Forms.DialogResult.OK Then Try ' Open file for reading sr = New IO.StreamReader(New IO.FileStream(dlg.FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)) Catch ex As Exception ' Catching possible exceptions MessageBox.Show(ex.Message) End Try End If End Using End Sub End Class
-
Jul 28th, 2011, 05:12 PM
#4
Thread Starter
Lively Member
Re: Help! Vb.net Reading a text file line by line
 Originally Posted by .paul.
here's how. on load it reads your text file into a string array, then displays the lines 1 by 1 as you click the button:
vb Code:
Public Class Form1
Dim lines() As String
Dim index As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lines = IO.File.ReadAllLines("filename.txt")
End Sub
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
TextBox1.Text &= lines(index) & Environment.NewLine
index += 1
If index > lines.GetUpperBound(0) Then button1.Enabled = False
End Sub
End Class
thank you but it did not work as i intended it did not display the second part it only displayed the first part.
-
Jul 28th, 2011, 05:18 PM
#5
Thread Starter
Lively Member
Re: Help! Vb.net Reading a text file line by line
 Originally Posted by cicatrix
vb Code:
'
Public Class Form1
Private sr As IO.StreamReader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Debug.Assert(Not IsNothing(sr))
' checking whether we're not at the end yet
If Not (sr.EndOfStream) Then
' Read the next line and display in in the texbox
TextBox1.Text = sr.ReadLine()
Else
MessageBox.Show("Reached the end of file")
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
sr.Close()
sr.Dispose()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using dlg As New OpenFileDialog With {.CheckFileExists = True}
Dim ret As DialogResult = dlg.ShowDialog(Me)
If ret = Windows.Forms.DialogResult.OK Then
Try
' Open file for reading
sr = New IO.StreamReader(New IO.FileStream(dlg.FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read))
Catch ex As Exception
' Catching possible exceptions
MessageBox.Show(ex.Message)
End Try
End If
End Using
End Sub
End Class
Thank you but that did not work,my app crashed...
-
Jul 28th, 2011, 05:25 PM
#6
Re: Help! Vb.net Reading a text file line by line
 Originally Posted by M0NKEYRASH
thank you but it did not work as i intended it did not display the second part it only displayed the first part.
in your text file, does it look like this:
?
with each line on a new line?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 28th, 2011, 05:26 PM
#7
Re: Help! Vb.net Reading a text file line by line
What do you mean 'crashed'? At which line?
-
Jul 28th, 2011, 05:33 PM
#8
Thread Starter
Lively Member
Re: Help! Vb.net Reading a text file line by line
 Originally Posted by .paul.
in your text file, does it look like this:
?
with each line on a new line?
yes my text file did look like that but what the app did was just display the first line over and over but it did not display it once it did it like this, hellohellohello
everclick
-
Jul 28th, 2011, 05:38 PM
#9
Thread Starter
Lively Member
Re: Help! Vb.net Reading a text file line by line
 Originally Posted by cicatrix
What do you mean 'crashed'? At which line?
it did not crash that was my other app i was working on while i was waiting for an answer,silly me and thank you for helping i do like how it opens up the open page and how would i make it read two txt files ? i really do like the code ratings for both of you!
EDIT: and how to do i name the OPEN so i can specify which txt to choose?
Last edited by M0NKEYRASH; Jul 28th, 2011 at 05:57 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|