I have a program that searches for a word in a .txt file. When it finds the word I want to print the following 5 lines under that word. So for example:
I search for "juice", and my label grabs this out of the textfile:

Is there anyway to this in VB.NET?


Sure read through the file, for each line use the instr function to see if the word exists in that line.

I think you misunderstood. I know how to find the one line, but I need to somehow print the following 5 lines after the line that word is in. That's where my problem is.

Actually I explained it bad.
Let me try again:
I have a code that searches for a word, it finds that word and views a bit of info on the same line as that word. What I need is to also be able to view the next 5 lines as well.

But once you've found the target line, then put a second loop in your code to read in the next five.

Dim sr As New System.IO.StreamReader("MyFile")
        Dim i As Integer
        Dim Line As String
        While sr.EndOfStream = False
            Line = sr.ReadLine
            If InStr(Line, "Juice") <> 0 Then
                'do what ever with the line
                For i = 0 To 4
                    Line = sr.ReadLine
            End If
        End While
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.