The following code snippets demonstrate the code used for formatting functions in RichTextBox .NET and .NET Framework versions.
You can also use Ctrl+a shortcut to select the entire text, Ctrl+b shortcut to bold the text, Ctrl+i shortcut to italicize the text, and Ctrl+u shortcut to underline the text.
Foreground Color
C# |
Copy Code
|
---|---|
rtb.Selection.Foreground = newSolidColorBrush(Colors.Red); Highlight (background) color rtb.Selection.InlineBackground = newSolidColorBrush(Colors.Yellow); Toggle Bold if(rtb.Selection.FontWeight != null&& rtb.Selection.FontWeight.Value.Weight == FontWeights.Bold.Weight) { rtb.Selection.FontWeight = FontWeights.Normal; } else { rtb.Selection.FontWeight = FontWeights.Bold; } |
Toggle Italic
C# |
Copy Code
|
---|---|
if(rtb.Selection.FontStyle != null&& rtb.Selection.FontStyle == FontStyle.Italic) { rtb.Selection.FontStyle = FontStyle.Normal; } else { rtb.Selection.FontStyle = FontStyle.Italic; } |
Toggle Underline
C# |
Copy Code
|
---|---|
var range = rtb.Selection; var collection = new C1TextDecorationCollection(); if (range.TextDecorations == null) { collection.Add(C1TextDecorations.Underline[0]); } else if (!range.TextDecorations.Contains(C1TextDecorations.Underline[0])) { foreach (var decoration in range.TextDecorations) collection.Add(decoration); collection.Add(C1TextDecorations.Underline[0]); } else { foreach (var decoration in range.TextDecorations) collection.Add(decoration); collection.Remove(C1TextDecorations.Underline[0]); if (collection.Count == 0) collection = null; } range.TextDecorations = collection; |
Clear Formatting
C# |
Copy Code
|
---|---|
rtb.Selection.InlineBackground = null; rtb.Selection.Foreground = rtb.Foreground; rtb.Selection.FontWeight = FontWeights.Normal; rtb.Selection.FontStyle = FontStyle.Normal; rtb.Selection.TextDecorations = null; |
You can also use Ctrl+a shortcut to select the entire text, Ctrl+b shortcut to bold the text, Ctrl+i shortcut to italicize the text, and Ctrl+u shortcut to underline the text.
The following code implements Selection, Bold, Italics, and Underline formatting functions.
C# |
Copy Code
|
---|---|
private void RichTextBoxSelectionChanged(object sender, EventArgs e) { var sel = _rtb.Selection; FontWeight? fw = _rtb.Selection.FontWeight; _btnBold.IsChecked = fw.HasValue && fw.Value == FontWeights.Bold; FontStyle? fs = _rtb.Selection.FontStyle; _btnItalic.IsChecked = fs.HasValue && fs.Value == FontStyles.Italic; _btnUnderline.IsChecked = (sel.TextDecorations != null); } private void UnderlineClick(object sender, RoutedEventArgs e) { if (_btnUnderline.IsChecked.HasValue) { _rtb.Selection.TextDecorations = _btnUnderline.IsChecked.Value ? C1.WPF.RichTextBox.Documents.C1TextDecorations.Underline : null; } } private void ItalicClick(object sender, RoutedEventArgs e) { FontStyle? fs = _rtb.Selection.FontStyle; _rtb.Selection.FontStyle = fs.HasValue && fs.Value == FontStyles.Italic ? FontStyles.Normal : FontStyles.Italic; } private void BoldClick(object sender, RoutedEventArgs e) { FontWeight? fw = _rtb.Selection.FontWeight; _rtb.Selection.FontWeight = fw.HasValue && fw.Value == FontWeights.Bold ? FontWeights.Normal : FontWeights.Bold; |