Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted
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.
Posted
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.

 

 

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

It's not impossible, it's Inevitable.
Posted (edited)
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

This assumes you have imports system.io before the beginning of the form class

Here you go this should work:

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
   

Edited by AFterlife
Posted

While im at it.

Here is an example that shows how to sort an array. Just need 2 listboxes on your form and a command button. Unnamed.

            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
      

Posted (edited)

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

ListBox2.Items.Add("There are " & intCount & " lines in the file")

Loop

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?

Edited by PureSc0pe
It's not impossible, it's Inevitable.
Posted

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

 

May or may not be helpful.

 

File paths have to be absolute, otherwise they are relative to the executable.

 

By calling just "file.txt", it will assume "file.txt" is in the same directory as the .exe file that your program is running from. That is a relative filepath.

Posted
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?

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.

Posted
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.

 

 

That worked, Thanks! :)

It's not impossible, it's Inevitable.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...