How to add comment programmatically

6 posts, 0 answers
  1. Marcel
    Marcel avatar
    37 posts
    Member since:
    Aug 2011

    Posted 21 Nov 2011 Link to this post

    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 
    
    
    
  2. Marcel
    Marcel avatar
    37 posts
    Member since:
    Aug 2011

    Posted 22 Nov 2011 Link to this post

    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?
  3. Marcel
    Marcel avatar
    37 posts
    Member since:
    Aug 2011

    Posted 22 Nov 2011 Link to this post

    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));
  4. Mihail
    Admin
    Mihail avatar
    241 posts

    Posted 23 Nov 2011 Link to this post

    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 >>

  5. Marcel
    Marcel avatar
    37 posts
    Member since:
    Aug 2011

    Posted 12 Dec 2011 Link to this post

    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 
  6. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 14 Dec 2011 Link to this post

    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 >>

Back to Top