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

Clearing items inside RadRichTextBox causes an error

5 Answers 180 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Joseph
Top achievements
Rank 1
Joseph asked on 04 Feb 2011, 11:17 AM
Here is the class :
public class GVRichTextBox : RadRichTextBox
    {
 private Telerik.Windows.Documents.Model.Paragraph _paragraph = new Telerik.Windows.Documents.Model.Paragraph();
        private Telerik.Windows.Documents.Model.Section _section = new Telerik.Windows.Documents.Model.Section();
  
  public GVRichTextBox()
        {        
            this._section.Children.Add(this._paragraph);
            this.Document.Sections.Add(this._section);
            this.IsSelectionMiniToolBarEnabled = false;
            this.IsSpellCheckingEnabled = false;
            this.ContextMenu = new ContextMenu();
            ((ContextMenu)this.ContextMenu).ContentBuilder = new CustomMenuBuilder(this);
        }
  
 public void AddSpan(Telerik.Windows.Documents.Model.Span span)
        {
            if (span != null)
            {
                this._paragraph.Inlines.Add(span);
            }        
        }
  
  
  
  
}

Clearing the inlines by using this code :

    this._paragraph.Inlines.Clear();

causes an error and the browser freezes.

Any suggestions ?

5 Answers, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 08 Feb 2011, 09:16 AM
Hello Joseph,

Thank you for reporting the issue you have stumbled upon. The Clear() method of the SpecializedDocumentElementCollection that is not working properly for the spans in the Inlines of the paragraph is a bug that we will try to fix in the next release.

A better approach would be  to select the text and delete it like this:

public void ResetParagraph()
{
    this.Document.Selection.SelectAll();
    this.Document.Delete(false);
    this._section = this.Document.Sections.First;
    this._paragraph = this._section.Blocks.First as Paragraph;
    this.Document.History.Clear();
}

You can decide if you want to keep the history or clear it, depending on what you wish to happen on Undo().
Note that modifying document-element structure directly through methods like Inlines.Add/Clear will always clear the editing history.
If you have any other questions, do not hesitate to contact us again.

Kind regards,
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>>
0
Rabeeh
Top achievements
Rank 2
answered on 21 Feb 2011, 04:16 PM
After you clear the RichTextBox using your suggested method and try to add a span to the control using :

 this.InsertInline(span);

I get a System.InvalidOperation Exception. Here is the Stack Trace :

at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)
at MS.Internal.XcpImports.MethodEx(DependencyObject obj, String name)
at System.Windows.UIElement.UpdateLayout()
at Telerik.Windows.Documents.UI.DocumentPresenterBase.Telerik.Windows.Controls.IDocumentEditorPresenter.UpdateLayout()
at Telerik.Windows.Controls.RadRichTextBox.UpdateEditorLayout(Boolean focusCarret, Boolean updateCaretSize)
at Telerik.Windows.Controls.RadRichTextBox.UpdateEditorLayout()
at Telerik.Windows.Controls.RadRichTextBox.document_PresentationChanged(Object sender, EventArgs e)
at System.EventHandler.Invoke(Object sender, EventArgs e)
at Telerik.Windows.Documents.Model.RadDocument.OnPresentationChanged()
at Telerik.Windows.Documents.Model.RadDocument.EndStructureChange(Boolean shouldUpdateLayout)
at Telerik.Windows.Documents.Model.RadDocument.InsertFragment(DocumentFragment fragment, DocumentPosition position)
at Telerik.Windows.Documents.Model.RadDocument.InsertFragment(DocumentFragment fragment)
at Telerik.Windows.Documents.Commands.InsertSubDocumentCommand.ExecuteInsertCore(InsertCommandContextBase myContext)
at Telerik.Windows.Documents.Commands.InsertCommandBase.ExecuteCore(Object context)
at Telerik.Windows.Documents.Commands.CommandBase.Execute(Object context)
at Telerik.Windows.Documents.Model.RadDocument.ExecuteCommand(CommandBase command, DocumentCommandContext context)
at Telerik.Windows.Controls.RadRichTextBox.InsertInline(Inline inline)
at GVRichTextBox.GVRichTextBox.AddSpan(Span span, Boolean postBack)
at GVRichTextBox.Implementation.GVRichTextBoxCtrl.AddText(String text, Boolean postBack)
at GVRichTextBox.Implementation.GVRichTextBoxCtrl.SegmentTextCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)
at System.Windows.Data.BindingExpression.SendDataToTarget()
at System.Windows.Data.BindingExpression.SourceAcquired()
at System.Windows.Data.BindingExpression.System.Windows.IDataContextChangedListener.OnDataContextChanged(Object sender, DataContextChangedEventArgs e)
at System.Windows.Data.BindingExpression.DataContextChanged(Object sender, DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.OnDataContextChanged(DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.OnAncestorDataContextChanged(DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.OnAncestorDataContextChanged(DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.OnAncestorDataContextChanged(DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyProperty dp)
at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
at System.Windows.FrameworkElement.set_DataContext(Object value)
at Telerik.Windows.Controls.GridView.DataCellsPresenter.PrepareContainerForItemOverride(DependencyObject element, Object item)
at Telerik.Windows.Controls.GridView.DataCellsPresenter.OnItemChanged(Object oldItem, Object newItem)
at Telerik.Windows.Controls.GridView.DataCellsPresenter.set_Item(Object value)
at Telerik.Windows.Controls.GridView.GridViewRowItem.OnItemChanged(Object oldItem, Object newItem)
at Telerik.Windows.Controls.GridView.GridViewRow.OnItemChanged(Object oldItem, Object newItem)
at Telerik.Windows.Controls.RadRowItem.OnItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at Telerik.Windows.PropertyMetadata.<>c__DisplayClass1.<Create>b__0(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
at Telerik.Windows.Controls.RadRowItem.set_Item(Object value)
at Telerik.Windows.Controls.GridView.GridViewRowItem.PrepareRow(Object item, GridViewDataControl owningDataGrid)
at Telerik.Windows.Controls.GridView.GridViewRow.PrepareRow(Object item, GridViewDataControl owningDataGrid)
at Telerik.Windows.Controls.GridView.GridViewDataControl.PrepareContainerForItemOverride(DependencyObject element, Object item)
at Telerik.Windows.Controls.GridView.BaseItemsControl.Telerik.Windows.Controls.GridView.IGeneratorHost.PrepareItemContainer(DependencyObject container, Object item)
at Telerik.Windows.Controls.GridView.GridViewItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject container)
at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled)
at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.InsertRecycledContainer(Int32 childIndex, UIElement container)
at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized)
at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.BringIndexIntoView(Int32 index)
at Telerik.Windows.Controls.GridView.GridViewDataControl.ScrollIntoViewRecursive(FrameworkElement element, Stack`1 itemStack, Action`1 scrollFinishedCallback)
at Telerik.Windows.Controls.GridView.GridViewDataControl.ScrollRowIntoViewInternal(Object dataItem, Action`1 scrollFinishedCallback)
at Telerik.Windows.Controls.GridView.GridViewDataControl.<>c__DisplayClass6d.<ScrollIntoViewAsync>b__6c()

The RichTextBoxes are inside a GridView. I am using :

this.gridView.ScrollIntoView(this.gridView.Items[int.Parse(this.textBox.Text)]);

When THe RichTextBox is in View, it works well. But if it is not it throw this error.

Any ideas ? 
0
Andrew
Telerik team
answered on 22 Feb 2011, 06:13 PM
Hi Rabeeh ,

I have attached a demo project where i test both scenarios you have introduced: standalone RadRichTextBox and RadRichTextBox in GridView. I couldn't get the exception you mentioned below. I also did not quite well understand what you mean by that the RichTextBox is not in the View. I don't think you can access it if it's not in the viewport (if that's what you mean). If i am wrong please send us a demo where you do this so we can assist you further. Please review the demo we sent you and get back to us if we didn't get your point.

All the best,

Andrew
the Telerik team

Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Rabeeh
Top achievements
Rank 2
answered on 23 Feb 2011, 10:55 AM
Hi Andrew, Thanks for your follow up. I appreciate it.

At Binding inside the GridView of the RichTextBox, Let's say you have created a Text property for the RadRichTextBox and you bind it to the appropriate field of the Model class. It you invoke the method InsertInline()  inside the  Set accessor of the Text property an error will raise : Object reference not set to an instance of an object.

StackTrace :
at Telerik.Windows.Documents.DocumentPosition.AnchorToCurrentBoxIndex()
at Telerik.Windows.Documents.Commands.InsertCommandBase.ExecuteCore(Object context)
at Telerik.Windows.Documents.Commands.CommandBase.Execute(Object context)
at Telerik.Windows.Documents.Model.RadDocument.ExecuteCommand(CommandBase command, DocumentCommandContext context)
at Telerik.Windows.Controls.RadRichTextBox.InsertInline(Inline inline)
at GVRichTextBox.GVRichTextBox.AddSpan(Span span, Boolean postBack)
at GVRichTextBox.Implementation.GVRichTextBoxCtrl.AddText(String text, Boolean postBack)
at GVRichTextBox.Implementation.GVRichTextBoxCtrl.ParseSegment(String text, Boolean isPostBack, Boolean reset)
at GVRichTextBox.Implementation.GVRichTextBoxCtrl.SegmentTextCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)
at System.Windows.Data.BindingExpression.SendDataToTarget()
at System.Windows.Data.BindingExpression.SourceAcquired()
at System.Windows.Data.BindingExpression.System.Windows.IDataContextChangedListener.OnDataContextChanged(Object sender, DataContextChangedEventArgs e)
at System.Windows.Data.BindingExpression.DataContextChanged(Object sender, DataContextChangedEventArgs e)
at System.Windows.DataContextChangedEventHandler.Invoke(Object sender, DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.OnDataContextChanged(DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.OnTreeParentUpdated(DependencyObject newParent, Boolean bIsNewParentAlive)
at System.Windows.DependencyObject.UpdateTreeParent(IManagedPeer oldParent, IManagedPeer newParent, Boolean bIsNewParentAlive, Boolean keepReferenceToParent)

InsertInline() method will work only when the RadRichTextBox is loaded.

I think the issue is routed to the fact that : this._paragraph.Inlines.Clear() bug.

So I need to know when the control is loading for the first time and when it is not to add Text to it. And there is not easy way to clear the inlines inside the RichTextBox !

Will this bug be resolved in the new release ? 

It is a show stopper for our project.

Thanks.




0
Iva Toteva
Telerik team
answered on 28 Feb 2011, 12:30 PM
Hi Rabeeh ,

This exception occurs when attempting to insert text (or a Span) in the document before the initial measuring of the document. That is when the first Section of the document is created, altogether with the first paragraph and the first span in that paragraph.
Putting the InsertInline() method on the queue for delayed execution should solve the problem with data binding:

set
{
    if (!String.IsNullOrEmpty(value))
    {
        Dispatcher.BeginInvoke(new Action(() =>
        {
            base.InsertInline(new Span(value));
        }));
    }
}

A more common solution would be to check if there are sections in the document and if not, create one. Something like this should do the trick:
if (!String.IsNullOrEmpty(value))
{
    Dispatcher.BeginInvoke(new Action(() =>
    {
        if (base.Document.Sections.Count == 0)
        {
            base.Document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
            base.Document.Arrange(new RectangleF(PointF.Empty, base.Document.DesiredSize));
        }
        base.InsertInline(new Span(value));
    }));
}

There are probably neater ways to achieve the desired behavior in your case, and if you give us some more details on your whole scenario - why you need to keep a reference to the first paragraph, span, etc., how have you declared the text property, we might be able to give you a more thorough suggestion. You can do that in the support ticket you opened, if you like.

Best wishes,
Iva
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
RichTextBox
Asked by
Joseph
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Rabeeh
Top achievements
Rank 2
Andrew
Telerik team
Share this question
or