New line vs New paragraph

6 posts, 1 answers
  1. Kenneth
    Kenneth avatar
    104 posts
    Member since:
    Apr 2014

    Posted 31 Aug 2014 Link to this post

    This has always frustrated me in ms word, spacing between paragraphs is larger than a line, double line I suppose, and the concept of a line has gone away replaced with line wrapping.

    So I want to insert three lines (not paragraphs) into my Q2 2014 Richtextbox.  When I do editor.InsertLine("") I get a paragraph.  I can set the ParagraphDefaultSpacingAfter to 0 and get my desired effect, but I would rather just insert a line.

    How would I do that?
  2. Answer
    Petya
    Admin
    Petya avatar
    983 posts

    Posted 03 Sep 2014 Link to this post

    Hi Kenneth,

    You can insert a line break with the Alt+Enter keyboard shortcut. Programmatically this is achieved with the InsertLineBreak() method of RadRichTextBox or RadDocumentEditor.
    this.radRichTextBox.InsertLineBreak();

    Note that lines separated in this manner are still interpreted as part of the same Paragraph document element. What you should be aware of is that extremely large document elements can cause performance diminishment when scrolling and typing in the control, so I would not recommend replacing all paragraph ends with a line break. Instead, you can simply change the default spacing after of the Normal style:
    this.radRichTextBox.Document.StyleRepository.GetValueOrNull(RadDocumentDefaultStyles.NormalStyleName).ParagraphProperties.SpacingAfter = 0;

    I hope this is useful.

    Regards,
    Petya
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. Kenneth
    Kenneth avatar
    104 posts
    Member since:
    Apr 2014

    Posted 03 Sep 2014 in reply to Petya Link to this post

    InsertLineBreak() works.  Please note that the Alt+Enter keyboard shortcut not work for me. (which does not matter).
  4. Dean
    Dean avatar
    64 posts
    Member since:
    Aug 2011

    Posted 28 Sep in reply to Petya Link to this post

    I've been through a number of posts here on the forum concerning line and paragraph spacing and tried everything suggested by Telerik admins and still cannot find a way to control paragraph spacing as it always defaults to double line.

    My RadRichTextBox is setup for data binding using the Telerik data provider as such:

    xmlns:t="http://schemas.telerik.com/2008/xaml/presentation"

    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    <RowDefinition Height="0"/>
    </Grid.RowDefinitions>
    <t:RadRichTextBox x:Name="RtbTextEditor" Grid.Row="0"/>
    <t:TxtDataProvider x:Name="TxtDataProvider" Grid.Row="1" Text="{Binding RtbContent, Mode=TwoWay}" 
    UpdateSourceTrigger="LostFocus" RichTextBox="{Binding ElementName=RtbTextEditor}" />
    </Grid>

    I first tried to set Paragraph Default Spacing in XAML:

    <t:RadRichTextBox x:Name="RtbTextEditor" Grid.Row="0">
    <t:RadRichTextBox.Document>
    <t:RadDocument ParagraphDefaultSpacingAfter="0" ParagraphDefaultSpacingBefore="0"/>
    </t:RadRichTextBox.Document>
    </t:RadRichTextBox>

    That did not work so I tried to set it in code after the SetupDocument event is fired as such:

    private void TxtDataProvider_OnSetupDocument(object sender, SetupDocumentEventArgs e)
    {
        RtbTextEditor.Document.ParagraphDefaultSpacingAfter = 0;
        RtbTextEditor.Document.ParagraphDefaultSpacingBefore = 0;
    }

    Finally I tried using the StyleRepository property of the Document:

    private void TxtDataProvider_OnSetupDocument(object sender, SetupDocumentEventArgs e)
    {    RtbTextEditor.Document.StyleRepository.GetValueOrNull(RadDocumentDefaultStyles.NormalStyleName).ParagraphProperties.SpacingAfter = 0;
    }

    I verified that this code executes after SetupDocument event fires and the properties are changed but none of that worked and when I press Enter it always defaults to double line spacing and the only way to get the single line spacing is to press Shift-Enter. 

    Any ideas on how to get the paragraph default spacing to change to single line?

  5. Dean
    Dean avatar
    64 posts
    Member since:
    Aug 2011

    Posted 28 Sep Link to this post

    Finally, I moved the ParagraphDefaultSpacingAfter setter to DocumentChanged event and that worked...

    private void RtbTextEditor_OnDocumentChanged(object sender, EventArgs e)
    {

    RtbTextEditor.Document.ParagraphDefaultSpacingAfter = 0;

    }

  6. Tanya
    Admin
    Tanya avatar
    583 posts

    Posted 02 Oct Link to this post

    Hello Dean,

    There are several approaches which you can use to achieve the desired behavior. The ones you are using are correct but need some slight modifications so they can work as expected.

    Setting in XAML
    You can set the default spacing before and after a paragraph. In the snippet from your first post, the property is applied only for the first instance of RadDocument, which is changed when opening another document. To ensure the settings you are applying are inherited when opening a new document, you should set the DocumentInheritsDefaultStyleSettings property to true.

    Through SetupDocument event
    When this event is raised, the imported document is still not set to the RadRichTextBox instance. Thus, you should set the desired properties to the Document property exposed by the event args:
    private void TxtDataProvider_SetupDocument(object sender, Telerik.Windows.Documents.FormatProviders.SetupDocumentEventArgs e)
    {
        e.Document.ParagraphDefaultSpacingAfter = 0;
        e.Document.ParagraphDefaultSpacingBefore = 0;  
    }

    Hope this is helpful.

    Regards,
    Tanya
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top