Current filter:
                                You should refresh the page.
                                Support Center
                                0
                                  • dear support,
                                    again a question where I am not sure if its my stupidity or if I am missing something.

                                    I have an xtrarichedit document in which I want to replace the contents of a bookmark.
                                    I follow the codeexample you give under:
                                    http://www.devexpress.com/Support/Center/p/Q239392.aspx?searchtext=bookmark&p=T4|P0|22

                                    It works ok for the first bookmark. but not if there is more then one bookmark in the document.
                                    The old text is not replaced correctly by the new text only fragments of the new text appear.

                                    The createRange method does not do what I want it to do.

                                    My source follows with comments:

                                    string newText = "newtext";
                                    int length = newText.Length;
                                    DocumentPosition docPos = footnotes.Document.Bookmarks[bmIndex].Range.End;
                                    // docPos has now value of 16 which is correct;
                                    footnotes.Document.InsertText(docPos, newText);
                                    //correct

                                       DocumentRange range = footnotes.Document.CreateRange(
                                       footnotes.Document.Bookmarks[bmIndex].Range.Start,
                                       footnotes.Document.Bookmarks[bmIndex].Range.End.ToInt() - length);

                                    once the program has executed the above line there is an error:

                                    // footnotes.Document.Bookmarks[bmIndex].Range.Start = 5 : correct
                                    // footnotes.Document.Bookmarks[bmIndex].Range.End = 16 : correct
                                    // length = 7 : correct

                                    // range.End = 14 : wrong! should be 9 , 16 - 7

                                    I cannot figure out why I get 14 instaed of 9.
                                    thanks
                                    Markus

                                1 Solution

                                Creation Date Rating Importance Sort by
                                0

                                Hi Marku,

                                I suggest that you save the parameter of the current bookmark, delete current bookmark and create a new bookmark by using saved parameters. Here is some sample code:

                                [C#]
                                ... private void simpleButton1_Click(object sender, EventArgs e) { for (int bmIndex = 0; bmIndex < richEditControl1.Document.Bookmarks.Count; bmIndex++) { string name = richEditControl1.Document.Bookmarks[bmIndex].Name; DocumentPosition bmStart = richEditControl1.Document.Bookmarks[bmIndex].Range.Start; richEditControl1.Document.Delete(richEditControl1.Document.Bookmarks[bmIndex].Range); string newText = "newtext"; DocumentRange docRange = richEditControl1.Document.InsertText(bmStart, newText); richEditControl1.Document.CreateBookmark(docRange, name + bmIndex.ToString()); } } ...
                                Try this solution, and notify us of your results. Thanks for your feedback in advance.

                                Thanks,
                                Elliot

                                • Paul Usher (DevExpress) 09.01.2015
                                  How does the above example delete a bookmark?  I can see it deleting the text (Document.Delete(...)), but then it creates a new bookmark with a new name, the existing one stays. ?

                                  After further review, it appears if the length of the original bookmark is zero (e.g., just a place holder) then it is not removed with the Document.Delete() method, however, if it has a range (e.g., some text was added), then subsequent calls actually remove it.

                                  This seems to be erroneous. ?
                                • Andrey (DevExpress Support) 09.02.2015

                                  Hello Paul,
                                  The code snippet provided by Elliot does not take into account the bookmarks that have ranges with a zero length.
                                  Since the range length is equal to zero, the Document.Delete method is not doing anything since there is no range for delete. As a result, the bookmark with such a range is not deleted.
                                  The code snippet can be easily modified for the scenario you described.
                                  Check the bookmark range length, and if it is equal to zero, simply delete the bookmark using the Bookmarks.Remove method. If the range length is greater than zero, use the Document.Delete method as follows:

                                  [C#]
                                  private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e) { for(int bmIndex = 0; bmIndex < richEditControl.Document.Bookmarks.Count; bmIndex++) { string name = richEditControl.Document.Bookmarks[bmIndex].Name; DocumentPosition bmStart = richEditControl.Document.Bookmarks[bmIndex].Range.Start; if(richEditControl.Document.Bookmarks[bmIndex].Range.Length > 0) richEditControl.Document.Delete(richEditControl.Document.Bookmarks[bmIndex].Range); else richEditControl.Document.Bookmarks.Remove(richEditControl.Document.Bookmarks[bmIndex]); string newText = "newtext"; DocumentRange docRange = richEditControl.Document.InsertText(bmStart, newText); richEditControl.Document.Bookmarks.Create(docRange, name + bmIndex.ToString()); }


                                  Please try this solution and let me know your results.

                                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, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

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