This is a migrated thread and some comments may be shown as answers.

New line vs New paragraph

5 Answers 398 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Kenneth
Top achievements
Rank 2
Iron
Kenneth asked on 31 Aug 2014, 08:20 PM
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?

5 Answers, 1 is accepted

Sort by
0
Accepted
Petya
Telerik team
answered on 03 Sep 2014, 03:29 PM
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.
 
0
Kenneth
Top achievements
Rank 2
Iron
answered on 03 Sep 2014, 05:11 PM
InsertLineBreak() works.  Please note that the Alt+Enter keyboard shortcut not work for me. (which does not matter).
0
Dean
Top achievements
Rank 1
answered on 28 Sep 2017, 05:10 PM

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?

0
Dean
Top achievements
Rank 1
answered on 28 Sep 2017, 05:25 PM

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

private void RtbTextEditor_OnDocumentChanged(object sender, EventArgs e)
{

RtbTextEditor.Document.ParagraphDefaultSpacingAfter = 0;

}

0
Tanya
Telerik team
answered on 02 Oct 2017, 02:29 PM
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.
Tags
RichTextBox
Asked by
Kenneth
Top achievements
Rank 2
Iron
Answers by
Petya
Telerik team
Kenneth
Top achievements
Rank 2
Iron
Dean
Top achievements
Rank 1
Tanya
Telerik team
Share this question
or