This topic has not yet been rated - Rate this topic

RichTextBox.ContentStart Property

Silverlight

Gets a TextPointer that indicates the start of content in the RichTextBox.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
public TextPointer ContentStart { get; }

Property Value

Type: System.Windows.Documents.TextPointer
A TextPointer that indicates the start of content in the RichTextBox.

In a completely empty RichTextBox (one without blocks), ContentEnd is the same as ContentStart, and both are insertion positions. This is the only instance when ContentStart or ContentEnd is an insertion position.

The following method uses the ContentStart and ContentEnd methods to determine if a RichTextBox is empty.


//This method returns true if the RichTextBox is empty.
public bool isRichTextBoxEmpty()
{
    TextPointer startPointer = MyRTB1.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward);
    TextPointer endPointer = MyRTB1.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
    if (startPointer.CompareTo(endPointer) == 0)
        return true;
    else
        return false;
}


Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Better method
The example method is prone to an error, for those who don't know who are new to RichTextBox and Blocks.

Also, the ending is redundant. I propose this instead of the one shown in the article:

//This method returns true if the RichTextBox is empty.public bool isRichTextBoxEmpty()
{
    //If no blocks are present, the richtextbox is empty.
    if(CodeShareBox.Blocks.Count == 0) {
        return true;
    }
 
    //After that, there must be a block, so we can begin.
    TextPointer startPointer = CodeShareBox.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward);
    TextPointer endPointer = CodeShareBox.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
 
    //Return true if empty, false if not.
    return (startPointer.CompareTo(endPointer) == 0);
}