zoukankan      html  css  js  c++  java
  • WPF中richtextbox的一些用法

     

    1. 从文件中读出纯文本文件后放进RichTextBox或直接将文本放进RichTextBox中:
    private void LoadTextFile(RichTextBox richTextBox, string filename)
    {
        richTextBox.Document.Blocks.Clear();
        using (StreamReader streamReader = File.OpenText(filename)) {
               Paragraph paragraph = new Paragraph();
               paragraph.Text = streamReader.ReadToEnd();
               richTextBox.Document.Blocks.Add(paragraph);
        }
    }

    private void LoadText(RichTextBox richTextBox, string txtContent)
    {
        richTextBox.Document.Blocks.Clear();
        Paragraph paragraph = new Paragraph();
        paragraph.Text = txtContent;
        richTextBox.Document.Blocks.Add(paragraph);
    }
    2. 取得指定RichTextBox的内容:
    private string GetText(RichTextBox richTextBox)
    {
            TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
            return textRange.Text;
    }
    3. 将RTF (rich text format)放到RichTextBox中:
            private static void LoadRTF(string rtf, RichTextBox richTextBox)
            {
                if (string.IsNullOrEmpty(rtf)) {
                    throw new ArgumentNullException();
                }
                TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                using (MemoryStream rtfMemoryStream = new MemoryStream()) {
                    using (StreamWriter rtfStreamWriter = new StreamWriter(rtfMemoryStream)) {
                        rtfStreamWriter.Write(rtf);
                        rtfStreamWriter.Flush();
                        rtfMemoryStream.Seek(0, SeekOrigin.Begin);
                        //Load the MemoryStream into TextRange ranging from start to end of RichTextBox.
                        textRange.Load(rtfMemoryStream, DataFormats.Rtf);
                    }
                }
            }

    4. 将文件中的内容加载为RichTextBox的内容
            private static void LoadFile(string filename, RichTextBox richTextBox)
            {
                if (string.IsNullOrEmpty(filename)) {
                    throw new ArgumentNullException();
                }
                if (!File.Exists(filename)) {
                    throw new FileNotFoundException();
                }
                using (FileStream stream = File.OpenRead(filename)) {
                    TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                    string dataFormat = DataFormats.Text;
                    string ext = System.IO.Path.GetExtension(filename);
                    if (String.Compare(ext, ".xaml",true) == 0) {
                        dataFormat = DataFormats.Xaml;
                    }
                    else if (String.Compare(ext, ".rtf", true) == 0) {
                        dataFormat = DataFormats.Rtf;
                    }
                    documentTextRange.Load(stream, dataFormat);
                }       
            }

    5. 将RichTextBox的内容保存为文件:
            private static void SaveFile(string filename, RichTextBox richTextBox)
            {
                if (string.IsNullOrEmpty(filename)) {
                    throw new ArgumentNullException();
                }
                using (FileStream stream = File.OpenWrite(filename)) {
                    TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                    string dataFormat = DataFormats.Text;
                    string ext = System.IO.Path.GetExtension(filename);
                    if (String.Compare(ext, ".xaml", true) == 0) {
                        dataFormat = DataFormats.Xaml;
                    }
                    else if (String.Compare(ext, ".rtf", true) == 0) {
                        dataFormat = DataFormats.Rtf;
                    }
                    documentTextRange.Save(stream, dataFormat);
                }
            }

    转自:http://hi.baidu.com/%D6%A3%F0%A9%D2%FC/blog/item/d2f03a1048d7ee0d213f2e11.html
  • 相关阅读:
    NodeMCU快速上云集锦
    云数据库 MySQL 8.0 重磅发布,更适合企业使用场景的RDS数据库
    MySQL 8.0 技术详解
    为更强大而生的开源关系型数据库来了!阿里云RDS for MySQL 8.0 正式上线!
    阿里云CDN技术掌舵人文景:相爱相杀一路狂奔的这十年
    容器服务kubernetes federation v2实践五:多集群流量调度
    Helm V3 新版本发布
    Serverless助力AI计算:阿里云ACK Serverless/ECI发布GPU容器实例
    详解TableStore模糊查询——以订单场景为例
    洛谷P2727 01串 Stringsobits
  • 原文地址:https://www.cnblogs.com/liancs/p/3879299.html
Copyright © 2011-2022 走看看