RichTextBox settings

4 posts, 0 answers
  1. Marcel
    Marcel avatar
    41 posts
    Member since:
    Jan 2014

    Posted 16 Jun 2015 Link to this post

    Hello,

    I have a couple of questions about the RichTextBox, I want to use this control for basic notes that are made in my application.

    The user should be able to use basic format options to format the note, like bold, understrike, italics and color.

    Also the text should be spellchecked, besides that I dont need any of the other features of this control.

     

    I am trying to set this up, but have some questions:

    • When I set the font in XAML the font of the text remains 12, the font of my contextmenu gets smaller but not the text in the richtextbox? How can I set this font?
    • The default settings show some margins below every line, I can remove this using the contextmenu option paragraph. How can I set this in XAML?
    • Is it possible to bind to some property that specify if there are spellcheck errors in the text or the number of spellcheck error in the text? So I can for example show a messagebox before the notes are saved which will tell the user that there are still spell check errors.
    • I want to use the Alt+Enter key-combination for next line, the enter key will be used to move to the next field, I tried to implement this see below, but cant programmaticaly add a return in the text. How can I accomplish this?

     

    public class RichTextbox : RadRichTextBox
    {
        protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
        {
             
            if ((Keyboard.Modifiers == ModifierKeys.Alt && Keyboard.IsKeyDown(Key.Enter)) ||
                (Keyboard.Modifiers == ModifierKeys.Alt && Keyboard.IsKeyDown(Key.Return)) ||
                (Keyboard.IsKeyDown(Key.RightAlt) && Keyboard.IsKeyDown(Key.Enter)) ||
                (Keyboard.IsKeyDown(Key.RightAlt) && Keyboard.IsKeyDown(Key.Return)))
            {
                //THIS CODE IS NOT WORKING
                XamlFormatProvider provider = new XamlFormatProvider();
                string text = provider.Export(this.Document);
                text += "\r\n";
                provider.Import(text);
                e.Handled = true;
            }
            else if (Keyboard.IsKeyDown(Key.Enter))
            {               
                this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                e.Handled = true;
            }
            else
            {
                base.OnPreviewKeyDown(e);
            }
        }
    }

     

    Regards,

     

    Marcel

  2. Tanya
    Admin
    Tanya avatar
    404 posts

    Posted 19 Jun 2015 Link to this post

    Hello Marcel,

     - To change the default font size of the control, you should set the DocumentInheritsDefaultStyleSettings to true:
    <telerik:RadRichTextBox  DocumentInheritsDefaultStyleSettings="True"
                   ...
    </telerik:RadRichTextBox>

    However, you should be aware that there is a known issue when using this approach - the font size of the context menu is changed as well. 

     - The default spacing before and after a Paragraph could be set trough the properties of RadDocument:
    <telerik:RadRichTextBox  DocumentInheritsDefaultStyleSettings="True" x:Name="radRichTextBox" ">
        <telerik:RadRichTextBox.Document>
            <telerik:RadDocument ParagraphDefaultSpacingAfter="0" />
         </telerik:RadRichTextBox.Document>
    </telerik:RadRichTextBox>

     - The spell checker does not expose a property, which you could use for binding, but you could try to call the Check() method od RadSpellChecker when the user tries to save:
    RadSpellChecker.Check(this.radRichTextBox, SpellCheckingMode.AllAtOnce);

    Calling this method will show a dialog, which will suggest the user to correct if there is any misspelled words.

    - The RadDocumentEditor class provides convenient methods, which could help you to insert a line break:
    RadDocumentEditor editor = new RadDocumentEditor(this.radRichTextBox.Document);
    editor.InsertLineBreak();

    Please, try these suggestions and let me know how they work for you. 

    Regards,
    Tanya
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Marcel
    Marcel avatar
    41 posts
    Member since:
    Jan 2014

    Posted 20 Jun 2015 in reply to Tanya Link to this post

    Hello Tanya,

     

    Thanks for your answers.

    The first two worked for me, for the third answer I rather check programmatically if there are spelling errors, but I guess there's no work around for this?

    For the last answer, when I hit enter in the RadRichTextBox, a new line is created and the cursor goes to the next line, only the cursor is not blinking anymore. So its not possible to start typing right away, I need to click in the RadRichTextBox to type on the new line? How can I fix this?

    Finally, I have also a problem with the tabindex of the RadRichTextBox, for some reason the value thats set is not working?

    I created this small example and that has the same problem, control will move from the textbox to the button and then to the RadRichTextBox? I am using version 2015.1.401.45.

     

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
         
        <TextBox Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center"
                   MaxHeight="25" Width="120" Margin="30" TabIndex="1"/>
        <telerik:RadRichTextBox Grid.Row="1" Width="200" Height="100" TabIndex="2"/>
        <Button Grid.Row="3" Width="140" HorizontalAlignment="Center" VerticalAlignment="Center"
                Content="Test button" TabIndex="3"/>
         
    </Grid>

     

    Marcel

     

  5. Boby
    Admin
    Boby avatar
    595 posts

    Posted 24 Jun 2015 Link to this post

    Hello Marcel,

    For the third answer: You can check if there are errors in the document using the DocumentProofingManager class:
    var proofingManager = new DocumentProofingManager(this.radRichTextBox.Document, this.radRichTextBox.SpellChecker, this.radRichTextBox.IgnoredWords);
    var documentStartPosition = new DocumentPosition(this.radRichTextBox.Document);
    bool hasErrors = proofingManager.GetNextError(documentStartPosition) != null;


    For the last answer: Actually you can re-map the InsertLineBreak command's shortcut directly in XAML:

    <telerik:RadRichTextBox
        xmlns:telerikDocs="clr-namespace:Telerik.Windows.Documents.RichTextBoxCommands;assembly=Telerik.Windows.Documents">
     
        <telerik:RadRichTextBox.InputBindings >
            <KeyBinding Gesture="Shift+Enter" Command="" />
            <KeyBinding Gesture="Alt+Enter" Command="telerikDocs:RichTextBoxCommands.InsertLineBreak"/>
             
        </telerik:RadRichTextBox.InputBindings>
     
    </telerik:RadRichTextBox>

    Using this code, RadRichTextBox doesn't lose focus after inserting the break. Note that Enter key will still insert new Paragraph, and the Paragrpaph.SpacingAfter and SpacingBefore will take effect in this case.

    For the tab index problem: this is known issue with RadRichTextBox. You can read more and track eventual progress in our feedback portal here:
    TabIndex property doesn't have effect

    I also want to elaborate more on Tanya's second answer - setting DocumentInheritsDefautlStyleSettings is the most easiest way to configure the default style of the text, however it have the mentioned unpleasant side effect on the context menu - as the context menu doesn't specify explicit font size, it will inherit the one set on its parent (RadRichTextBox.FontSize). Instead, we usually recommend modifying the default styles of the document, for example:
    this.radRichTextBox.Document.Style.SpanProperties.FontSize = Unit.PointToDip(20);

    You can read more about the default styles of RadRichTextBox here.


    Regards,
    Boby
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top