Current filter:
                                You should refresh the page.
                                Support Center
                                0
                                  • Hi,
                                    I am working on a Silverlight application where we customized RichTextBox according to our needs. we have a feature where user can delete the sections available in the document by using a custom button from the menu bar. I don't see any inbuilt functionality to remove the section. Document.Sections property does not have remove method. Is there any way we can achieve remove section feature.

                                    Thanks
                                    Anil

                                You must  log in  or  register  to leave comments

                                1 Solution

                                1

                                Hello Anil,

                                If you need to remove a section with its content, you can do this by removing the section's paragraphs. Here is a sample code snippet illustrating this approach:

                                [C#]
                                DevExpress.XtraRichEdit.API.Native.Section section = richEditControl.Document.Sections[x]; richEditControl.Document.BeginUpdate(); for ( int i = section.Paragraphs.Count - 1; i >= 0; i-- ) { richEditControl.Document.Delete(richEditControl.Document.Paragraphs[i].Range); } richEditControl.Document.EndUpdate();

                                Updated by Ted (DevExpress Support), 01/06/2014:
                                The following code deletes all section breaks from a document:

                                [C#]
                                richEditControl1.Document.BeginUpdate(); for ( int i = richEditControl1.Document.Sections.Count - 1; i >= 0; i-- ) { Section section = richEditControl1.Document.Sections[i]; DocumentPosition position = section.Paragraphs[section.Paragraphs.Count - 1].Range.End; richEditControl1.Document.Delete(richEditControl1.Document.CreateRange(position.ToInt() - 1, 1)); } richEditControl1.Document.EndUpdate();
                                Show all comments
                                • Anil_Gupta29 12.29.2013

                                  Thanks a lot for your answer. In my case, I don't want to delete the contents of the section. I just want to delete the formatting like page layout, header, footer and all the other settings that are applicable to section. Is there any way to achieve this? I have found a way to remove the header, footer but could not find anything which can remove all the other settings (pagetype. margin etc....)

                                • Thank you for the clarification, Anil. It is possible to remove a section break and thus remove a section-specific formatting using the following code:

                                  [C#]
                                  Document document = richEditControl1.Document; SectionCollection sections = document.Sections; if(sections.Count > 1) { Section section = sections[x]; DocumentPosition position = section.Paragraphs[section.Paragraphs.Count - 1].Range.End; document.Delete(document.CreateRange(position.ToInt() - 1, 1)); }
                                • Anil_Gupta29 12.31.2013

                                  Thanks a lot for your response. The given code is working perfectly fine however it is not working for the last section. The delete line is actually not doing anything. the paragraph count is not changing even after delete for the last section. I have tested it for other sections and it is working for them. Can you please suggest me what is wrong with the last section?

                                • Anil_Gupta29 01.03.2014

                                  I didn't get any solution on my previous comment. Should I assume that there is no solution for it?

                                • Anil,

                                  A document always has a default section - Sections[0]. This section isn't represented by a section break line and it cannot be removed. Are you talking about this section?

                                • Anil_Gupta29 01.03.2014

                                  No I am not talking about the first section. I had three sections in the document and tried to delete the last section by using the suggested code. but it was not working. Can you please give ne some pointer? We assume that there should be at least one section and are not allowing the user to delete the first section.

                                • Anil_Gupta29 01.04.2014

                                  still waiting for the response... I am trying to delete the last section available our of 3 section by using the following code.
                                  Section section = sections[x];
                                       DocumentPosition position = section.Paragraphs[section.Paragraphs.Count - 1].Range.End;
                                       
                                       document.Delete(document.CreateRange(position.ToInt() - 1, 1));

                                • The code is correct. Apparently you're missing the first section while iterating through all of them. However, you need to process this section as well, since the last paragraph of this section contains a section break symbol. Here is a code snippet that removes all sections in a document:

                                  [C#]
                                  richEditControl1.Document.BeginUpdate(); for ( int i = richEditControl1.Document.Sections.Count - 1; i >= 0; i-- ) { Section section = richEditControl1.Document.Sections[i]; DocumentPosition position = section.Paragraphs[section.Paragraphs.Count - 1].Range.End; richEditControl1.Document.Delete(richEditControl1.Document.CreateRange(position.ToInt() - 1, 1)); } richEditControl1.Document.EndUpdate();
                                • Anil_Gupta29 01.06.2014

                                  I don't see any difference between the code except that you are trying to delete all the sections however I am trying to delete the section that user has selected. When I run the below code for last section, it is not deleting the section however it is working for all other section including section at 0th index.

                                • Anil_Gupta29 01.06.2014

                                  Here is the code that I am trying.
                                  this.MainDocument.BeginUpdate();
                                              Section section = this.MainDocument.Sections[this.MainDocument.Sections.Count - 1];
                                              DocumentPosition position = section.Paragraphs[section.Paragraphs.Count - 1].Range.End;
                                              this.MainDocument.Delete(this.MainDocument.CreateRange(position.ToInt() - 1, 1));
                                              this.MainDocument.EndUpdate();
                                              var a = this.MainDocument.Sections;

                                  at last I can still see the 3 sections in variable a.

                                • Anil_Gupta29 01.06.2014

                                  I even tried the exact same code as you suggested. This code does not do anything when it goes inside the for loop for very firsts time (for last section). the section count does not decrease however it works when i = 0 which is completely opposite what you said earlier. I am able to delete the first section but not last section. Is there a rule that last section is always the default section and user would not be able to delete the last section.

                                  this.MainDocument.BeginUpdate();

                                              for (int i = this.MainDocument.Sections.Count - 1; i >= 0; i--)
                                               {
                                                   Section section = this.MainDocument.Sections[i];
                                                   DocumentPosition position = section.Paragraphs[section.Paragraphs.Count - 1].Range.End;
                                                   this.MainDocument.Delete(this.MainDocument.CreateRange(position.ToInt() - 1, 1));
                                               }

                                              this.MainDocument.EndUpdate();

                                  Waiting for the response. Thanks a lot for your help..

                                • Hi,

                                  We need additional time to research this. We will answer you as soon as we can.

                                • Anil, I have attached a sample project illustrating the approach I suggested in action. Initially, several sections are added to a document. After that you can delete all of them using the "Delete sections" bar item in the "File" Ribbon tab.

                                • Anil_Gupta29 01.08.2014

                                  I tried the attached sample and it has the same issue. I would suggest you to try this.
                                  check the richEditControl1.Document.Sections.count after the for loop is executed for the first time. The count would not change.
                                  Check the count again after the for loop is executed and you would see the count as 1.
                                  We in-fact tried it in MS-Word as well and it is behaving similar way. just want to make sure that dev express behavior is expected behavior before communicating it to customer.

                                • I am afraid I don't understand what you're talking about. Attached is a video I recorded to illustrate how this project works on my side. The only difference with the project I sent you is that I added a BarStaticItem showing the number of sections. As you can see, all sections are deleted as expected after a single button click.
                                  Please modify this project to illustrate the issue you're experiencing and send it to us.

                                • Anil_Gupta29 01.09.2014

                                  I did some change in the code. I set the default section header and then after that I added the 5 sections. Now when I delete the section last section is remaining. I think last section can never be deleted. I am attaching the codebase.

                                • I have checked this sample and again I don't see any remaining section breaks. Only a single section is left after I press the button, but a document always has a single (default) section. I have recorded a video illustrating this behavior. Do you observe a different behavior on your side?

                                • Anil_Gupta29 01.13.2014

                                  I think we both are talking the same think & we are. at the same page. Thanks a lot for having patience for answering my questions.

                                • You're welcome, Anil. I am happy to hear that my assistance was helpful to you.

                                You must  log in  or  register  to leave comments

                                If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

                                FOLLOW US

                                DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, Silverlight, ASP.NET, WinForms, HTML5 or Windows 8, DevExpress tools help you build and deliver your best in the shortest time possible.

                                Copyright © 1998-2014 Developer Express Inc.
                                All trademarks or registered trademarks are property of their respective owners