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