[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
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
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
Re: Help! Vb.net Reading a text file line by line
Quote:
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.
Re: Help! Vb.net Reading a text file line by line
Quote:
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...
Re: Help! Vb.net Reading a text file line by line
Quote:
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?
Re: Help! Vb.net Reading a text file line by line
What do you mean 'crashed'? At which line?
Re: Help! Vb.net Reading a text file line by line
Quote:
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
Re: Help! Vb.net Reading a text file line by line
Quote:
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?