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

error while removing the DocumentElement from section ?

1 Answer 122 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Sopan
Top achievements
Rank 1
Sopan asked on 04 Sep 2013, 08:46 AM

Hi,

I am trying to remove some Document Element as Paragraph, Table, Images from section under RadRichTextBox .
But I want to implement the functionality which allow to add and remove the Document element on button click event.

Suppose I want to remove table from Section so Table having collection of Rows, Row having collection of Cells, Cell having collection of DocumentElement.

When I am get table from section and store it in local dictionary then table is come but not its inner collection like rows collection, cells collection (ie empty collection come), to solve this problem I have taken this values in my another collection.

But after got all the inner collection value I am trying to remove the DocumentElement from section it throws an exception and If I try to remove this DocuemntElement without getting his inner collection values then it removes normally.

reference code If I remove all comments then code thrown an error and with comment it run normally
Telerik.Windows.Documents.Model.Paragraph NextPara;
           try {
               Telerik.Windows.Documents.Model.Span spanbox = editor.Document.CaretPosition.GetCurrentSpanBox().AssociatedSpan;
               if(spanbox.FontWeight==FontWeights.Bold && spanbox.ForeColor==Colors.Aqua)
               {
                   Telerik.Windows.Documents.Model.Section currsect = editor.Document.CaretPosition.GetCurrentSectionBox().AssociatedSection;
                   Telerik.Windows.Documents.Model.Span currspan = editor.Document.CaretPosition.GetCurrentSpanBox().AssociatedSpan;
                   bool check = editor.Document.CaretPosition.MoveToNextInline();
                   bool c=editor.Document.CaretPosition.MoveToNextInline();
                   bool c1=editor.Document.CaretPosition.MoveToNextInline();
                   
                   var documentelement = editor.Document.CaretPosition.GetCurrentParagraphBox();
 
                   if (documentelement != null && documentelement.GetType() == typeof(Telerik.Windows.Documents.Layout.ParagraphLayoutBox))
                   {
                       Telerik.Windows.Documents.Model.Paragraph para = documentelement.AssociatedParagraph;
                       spanbox.Tag = Guid.NewGuid().ToString();
                        
                       string spantext = (para.Inlines.FirstOrDefault() as Telerik.Windows.Documents.Model.Span).Text;
 
                       if (!string.IsNullOrEmpty(spantext) && spantext == "[@[")
                       {
                            
                           NextPara = para.NextSibling as Telerik.Windows.Documents.Model.Paragraph;
                           //var DocEle = new DocumentElementCollection(para);
 
                           //foreach (var item in para.Children)
                           //{
                           //    DocEle.Add(item);
                           //}
                           currsect.Children.Remove(para);
 
                           //DocumentEleCollection.Add(DocEle);
 
                           while (NextPara !=null)
                           {                          
 
                               if (NextPara !=null && (NextPara.Inlines.FirstOrDefault() as Telerik.Windows.Documents.Model.Span).Text == "]@]")
                               {
                                   //var DocEle2 = new DocumentElementCollection(NextPara);
                                   //foreach (var item in NextPara.Children)
                                   //{
                                   //    DocEle2.Add(item);
                                   //}
 
                                  currsect.Children.Remove(NextPara);
                                  //DocumentEleCollection.Add(DocEle2);
                                   break;
                               }
 
                               //var DocEle1 = new DocumentElementCollection(NextPara);
                               //foreach (var item in NextPara.Children)
                               //{
                               //    DocEle1.Add(item);
                               //}
                               currsect.Children.Remove(NextPara);
                               //DocumentEleCollection.Add(DocEle1);
                               NextPara = NextPara.NextSibling as Telerik.Windows.Documents.Model.Paragraph;
                           }
                       }
                   }
 
               }
            
            
           }
           catch (Exception ex)
           { }



1 Answer, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 05 Sep 2013, 10:48 AM
Hi Sopan,

Please refer to the other forum thread you opened with a similar question for an answer on how to delete a table element.

In general, the proper approach when deleting content from a measured document is to select it and call the Delete() method of RadRichTextBox. Respectively, when inserting content you should move the caret to the desired location and call one of the Insert~() methods of the control.

I suggest you refer to the Positioning and Selection articles in the online documentation for further information on how to manipulate DocumentSelection.

I hope this helps!

Regards,
Petya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
RichTextBox
Asked by
Sopan
Top achievements
Rank 1
Answers by
Petya
Telerik team
Share this question
or