Hello everyone,
In my application, I have formatting features such as bolding, underlining, italics, and strikethrough. In order to make these features accessible to the user, I have buttons that the user can click to turn them on or off. If the user highlights text and then clicks these buttons, everything works fine. However, imagine you've just clicked at the end of the text in a RichTextBox and then click the underline button. The underline button will light up, indicating that any text you type next will be underlined; however, it will not actually underline the text that you type next. If some text isalready underlined and the user starts typing afterwards, the proceeding text will in fact be underlined.
I am wondering how I can achieve this functionality. The behavior I'm looking for is as follows:
If the cursor in the RichTextBox is in between text that is NOT underlined, the user can click on the Underline button and start typing. The text that the user types WILL be underlined. Similarly, if the cursor in the RichTextBox is between text that IS underlined, the user can click on the Underline button and start typing. The text that the user types in this scenario WILL NOT be underlined because the user has turned off underlining.
Currently, this feature works fine with bold and italics. I'm not exactly sure why, but it seems that the RichTextBox has better support for them.
Here's my event handler for the underline button's Click event:
private void btnUnderline_Click(object sender, RoutedEventArgs e) { SetTextDecoration(btnUnderline, TextDecorations.Underline); }
Now here is my code for the SetTextDecoration method:
private void SetTextDecoration(Button FormatButton, TextDecorationCollection Decoration) { // Get the selected text TextRange TextRange = new TextRange(SelectedTextBox.Selection.Start, SelectedTextBox.Selection.End); // Create a new TextDecorationCollection based off of the current selected text's TextDecorationCollection TextDecorationCollection DecorationCollection = new TextDecorationCollection(clsPages.GetPropertyValue(TextRange, Inline.TextDecorationsProperty) as TextDecorationCollection); // Check if the collection does not contain the specified TextDecoration (Underline or Strikethrough) if (!DecorationCollection.Contains(Decoration[0])) { // It doesn't, so add it to the collection DecorationCollection.Add(Decoration); // Change the Button Background to indicate that the formatting was applied FormatButton.Background = FormattedButtonBrush; } else // It contains the specified TextDecoration { // Remove it from the collection DecorationCollection.Remove(Decoration[0]); // Change the Button Background to indicate that the formatting was not applied FormatButton.Background = DefaultButtonBrush; } // Apply the TextDecorationCollection to the selected text TextRange.ApplyPropertyValue(Inline.TextDecorationsProperty, DecorationCollection); // Focus on the Selected TextBox SelectedTextBox.Focus(); }
Thanks in advance for any help!