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

Add text from code behind problem

3 Answers 540 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Animesh Dey
Top achievements
Rank 2
Animesh Dey asked on 20 Oct 2011, 09:04 AM
Hi Telerik,

I am working with richtextbox. I am facing problem when I add text from code behind.
1. The richtextbox showing no text.
2. When I try to select text "Object reference not set" occur some time. Other time, after selecting the text become visible.
3. An additional enter is showing at the top of the richtextbox.

<Window x:Class="Imagine.TelerickTest.RichTextBox.WithoutMVVM"
        xmlns:my="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Documents"
        xmlns:radDoc ="clr-namespace:Telerik.Windows.Documents.Model;assembly=Telerik.Windows.Documents"
        Title="WithoutMVVM" Height="500" Width="500">
    <DockPanel>
        <StackPanel DockPanel.Dock="Top">
            <TextBlock Text="Input"></TextBlock>
            <TextBox x:Name="txtInput" ></TextBox>
        </StackPanel>
        <Button x:Name="btnClick" Content="Click" Click="btnClick_Click" DockPanel.Dock="Top"></Button>
        <my:RadRichTextBox x:Name="txtMessage" LayoutMode="Flow"></my:RadRichTextBox>
    </DockPanel>
</Window>

public WithoutMVVM()
        {
            InitializeComponent();
        }
        private void btnClick_Click(object sender, RoutedEventArgs e)
        {
            var span = new Telerik.Windows.Documents.Model.Span(txtInput.Text);
            var p = new Telerik.Windows.Documents.Model.Paragraph();
            p.Inlines.Add(span);
 
            var section = new Telerik.Windows.Documents.Model.Section();
            section.Blocks.Add(p);
 
            txtMessage.Document.Sections.Add(section);
        }

Regards

Animesh 

3 Answers, 1 is accepted

Sort by
0
Mihail
Telerik team
answered on 25 Oct 2011, 08:49 AM
Hello Animesh Dey,

There are two ways to add text to RadRichTextBox, as follows:
First way:

private void buttonInsertTextToRTB_Click(object sender, RoutedEventArgs e)
{
    // you can move the caret position where you want
    // and add the text on that position.
    this.editor.Document.CaretPosition.MoveToLastPositionInDocument();
    this.editor.InsertInline(new Span(this.textBoxInputText.Text));
}

Second way:
private void buttonInsertTextToRTB_Click(object sender, RoutedEventArgs e)
{
    Span span = new Span(this.textBoxInputText.Text);
    Paragraph paragraph = new Paragraph();
    paragraph.Inlines.Add(span);
 
    Section section = new Section();
    section.Blocks.Add(paragraph);
 
    this.editor.Document.Sections.Add(section);
    this.editor.UpdateEditorLayout();
}

Regards,
Mihail
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Animesh Dey
Top achievements
Rank 2
answered on 25 Oct 2011, 09:06 AM
Hi,

Thank you very much for your reply. Would you please tell me how can I do it using MVVM pattern.

Regards

Animesh

0
Mihail
Telerik team
answered on 27 Oct 2011, 07:40 AM
Hi Animesh Dey,

Iva has already answered your question in the other thread you opened.

Greetings,
Mihail
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
RichTextBox
Asked by
Animesh Dey
Top achievements
Rank 2
Answers by
Mihail
Telerik team
Animesh Dey
Top achievements
Rank 2
Share this question
or