zoukankan      html  css  js  c++  java
  • WPF 中RichTextBox控件用法细讲

    1. 取得已被选中的内容:
    (1)使用RichTextBox.Document.Selection属性
    (2)访问RichTextBox.Document.Blocks属性的“blocks”中的Text


    2.WPF RictTextBox内容清空方式:txtXml.Document.Blocks.Clear();


    3. 从文件中读出纯文本文件后放进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);
    }

    4. 取得指定RichTextBox的内容:

    private string GetText(RichTextBox richTextBox)
    {
        TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
        return textRange.Text;
    }

    5. 将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);
            }
        }
    }

    6. 将文件中的内容加载为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);
        }
    }

    7. 将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);
        }
    }

    读取与写入图片和文本操作::::

    读取RichTextBox的内容到string,将字符串保存到数据库的方法就不写了,大家都会

    string GetTextByRichBox(RichTextBox box)
    {
        MemoryStream s = new MemoryStream();
        TextRange documentTextRange = new TextRange(box.Document.ContentStart, box.Document.ContentEnd);
        documentTextRange.Save(s, DataFormats.XamlPackage);
        return Convert.ToBase64String(s.ToArray());
    }

    将string的内容转换成图片显示在RichTextBox中

    private ShowTextToRichBox(RichTextBox box)
    {
        MemoryStream s = new MemoryStream((Convert.FromBase64String(Convert.ToString(dr[“D_DESC”]))));
        TextRange TR = new TextRange(box.Document.ContentStart, box.Document.ContentEnd);
        TR.Load(s, DataFormats.XamlPackage);
    }
  • 相关阅读:
    Mysql 创建外键 1005 err 150
    骑车目标
    windows 如何查看端口占用进程ID 进程名称 强制结束进程
    Eclipse去除JavaScript验证错误
    MyEclipse 代码里的中文字太小设置方法
    security自动登陆
    Tomcat 7 可以修改 Session 默认的 Cookie 名 JSESSIONID 了
    windows2008 安装oracle10g“程序异常终止。发生内部错误。请将以下文件提供给oracle技术支持部门
    ORA-01652: 无法通过 128 (在表空间 TEMP 中) 扩展 temp 段(EXP-00056: 遇到 ORACLE 错误 1652 ORA-01652: unable to extend temp segment by 128 in tablespace TEMP)
    oracle数据库启动时出现ORA-01157和ORA-01110问题
  • 原文地址:https://www.cnblogs.com/wzwyc/p/6292138.html
Copyright © 2011-2022 走看看