RadRichTextBox with Replacing some value on Drag and Drop

12 posts, 0 answers
  1. Pradnya
    Pradnya avatar
    31 posts
    Member since:
    Aug 2011

    Posted 29 May 2012 Link to this post

    Hi All,

    I have implemented RadRichTextBox with Drag n Drop functionality

    When I drop the <<Collateral>> I want to replace <*parm1*> with this value [<<Collateral>> ]. Please find the more information in image.

    XAML:

    <telerik:RadRichTextBox Grid.Row="1" x:Name="editor" ShowComments="False"                                             telerikDragDrop:RadDragAndDropManager.AllowDrop="True" KeyDown="editor_KeyDown" Height="108" VerticalAlignment="Top">
      </telerik:RadRichTextBox>

    OnDropInfo:

    PointF pointF=ew PointF((float)e.Options.RelativeDragPoint.X, (float)e.Options.RelativeDragPoint.Y)

    DocumentPosition docPosition = new DocumentPosition(editor.Document);
                docPosition.SetPosition(pointF);
                var styleDef = new StyleDefinition();
                styleDef.SetPropertyValue(Telerik.Windows.Documents.Model.Span.ForeColorProperty, System.Windows.Media.Color.FromArgb(255,35,142,35));
                box.Document.Insert("DropValue", styleDef, docPosition);

                //After DD is inserted, remove the highligting
                docPosition.MoveToCurrentWordEnd();
                styleDef.Clear();
                box.Document.Insert("", styleDef, docPosition);

                //Put cursor at the end of the DD
                box.Focus();
                box.Document.CaretPosition.MoveToPosition(docPosition);


    Please help me how can implement this scenario.

    Thanks-

  2. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 04 Jun 2012 Link to this post

    Hi Pradnya,

    In order to replace some of the content of the editor with the text you would obtain from the drag-and-drop operation, you have to determine what part exactly you would like to have deleted. This can be done by surrounding that part of the document with annotation ranges - Bookmarks, Comments or custom annotation ranges. You can find more information on bookmarks here, whereas the custom annotation ranges approach is described in this article.

    For example, if you surround with comments <*parm1*> and the other parts of the document you would like to have replaced on drop, you can handle the DropComplete case like this:

    RadRichTextBox rrtb = e.Options.Destination as RadRichTextBox;
    Point viewPoint = e.Options.RelativeDragPoint;
    DocumentPosition pos = rrtb.ActiveEditorPresenter.GetDocumentPositionFromViewPoint(viewPoint);
    Inline currentInline = pos.GetCurrentInlineBox().AssociatedInline;
    IEnumerable<CommentRangeStart> comments = rrtb.Document.GetContainingAnnotationRanges<CommentRangeStart>(currentInline, true);
    if (comments != null && comments.Count() > 0)
    {
        rrtb.Document.Selection.SelectAnnotationRange(comments.First());
        rrtb.Delete(false);
    }
     
    string text = ((RadTreeViewItem)e.Options.Payload).Header.ToString();
    var styleDef = new StyleDefinition(StyleType.Character);
    styleDef.SetPropertyValue(Telerik.Windows.Documents.Model.Span.ForeColorProperty, System.Windows.Media.Color.FromArgb(255, 35, 142, 35));
    rrtb.Document.Insert(text, styleDef);
     
    e.Handled = true;

    In the example the Payload is a RadTreeView item.

    Please, let us know if you managed to get it working or you need further assistance.

    Regards,
    Iva Toteva
    the Telerik team

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

  3. DevCraft banner
  4. Pradnya
    Pradnya avatar
    31 posts
    Member since:
    Aug 2011

    Posted 04 Jun 2012 Link to this post

    Hi Iva,

    Thank you for your valuable reply,

    In order to replace some of the content of the editor with the text you would obtain from the drag-and-drop operation, you have to determine what part exactly you would like to have deleted : Always I want to delete text <*Param1.... *>

    So please help me how can I get text between "<*" and "*>"

    I have used follwing methods to resolved above issue using WPF RichTextBox. I want to implement same scenario in Silverlight using Telerik RichTextBox

     private bool IsTheDropPointInsideAnAreaThatNeedsToBeReplaced()
            {
                for (var i = richTextBox1.SelectionStart - 1; i > -1; i--)
                {
                    try
                    {
                        if (richTextBox1.Text[i] == '<')
                        {
                            if (richTextBox1.Text[i - 1] == '<' || richTextBox1.Text[i + 1] == '*')
                            {
                                return true;
                            }
                        }
                        if (richTextBox1.Text[i] == '>')
                        {
                            if (richTextBox1.Text[i - 1] == '>' || richTextBox1.Text[i - 1] == '*')
                            {
                                return false;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
                return false;
            }
            private int FindStart()
            {
                var left = richTextBox1.SelectionStart - 1;
                var start = 0;

                for (var i = left; i > 0; i--)
                {
                    // Find a replacable field indicator
                    if (richTextBox1.Text[i] == '<')
                    {
                        if (richTextBox1.Text[i + 1] == '*')
                        {
                            start = i;
                            break;
                        }
                        if (richTextBox1.Text[i - 1] == '<')
                        {
                            start = i - 1;
                            break;
                        }
                    }
                }
                return start;
            }
            private int FindEnd()
            {
                var right = richTextBox1.SelectionStart;
                var end = 0;

                for (var i = right; i < richTextBox1.TextLength; i++)
                {
                    // Find a replacable field indicator
                    if (richTextBox1.Text[i] == '>')
                    {
                        if (richTextBox1.Text[i - 1] == '*')
                        {
                            end = i;
                            break;
                        }
                        if (richTextBox1.Text[i + 1] == '>')
                        {
                            end = i + 1;
                            break;
                        }
                    }
                }
                return end;
            }

    Thanks-



  5. Pradnya
    Pradnya avatar
    31 posts
    Member since:
    Aug 2011

    Posted 07 Jun 2012 Link to this post

    Hello Guys,

    I am expecting some answer from you, is it possible to implement this scenario?

    Tell me some ways to achieve my scenario.

    Thanks-
  6. Boby
    Admin
    Boby avatar
    595 posts

    Posted 07 Jun 2012 Link to this post

    Hi Pradnya,
    RadRichTextBox doesn't have text indexer, so the best way is what Iva suggested - to insert some annotations (custom or not) around the text to be replaced, then detect them and replace the text between them.
    If you decide to stick to your implementation, you could try to select different parts of the document using RadDocument.Selection.SetSelectionStart and RadDocument.AddSelectionEnd methods and get the text content for the selected part using RadDocument.Selection.GetSelectedText(). This, however, should be harder to implement and most likely much slower, as RadRichTextBox uses advanced data structures and algorithms for finding the containing annotation ranges.

    Greetings,
    Boby
    the Telerik team

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

  7. Pradnya
    Pradnya avatar
    31 posts
    Member since:
    Aug 2011

    Posted 08 Jun 2012 Link to this post

    Thank you for your reply.

    Can you help me how can I insert comment at run time. So that I can replace my text using comment.

    Thanks-
  8. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 08 Jun 2012 Link to this post

    Hi Pradnya,

    Please find attached an example illustrating how a template with comments can be created and how these comments can be deleted and substituted for text on drag and drop.

    Look through it and let us know if you have any questions or comments about the implementation.

    Greetings,
    Iva Toteva
    the Telerik team

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

  9. Pradnya
    Pradnya avatar
    31 posts
    Member since:
    Aug 2011

    Posted 14 Jun 2012 Link to this post

    Hi Iva,

    Thank you for reply it's useful to resolved my issue.

    Thanks-
  10. Pradnya
    Pradnya avatar
    31 posts
    Member since:
    Aug 2011

    Posted 14 Jun 2012 Link to this post

    Hi All,

    How can I load template at run time I mean the number of Param values are differents, its not fixed size.

    for ex.- <*parm1*>,<*parm2*>,<*parm3*> - 3 Parameters
    <*parm1*>,<*parm2*> - 2 Parameters
    <*parm1*>,<*parm2*>,<*parm3*><*parm1*>,<*parm2*>,<*parm3*>- 6 Parameters

    Thanks-
  11. Pradnya
    Pradnya avatar
    31 posts
    Member since:
    Aug 2011

    Posted 14 Jun 2012 Link to this post

    Hi Iva,

    One more question about Document Object,

    I want to attched one more expression with current document

    ex:

    Concat(<*Param1*>,<*Param2*>

    On drop I want attached this existing expression with INDEXOF(<*Param1*>,<*Param2*>)

    So final expression will Be in RichTextBox is

    Concat(<*Param1*>,<*Param2*> INDEXOF(<*Param1*>,<*Param2*>)

    Please find more infomation in attached image.

    So please help me how can I implement this scenario using your code?

    Thanks-



  12. Pradnya
    Pradnya avatar
    31 posts
    Member since:
    Aug 2011

    Posted 15 Jun 2012 Link to this post

    Hello Guys,

    Please help us to resolved this issues ASAP.

    Thanks-
  13. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 19 Jun 2012 Link to this post

    Hello Pradnya,

    With regard to the creation of the template, you can see how spans and comments are added to the document programmatically. You can create an arbitrary number of comments and spans and add them in this way.

    When it comes to inserting a part of the document with comments in the rich text box dynamically, you can create a RadDocument with different content (paragraphs, spans and comments) and then insert it in the RadRichTextBox like this:

    RadDocument document = this.CreateDocumentWithComments();
    DocumentFragment fragment = new DocumentFragment(document);
    this.radRichTextBox.InsertFragment(fragment);

    This will insert the new content at the current caret position. 

    Here is a sample implementation of the CreateDocumentWithComments method:
    private RadDocument CreateDocumentWithComments()
    {
        RadDocument document = new RadDocument();
     
        Section section = new Section();
        document.Sections.Add(section);
     
        Paragraph paragraph = new Paragraph();
        section.Blocks.Add(paragraph);
        Span spanBefore = new Span("INDEXOF(");
     
        CommentRangeEnd end1 = new CommentRangeEnd();
        CommentRangeStart start1 = end1.CreatePairedStart() as CommentRangeStart;
        end1.Comment = new Comment();
     
        Span spanParm1 = new Span("<*parm1*>");
        Span spanComma1 = new Span(",");
     
        CommentRangeEnd end2 = new CommentRangeEnd();
        CommentRangeStart start2 = end2.CreatePairedStart() as CommentRangeStart;
        end2.Comment = new Comment();
     
        Span spanParm2 = new Span("<*parm2*>");
     
        Span spanAfter = new Span(")");
     
        paragraph.Inlines.Add(spanBefore);
        paragraph.Inlines.Add(start1);
        paragraph.Inlines.Add(spanParm1);
        paragraph.Inlines.Add(end1);
        paragraph.Inlines.Add(spanComma1);
        paragraph.Inlines.Add(start2);
        paragraph.Inlines.Add(spanParm2);
        paragraph.Inlines.Add(end2);
     
        paragraph.Inlines.Add(spanAfter);
     
        document.MeasureAndArrangeInDefaultSize();
        document.StyleRepository.Add(this.GetGreenCharacterStyleDefinition());
     
        return document;
    }

    Of course, you can refactor this method so that the information about the number of spans and their text is passed as parameters.

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