only display certain lines?

ben84

New member
Joined
Mar 26, 2010
Messages
3
Programming Experience
Beginner
Hello.

I have read a text file into a textbox.

The text looks like this (small snippet):

N08861 A1 P 0 A2 P 0 A3 P 0
N08862 P 0
N08870 P 0
N08900 P 00000001

What i want to do is to only display certain lines - this depends on the N number shown above.

E.G

I want the text box the display line N5000 upto N6000.

Can anyone help please.

Thanks
Ben
 
Read the file into a collection of the lines, then loop the collection and aggregate the needed ones into a string (I'd use a StringBuilder) then simply display the output.

If everything's in a collection, you can clear the results in the textbox on the form, and re loop the collection with different criteria without needing to re-read the file.
 
You need to loop the file with a StreamReader and hold each value(ReadLine) in an array. Then iterate over this list/array get just the first part of the value(N5001) and strip off the "N" and test if this number is > 5000 and also < 6000 - if it meets the criteria add this to the textbox. With such a big list I would use a StringBuilder for concatenating the string together.
 
No. It's probably to basic for a school project!!!

I'm trying to learn VB and i am simply thinking of projects to do.

This one is - load in text file > select certain lines > then read the data.
 
You'll be needing something like this:
VB.NET:
'At the top of the form:
Private m_TextFileLines As New List(Of String)

Private Sub ReadTextFile(ByVal FileName As String)
    Dim sr As IO.StreamReader
    Try
        sr = New IO.StreamReader(FileName)
        m_TextFileLines.Clear()
        m_TextFileLines.AddRange(sr.ReadToEnd.Split(CChar(Environment.NewLine)))
    Catch Ex As Exception
        MessageBox.Show(String.Format("Couldn't read the file '{0}'" & Environment.NewLine & "{1}", IO.Path.GetFileName(FileName), ex.Message),"Couldn't read file")
    Finally
        If sr IsNot Nothing Then sr.close()
    End Try
End Sub

Private Sub DisplayData()
    If m_TextFileLines.Count > 0I Then
        For Counter As Integer = 0I To m_TextFileLines.Count - 1I
            'Not enough info from OP to make suggestions here yet
        Next Counter
    End If
End Sub
 
Yeah I was more curious than anything else. Looks like JB got ya fixed up =).
 
Back
Top