Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Thanks to Richard I was able to go a little further in the process of manipulating the data I have. However, I need to remove blank lines from each file and then separate them individually by a blank line per file being read.

this is what I have so far:

VB
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

       Dim Files As New OpenFileDialog
       Files.Filter = "text(*.TXT files|*.txt"
       ' Allow the user to select multiple images.
       Files.Multiselect = True
       Files.Title = "Select an image"
       Files.ShowDialog()

       Dim fileCount = Files.FileNames.Length
       Dim text(fileCount - 1) As String

       For i As Integer = 0 To fileCount - 1

           Dim textpath As String = Path.GetFullPath(Files.FileNames(i))
           text(i) = System.IO.File.ReadAllText(textpath)

       Next


       For Each textpath As String In Files.FileNames

           RichTextBox1.Text += Environment.NewLine + String.Join(Environment.NewLine, System.IO.File.ReadAllText(textpath).Split(New Char() {ControlChars.Lf}, StringSplitOptions.RemoveEmptyEntries))

       Next


This code, in particular, removes the lines after completion on the all file read and then removes the lines. However, this is not my anticipated result as I want to remove per file and then add a blank line to separate each file, then display it into the richtextbox.

What I have tried:

I have tried use
VB
RichTextBox1.Lines = Me.RichTextBox1.Text.Split(New Char() {ControlChars.Lf}, StringSplitOptions.RemoveEmptyEntries)
in the for each statement but it still produced a singular output with no lines.
Posted
Updated 16-Feb-18 18:12pm
v4

1 solution

Well, I definitely wouldn't do it that way, but as long as you are, how about something like this (please excuse the C#ishness, this is only psuedo code):

C#
foreach ( string textpath in Files.FileNames )
    RichTextBox1.Text += Environment.NewLine + Environment.NewLine + String.Join (Environment.NewLine, System.IO.File.ReadAllText(textpath).Split(New Char() {ControlChars.Lf}, StringSplitOptions.RemoveEmptyEntries)) ;


Or this, with a StringBuilder:

C#
StringBuilder sb = new StringBuilder() ;

foreach ( string textpath in Files.FileNames )
  foreach ( string line in System.IO.File.ReadAllLines ( textpath ) )
    if ( line.Length > 0 )
      sb.AppendFormat ( "{0}\r\n\r\n" , line ) ;

sb.Length -= 4 ;
RichTextBox1.Text = sb.ToString() ;
 
Share this answer
 
v4
Comments
Member 11856456 16-Feb-18 23:47pm    
When I import multiple text files it only adds one blank line, which is at the top of the richtextbox. I was looking for something that would add a blank line as a separator per text file being imported. If there is a better method to do so I would like to know it. I have not used this portion or vb.net before. So, dealing with lines and text files are a new realm.
PIEBALDconsult 17-Feb-18 0:04am    
Update the question with your current code.
Member 11856456 17-Feb-18 0:13am    
Just updated it for you.
PIEBALDconsult 17-Feb-18 0:20am    
OK, and you're still not getting empty lines between files?
Maybe add another Environment.NewLine + ?
Member 11856456 17-Feb-18 0:33am    
That didnt work, it kept giving me red lines where the split function or the string.join are.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900