3
Contributors
2
Replies
15
Views
4 Years
Discussion Span
Last Post by tinstaafl
0

I'm assuming you mean you want to separate the lines in a notepad (text) document into an array of lines. Do you need to get the text from an open instance of notepad or is it sufficient to read the text from a file?

If you are reading from a file you can just do

Dim lines() As String = System.IO.File.ReadAllLines(filename)

If you already have a string that you want to separate you can do

Dim lines() As String = Split(text.Replace(vbCr, ""), vbLf)

If you do this instead of just

Dim lines() As String = Split(text, vbCrLf)

then you guarantee that the lines will be parsed whether the lines end with vbLf or vbCrLf.

0

Or you could simply add the lines directly to the listbox:

ListBox1.Items.AddRange(IO.File.ReadAllLines("textfile1.txt")

Unless you're confused about the difference between a listbox and a multiline textbox, then add the file to a textbox like this:

TextBox1.Lines = IO.File.ReadAllLines("textfile1.txt")

If you need to be selective about which lines to add you can loop through the file with a while loop:

    Dim sr As New IO.StreamReader("textfile1.txt")
    While Not sr.EndOfStream
        Dim tempstr As String = sr.ReadLine
        If tempstr.Contains("abc") Then
            ListBox1.Items.Add(tempstr)
        End If
    End While

Edited by tinstaafl

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.