how can i parse files from notepad and transfer it to textbox with delimiter of newline?

Recommended Answers

All 2 Replies

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.

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
Be a part of the DaniWeb community

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