Click here to Skip to main content
15,894,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to reuse some code I had from another project that involved images. However, images and text differ on how they are accessed.

Here is my code so far:

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 New StreamReader

       For i As Integer = 0 To fileCount - 1


           Text(i) = Text.FromFile(Files.FileNames(i))

           'Gets all filenames selected as a string
           ' Dim Imagename As String = Path.GetFileNameWithoutExtension(Files.FileNames(i))
           Dim textpath As String = Path.GetFullPath(Files.FileNames(i))

           Me.RichTextBox1.LoadFile(textpath, RichTextBoxStreamType.PlainText)



the issue I am having is that it only read one file. also, the
Text(i) = Text.FromFile(Files.FileNames(i))
I do not know how to convert this into something usuable for text?

What I have tried:

I have tried to remove the
Text(i) = Text.FromFile(Files.FileNames(i))
entirely, but that didnt give the result I was looking for.
Posted
Updated 15-Feb-18 7:59am
Comments
Maciej Los 14-Feb-18 15:02pm    
How many textboxes are there?

How to read multiple text files in a richtextbox

Well, using RichTextBox.LoadFile Method (String, RichTextBoxStreamType) (System.Windows.Forms)[^] you can't! See MSDN documentation:
Quote:

Remarks
When loading a file with the LoadFile method, the contents of the file being loaded replace the entire contents of the RichTextBox control. This will cause the values of the Text and Rtf properties to change. You can use this method to load a previously created text or rich text format (RTF) document into the control for manipulation. If you want to save the file, you can use the SaveFile method.


Conclusion: you have to provide custom logic to add content of another file. You can use TextBoxBase.Lines Property (System.Windows.Forms)[^].

As to this piece of code: Text(i) = Text.FromFile(Files.FileNames(i)) i can say nothing... Well... it might be a collection of TextBox'es under common name.
 
Share this answer
 
Comments
Member 11856456 14-Feb-18 23:02pm    
I appreciate the feedback, still trying to get the Richtextbox to hold the text information from multiple text files.
At a guess, something like this should work:
VB.NET
Dim fileCount = Files.FileNames.Length
Dim text(fileCount - 1) As String

For i As Integer = 0 To fileCount - 1
    Dim textpath As String = System.IO.Path.GetFullPath(Files.FileNames(i))
    Text(i) = System.IO.File.ReadAllText(textpath)
Next

Me.RichTextBox1.Text = String.Join(Environment.NewLine, Text)
 
Share this answer
 
v2
Comments
Member 11856456 15-Feb-18 13:31pm    
Awesome, I appreciate it. This is exactly what I needed.

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