在RichTextBox的特定内容内容、RichTextBox

2023-09-06 09:10:27 作者:薄荷撞可乐

我要显示一些大胆的形式一些简单的内容,所以我使用richtextbox.And我已经做了一个文件扩展名.rtf.Now我加载与使用的LoadFile的RichTextBox的该文件()函数。这个工作。但我想显示在RichTextBox的文件的特定内容,比如可能是第一个五线,也可能是线没。六ten.Then是有什么解决办法?

I want to display some bold and some simple content in the form so I am using richtextbox.And I have made one file with extension of .rtf.Now I load that file in the richtextbox with use of the Loadfile() function.This works. But I want to display particular content of the file in the richtextbox,like may be first five lines or it may be line no. of six to ten.Then is there any solution ??

推荐答案

这是可能的,只是不是很干净。这code使用其他RTB加载该文件,剪贴板,以获得格式化的RTF。要注意的是它破坏了剪贴板中的内容。

It is possible, just not very cleanly. This code uses another RTB to load the file and the clipboard to get the formatted RTF. Beware that it destroys the clipboard content.

  using (var helper = new RichTextBox()) {
    helper.LoadFile(@"c:\temp\test.rtf");
    // Copy line #6
    int startRange = helper.GetFirstCharIndexFromLine(5);
    int endRange = helper.GetFirstCharIndexFromLine(6);
    helper.SelectionStart = startRange;
    helper.SelectionLength = endRange - startRange;
    helper.Copy();
  }
  richTextBox1.SelectAll();
  richTextBox1.Paste();