如何读取richtextbox中的多个文本文件 [英] How to read multiple text files in a richtextbox

查看:62
本文介绍了如何读取richtextbox中的多个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试重用我从另一个涉及图像的项目中获得的代码。但是,图片和文字的访问方式不同。



这是我的代码到目前为止:



将文件调暗为新的OpenFileDialog 
Files.Filter =text(* .TXT文件| * .txt
'允许用户选择多个图像。
Files .Multiselect = True
Files.Title =选择图像
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 ))

'获取选择为字符串的所有文件名
'Dim Imagename As String = Path.GetFileNameWithoutExtension(Files.FileNames(i))
Dim textpath As String = Path。 GetFullPath(Files.FileNames(i))

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







我遇到的问题是它只读取一个文件。另外,

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

我不知道如何将其转换成可用于文本的东西?



我的尝试:



我试图删除

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

完全,但是没有给出我想要的结果。

解决方案

会员11856456 [ ^ ]写道:

如何在richtextbox中读取多个文本文件



好​​吧,使用RichTextBox.LoadFile方法(字符串,RichTextBoxStreamType)(System.Windows.Forms) [ ^ ]你不能!请参阅MSDN文档:

Quote:



备注

使用LoadFile方法加载文件时,正在加载的文件的内容替换RichTextBox控件的全部内容。这将导致Text和Rtf属性的值发生更改。您可以使用此方法将先前创建的文本或富文本格式(RTF)文档加载到控件中以进行操作。如果要保存文件,可以使用SaveFile方法。





结论:你必须提供自定义逻辑添加另一个文件的内容。您可以使用 TextBoxBase.Lines属性(系统.Windows.Forms) [ ^ ]。



至于这段代码: Text( i)= Text.FromFile(Files.FileNames(i))我什么都不说......嗯......它可能是一个普通名称下的TextBox'的集合。


猜测,这样的事情应该有效:

  Dim  fileCount = Files.FileNames.Length 
Dim text(fileCount - 1 作为 字符串

对于 i < span class =code-keyword>作为 整数 = 0 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,文本)


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.

解决方案

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.


At a guess, something like this should work:

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)


这篇关于如何读取richtextbox中的多个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆