Bug with multiple RadRichTextBox in RadTabControl

7 posts, 1 answers
  1. Simon
    Simon avatar
    166 posts
    Member since:
    Sep 2010

    Posted 06 Jan 2011 Link to this post

    Hi,

    In some situation, when I call the "RichTextBox.Insert", I get the following error: "Object reference not set to an instance of an object"

    I did investigate the problem and it will occur for every RichTextBox in a RadTabControl, execpt the first one.

    Example #1:

    <Grid>
        <telerik:RadTabControl Name="RadTabControl1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
          <telerik:RadTabItem Header="Recherche" Height="23" HorizontalAlignment="Left" Name="RadTabItem1" VerticalAlignment="Top" Width="80">
              <telerik:RadRichTextBox Margin="0" Name="RadRichTextBox1"/>
          </telerik:RadTabItem>
          <telerik:RadTabItem Header="Rapport" Height="23" HorizontalAlignment="Left" Name="RadTabItem2" VerticalAlignment="Top" Width="80">
              <telerik:RadRichTextBox Margin="0" x:Name="RadRichTextBox2"/>
          </telerik:RadTabItem>
          <telerik:RadTabItem Header="Analyse" Height="23" HorizontalAlignment="Left" Name="RadTabItem3" VerticalAlignment="Top" Width="80">
              <telerik:RadRichTextBox Margin="0" x:Name="RadRichTextBox3"/>
          </telerik:RadTabItem>
        </telerik:RadTabControl>
    </Grid>

    In example #1, RadRichTextBox2 and RadRichTextBox3 will raise an error if they call the Insert function.  Howerver RadRichTextBox1 won't raise any error.


    Example #2
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="33*" />
            <ColumnDefinition Width="33*" />
            <ColumnDefinition Width="33*" />
        </Grid.ColumnDefinitions>
        <telerik:RadRichTextBox Grid.Column="0" x:Name="RadRichTextBox1" />
        <telerik:RadRichTextBox Grid.Column="1" x:Name="RadRichTextBox2" />
        <telerik:RadRichTextBox Grid.Column="2" x:Name="RadRichTextBox3" />
    </Grid>

    In exemple #2, no error will be raised.

    Is there a bug or amI doing something wrong here?

    Regards,

    Simon
  2. Simon
    Simon avatar
    166 posts
    Member since:
    Sep 2010

    Posted 08 Jan 2011 Link to this post

    Welll I don't know what was the problem but I solved the problem by setting the text like this:

     

     

     

    radRichTextBox.Document = New Telerik.Windows.Documents.FormatProviders.Txt.TxtFormatProvider().Import("Some text..."

     

     

  3. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 10 Jan 2011 Link to this post

    Hello Simon,

    The Document of RadRichTextBox needs to be measured and arranged before text can be inserted in it. These actions are automatically performed when the rich text box is shown (measured and arranged by the framework as is the case with RadRichTextBox1), but for the other two, you would need to manually invoke the following method:

    private void MeasureAndArrangeDocument(RadRichTextBox radRichTextBox)
    {
        RadDocument doc = radRichTextBox.Document;
        doc.Measure(RadDocument.MAX_DOCUMENT_SIZE);
        doc.Arrange(new RectangleF(PointF.Empty, doc.DesiredSize));
    }
    like this:
    MeasureAndArrangeDocument(this.RadRichTextBox1);
    MeasureAndArrangeDocument(this.RadRichTextBox2);
    MeasureAndArrangeDocument(this.RadRichTextBox3);
       ***
    //Now you can insert text
    this.RadRichTextBox3.Insert("SDFdsfsdfsdfsdF");
    This only needs to be done the first time the application is started, for example in the handler for the Loaded event of the RadTabControl.

    The approach you have adopted works in case you want to set the text only once from code-behind. TxtFormatProvider creates a new RadDocument on Import, which is also not measured and arranged and if you try to manipulate it before it is shown, you will get an exception again. 

    If you have any other questions, do not hesitate to contact us again.

    Greetings,
    Iva
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  4. Simon
    Simon avatar
    166 posts
    Member since:
    Sep 2010

    Posted 10 Jan 2011 Link to this post

    Hello Iva,

    Thanks for the solution and the explainations.

    I did implement you code and the code doesn't crash anymore. 

    However, if I use your solution instead of the TxtFormatProvider, there's 2 small bug with display.

    1- If I change the font size of the text, the change are not displayed.
    2- When a RadExpander expands and change the size of the RadRichTextBox, some word are overlapping each others.

    Both bugs are corrected when the RadRichTextBox is resized manually.

    I remember someone who mentionned an overlapping problem with the RadRichTextBox on this forum.  Are these problems related?

    Regards,

    Simon
  5. Answer
    Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 12 Jan 2011 Link to this post

    Hello Simon,

    I am glad to hear that your previous issues were successfully overcome. 
    As for the bugs with the display you have encountered, I was not able to reproduce them.

    1. Changing the FontSize of the text:
    Are you changing the FontSize programmatically? If so, the following line should be working, i.e. Changing the font size of the selected text:
    1. this.radRichTextBox.ChangeFontSize(Unit.PointToDip(50));
      Note that this method sets the font size in Dips, whereas the SelectionMiniToolBar and RadRibbonComboBox show the value in Points.
      If you want to set the FontSize of the text you are about to insert, you have to invoke the following methods in that order:
      this.radRichTextBox.Document.Selection.Clear(); //if you skip that, any selected text will be deleted
      this.radRichTextBox.ChangeFontFamily(new FontFamily("Arial Black"));
      this.radRichTextBox.ChangeFontSize(Unit.PointToDip(50));
      this.radRichTextBox.Insert("some Arial Black enormous text");
    2. As for the issue with the RadExpander, can you give us some details on your implementation, as it works fine on our side?
    Looking forward to your reply.

    Best wishes,
    Iva
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  6. Simon
    Simon avatar
    166 posts
    Member since:
    Sep 2010

    Posted 27 Jan 2011 Link to this post

    Thanks you Iva,

    I changed my code and now everything is ok.

    Regards,

    Simon
  7. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 28 Jan 2011 Link to this post

    Hello Simon,

    We are glad that the issue was solved.
    If you meet any other problems, do not hesitate to get back to us.

    Greetings,
    Iva
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Back to Top