|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RichTextBoxExtensions.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// Extension methods for <see cref="RichTextBox" />.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System.Windows.Controls;
using System.Windows.Documents;
using Catel.Windows.Documents;
namespace Catel.Windows.Controls
{
/// <summary>
/// Extension methods for <see cref="RichTextBox"/>.
/// </summary>
public static class RichTextBoxExtensions
{
/// <summary>
/// Sets the text of a <see cref="RichTextBox"/>. This method will replace
/// any existing content.
/// </summary>
/// <param name="rtb"><see cref="RichTextBox"/> that should contain the text.</param>
/// <param name="text">Text to set.</param>
public static void SetText(this RichTextBox rtb, string text)
{
var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
textRange.ApplyPropertyValue(Control.FontSizeProperty, rtb.Document.FontSize);
textRange.ApplyPropertyValue(Control.FontFamilyProperty, rtb.Document.FontFamily);
if (text.GetDataFormat() == System.Windows.DataFormats.Text)
{
text = text.ConvertPlainTextToRtf(rtb.FontSize, rtb.FontFamily);
}
textRange.SetText(text);
}
/// <summary>
/// Returns the text that a <see cref="RichTextBox"/> contains.
/// </summary>
/// <param name="rtb"><see cref="RichTextBox"/> that contains the text.</param>
/// <returns>String containing the text of the <see cref="RichTextBox"/> control.</returns>
public static string GetText(this RichTextBox rtb)
{
var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
return textRange.GetText();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.