Replace a specific text with Table in RadRichTextBox

3 posts, 1 answers
  1. Manish Mishra
    Manish Mishra avatar
    11 posts
    Member since:
    Feb 2010

    Posted 19 Jul 2012 Link to this post

    Am trying to search a specific Text in the Document, and replace it with Table.
    this is what am doing:
          private void Replace()
            {
                  this.radRichTextBox1.Document.Selection.Clear();
                  DocumentTextSearch ser = new DocumentTextSearch(this.radRichTextBox1.Document);
                  IList<DocumentPosition> startPositions = new List<DocumentPosition>();
                  IList<DocumentPosition> endPositions = new List<DocumentPosition>();
       
                  foreach (var textRange in ser.FindAll("<Threat"))
                    startPositions.Add(textRange.StartPosition);
                  foreach (var textRange in ser.FindAll(KEYEND))
                       endPositions.Add(textRange.EndPosition);
                  string xml = string.Empty;
                  for (int i = 0; i < startPositions.Count; i++)
                 {
                    this.radRichTextBox1.Document.Selection.Clear();
                    this.radRichTextBox1.Document.Selection.AddSelectionStart(startPositions[i]);
                    this.radRichTextBox1.Document.Selection.AddSelectionEnd(endPositions[i]);
                     
                    xml = this.radRichTextBox1.Document.Selection.GetSelectedText();
                    Table tbl = GenerateTable(xml);
                    this.radRichTextBox1.InsertTable(tbl);
                }



    but it doesn't work. when i try to replace the selection with any string, it works fine. what's wrong??? please help
  2. Manish Mishra
    Manish Mishra avatar
    11 posts
    Member since:
    Feb 2010

    Posted 19 Jul 2012 Link to this post

    Hello everyone..
    I found out the solution of my problem. 
    this is what i did:

       private void ConvertXMLToTable()
            {
                this.radRichTbParsedReport.Document.Selection.Clear();


                DocumentTextSearch ser = new DocumentTextSearch(this.radRichTbParsedReport.Document);
                IList<DocumentPosition> startPositions = new List<DocumentPosition>();
                IList<DocumentPosition> endPositions = new List<DocumentPosition>();


                foreach (var textRange in ser.FindAll(KEYIDENTIFIER))
                    startPositions.Add(textRange.StartPosition);
                foreach (var textRange in ser.FindAll(KEYEND))
                    endPositions.Add(textRange.EndPosition);
                string xml = string.Empty;


                for (int i = 0; i < startPositions.Count; i++)
                {
                    this.radRichTbParsedReport.Document.Selection.Clear();
                    this.radRichTbParsedReport.Document.Selection.AddSelectionStart(startPositions[i]);
                    this.radRichTbParsedReport.Document.Selection.AddSelectionEnd(endPositions[i]);
                    xml = this.radRichTbParsedReport.Document.Selection.GetSelectedText();


                    Table tbl = GenerateTable(xml);


                    this.radRichTbParsedReport.Document.Selection.Clear();
                    this.radRichTbParsedReport.Document.CaretPosition.MoveToPosition(startPositions[i]);
                    this.radRichTbParsedReport.Document.DeleteRange(startPositions[i], endPositions[i]);
                    this.radRichTbParsedReport.InsertTable(tbl);


                }


            }


    but now am struck with a new problem. 
    actually i have two radrichtextboxes, first richtextbox is the one containing all those specific words (which are to be replaced) and second richtextbox is the one going to contain tables instead of those words. so i do this to set the content of second richtextbox

    radRichTextBox2.Document= radRichTextBox1.Document

    Problem is: whenever i try to replace, replacement is done on both the richtextboxes contrary to what I wanted( just the second one). Not only that, whatever i write, insert on 2nd richtextbox, gets written, inserted (at the very same time) on first one aswell.

    am new to telerik winform controls, specially radrichtextbox...please help




  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Svett
    Admin
    Svett avatar
    728 posts

    Posted 23 Jul 2012 Link to this post

    Hi Manish,

    You should create a copy of the original document to avoid its modification in both editors. You can do that by invoking the CreateDeepCopy method of RadDocument:
    RadDocument copiedDocument = this.radRichTextBox1.Document.CreateDeepCopy() as RadDocument;
    this.radRichTextBox2.Document = copiedDocument;

    I hope this helps.

    Greetings,
    Svett
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Back to Top