mutant said:Open the file with a StreamReader. Loop through the file using its ReadLine method which reads on line. Add 1 to the your counter. Continue the loop while the Peek method of the stream method does not return -1.
This assumes you have imports system.io before the beginning of the form classPureSc0pe said:How far off am I?
VB Code:
Dim prog As System.IO.File
Dim amount As IO.StreamReader
Dim file As String = "TextBox2.Text"
amount.ReadLine()
While amount.Peek <> -1
ListBox2.Items.Add(amount)
End While
Dim sr As New StreamReader("C:\Documents and Settings\Tower\My Documents\My Received Files\fso.txt")
Dim intCount As String
Dim strDummyvar As String
Do While sr.Peek <> -1
strDummyvar = sr.ReadLine
intCount += 1
listbox1.items.add(strdummyvar)
Loop
MessageBox.Show("There are " & intCount & "lines in the file")
this will add all the lines into a listbox and tell you how many lines are in the file
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sr As New StreamReader("C:\Documents and Settings\Tower\My Documents\My Received Files\test.txt")
Dim intCount As String
Dim strDummyvar As String
Do While sr.Peek <> -1
strDummyvar = sr.ReadLine
intCount += 1
ListBox1.Items.Add(strDummyvar)
Loop
MessageBox.Show("There are " & intCount & " lines in the file")
Dim i As Integer, strArray(ListBox1.Items.Count - 1) As String, objArray As Array
For i = 0 To ListBox1.Items.Count - 1
strArray(i) = ListBox1.Items(i)
Next
objArray.Sort(strArray)
Dim j As Integer
For j = 0 To strArray.Length - 1
ListBox2.Items.Add(strArray(j))
Next
End Sub
End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strFile As String = "file.txt"
lstBox.Items.Add(strFile & " has " & GetLineAmount(strFile) & " lines.")
End Sub
Public Function GetLineAmount(ByVal file As String) As Integer
Dim streamReader As New IO.StreamReader(file)
Dim text As String = streamReader.ReadToEnd
streamReader.Close()
Dim lines() As String = Split(text, Environment.NewLine)
Return lines.GetUpperBound(0) - 1
End Function
Take a look I modified your code. You had to take the part where its adding items to the listbox out of the loop. If you dont want them all displayed.PureSc0pe said:OK, It almost works the way I want it to...except it displays the message with every line.
VB Code:
ListBox2.Items.Clear()
Dim sr As New StreamReader("C:\list.txt")
Dim intCount As String
Dim strDummyvar As String
Do While sr.Peek <> -1
strDummyvar = sr.ReadLine
intCount += 1
Loop
ListBox2.Items.Add("There are " & intCount & " lines in the file")
End Sub
If there are 12 Lines, the Listbox looks like this:
There are 1 lines in the file
There are 2 lines in the file
There are 3 lines in the file
There are 4 lines in the file
There are 5 lines in the file
There are 6 lines in the file
There are 7 lines in the file
There are 8 lines in the file
There are 9 lines in the file
There are 10 lines in the file
There are 11 lines in the file
There are 12 lines in the file
I just want one line with the accurate number like this...
There are 12 lines in the file
Also, why is it not working when I put TextBox2.Text into the Filename. It should just read the text from that textbox right?
AFterlife said:Take a look I modified your code. You had to take the part where its adding items to the listbox out of the loop. If you dont want them all displayed.