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

How to add comment programmatically

5 Answers 128 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Marcel
Top achievements
Rank 1
Marcel asked on 21 Nov 2011, 10:26 AM
Hi, 

Based on a Selection, I would like to add a comment. 

I was wondering how i would do this within the code. 

So i have my selection:
this.editor.Document.Selection // I would like to add my comment for this selection. 

An example for doing this would be great. 

Thanks 


5 Answers, 1 is accepted

Sort by
0
Marcel
Top achievements
Rank 1
answered on 22 Nov 2011, 01:26 PM
I have tried this:

Comment comment = new Comment(commentmessage);
DocumentPosition startPosition = RadRichTextDocument.Document.Selection.Ranges.First.StartPosition;
DocumentPosition endPosition =  RadRichTextDocument.Document.Selection.Ranges.First.EndPosition; 
RadRichTextDocument.Document.InsertComment(startPosition, endPosition, comment);
RadRichTextDocument.UpdateLayout();
But nothing seems to happen... Is there anything i'm missing out?
0
Marcel
Top achievements
Rank 1
answered on 22 Nov 2011, 04:45 PM
Answer to my question:

RadRichTextDocument.ShowComments = true; // I WAS MISSING THIS OUT!!!
 
			DocumentPosition startPosition = RadRichTextDocument.Document.CaretPosition;
			DocumentPosition endPosition = new DocumentPosition(startPosition);
 
			startPosition.MoveToFirstPositionInParagraph();
			endPosition.MoveToLastPositionInParagraph();
 
			RadRichTextDocument.Document.InsertComment(startPosition, endPosition, new Comment(commentmessage));
0
Mihail
Telerik team
answered on 23 Nov 2011, 02:56 PM
Hello Chez,

We are glad you found out the solution to the problem. Lets us know if you experience any other setbacks.

Best wishes,
Mihail
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Marcel
Top achievements
Rank 1
answered on 12 Dec 2011, 05:55 PM
Hi, I have managed to delete all the comments in the document using:

CommentRangeStart[] cR= RadRichTextDocument.Document.EnumerateChildrenOfType<CommentRangeStart>().ToArray();
   for (int i = 0; i < cR.Length; i++)
			{
				CommentRangeStart currentC = cR[i];
			 	start.MoveToInline(currentC.FirstLayoutBox as InlineLayoutBox, 0);
			 	end.MoveToInline(currentC.End.FirstLayoutBox as InlineLayoutBox, 0);
				end.MoveToNextInline();
 
				RadRichTextDocument.DeleteComment(currentComment);
			}

But I want to delete a comment which is within the currently selected text, how would i go about doing this? 

Thanks 
0
Iva Toteva
Telerik team
answered on 14 Dec 2011, 02:31 PM
Hello Chez,

You can get the boxes in the selection and remove all comments like this:

if (!this.editor.Document.Selection.IsEmpty)
{
    IEnumerable<InlineLayoutBox> selectedBoxes = this.editor.Document.Selection.GetSelectedBoxes().ToList<InlineLayoutBox>();
    foreach (InlineLayoutBox inlineBox in selectedBoxes)
    {
        if (inlineBox.AssociatedInline is CommentRangeStart)
        {
            this.editor.DeleteComment((CommentRangeStart)inlineBox.AssociatedInline);
        }
    }
}

In this way, all comments that have their start within the selection will be removed. You can further customize this solution to check if the CommentRangeEnd is within the selection instead of CommentRangeStart or check if both the start and end of the comment are within the selection.

However, you should have in mind that each delete operation will be registered separately in the history stack and undo/redo will add/remove only one comment at a time. I hope this helps.

Greetings,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
RichTextBox
Asked by
Marcel
Top achievements
Rank 1
Answers by
Marcel
Top achievements
Rank 1
Mihail
Telerik team
Iva Toteva
Telerik team
Share this question
or