Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set RichTextBox Font for the next text to be written?

I need to set the font family for the next text to be written in a RichTextBox. I tried setting that with...

<RichTextBox x:Name="RichTextEditor" MaxWidth="1000" SpellCheck.IsEnabled="True"
             FontFamily="{Binding ElementName=TextFontComboBox, Path=SelectedItem}"
             FontSize="{Binding ElementName=TextSizeComboBox, Path=SelectedValue}"
             Width="Auto" Height="Auto" HorizontalScrollBarVisibility="Auto" 
VerticalScrollBarVisibility="Auto" />

...but it changed the whole text. I suppose that with the Selection property I can restrict the change to be applied just to the selected area. But how for the next -not yet typed- text?

like image 731
Néstor Sánchez A. Avatar asked Dec 06 '09 08:12

Néstor Sánchez A.


People also ask

How Do I Get Rich Text textbox?

To get the rich text formatting (RTF) codes, use the Rtf property. The amount of text that can be entered in the RichTextBox control is limited only by available system memory.

How do I change font size in RichTextBox?

For Changing font size and making bold of seleted text you can try like, Font currentFont = richTextBox1. SelectionFont; FontStyle newFontStyle = (FontStyle)(currentFont. Style | FontStyle.


1 Answers

There's a much easier way to do this: Implement a toolbar for your RichTextBox.

Unlike WinForms, the RichTextBox in WPF doesn't come with a toolbar by default, but it's really easy to create one yourself. The RichTextBox automatically handles many EditingCommands, so it's just a matter of creating a toolbar and some buttons. Microsoft has provided sample code for this at the bottom of the RichTextBox Overview on MSDN.

Unfortunately, those editing commands don't include setting the FontFace property of the selection, though you can create a ComboBox on the toolbar that can trigger the change with an event handler in the codebehind file.

That's the approach taken in this CodePlex article by Gregor Pross: WPF RichTextEditor

The project is commented in German, but the source itself is very clearly written. The codebehind used for his font selector ComboBox looks like this:

    private void Fonttype_DropDownClosed(object sender, EventArgs e)
    {            
        string fontName = (string)Fonttype.SelectedItem;

        if (fontName != null)
        {                
            RichTextControl.Selection.ApplyPropertyValue(System.Windows.Controls.RichTextBox.FontFamilyProperty, fontName);
            RichTextControl.Focus();
        }
    }

The main reason that people struggle with the FontFace selection is that after the font selection has been made, you must return focus to the RichTextBox. If the user must manually press tab or click into the RichTextBox, a new text selection gets created and you lose the formatting options you've chosen.

One of the answers to this StackOverflow question discusses that problem. WPF Richtextbox FontFace/FontSize

like image 102
dthrasher Avatar answered Nov 17 '22 01:11

dthrasher