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

Replace a specific text with Table in RadRichTextBox

2 Answers 328 Views
RichTextBox (obsolete as of Q3 2014 SP1)
This is a migrated thread and some comments may be shown as answers.
Manish Mishra
Top achievements
Rank 1
Manish Mishra asked on 19 Jul 2012, 09:14 AM
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 Answers, 1 is accepted

Sort by
0
Manish Mishra
Top achievements
Rank 1
answered on 19 Jul 2012, 12:08 PM
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




0
Accepted
Svett
Telerik team
answered on 23 Jul 2012, 01:22 PM
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 >>
Tags
RichTextBox (obsolete as of Q3 2014 SP1)
Asked by
Manish Mishra
Top achievements
Rank 1
Answers by
Manish Mishra
Top achievements
Rank 1
Svett
Telerik team
Share this question
or