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

RadRichTextBox with Replacing some value on Drag and Drop

11 Answers 291 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Pradnya
Top achievements
Rank 1
Pradnya asked on 29 May 2012, 05:05 PM
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-

11 Answers, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 04 Jun 2012, 08:52 AM
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 >>

0
Pradnya
Top achievements
Rank 1
answered on 04 Jun 2012, 11:22 AM
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-



0
Pradnya
Top achievements
Rank 1
answered on 07 Jun 2012, 02:44 PM
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-
0
Boby
Telerik team
answered on 07 Jun 2012, 03:35 PM
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 >>

0
Pradnya
Top achievements
Rank 1
answered on 08 Jun 2012, 01:21 PM
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-
0
Iva Toteva
Telerik team
answered on 08 Jun 2012, 03:47 PM
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 >>

0
Pradnya
Top achievements
Rank 1
answered on 14 Jun 2012, 07:36 AM
Hi Iva,

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

Thanks-
0
Pradnya
Top achievements
Rank 1
answered on 14 Jun 2012, 10:38 AM
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-
0
Pradnya
Top achievements
Rank 1
answered on 14 Jun 2012, 03:28 PM
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-



0
Pradnya
Top achievements
Rank 1
answered on 15 Jun 2012, 03:41 PM
Hello Guys,

Please help us to resolved this issues ASAP.

Thanks-
0
Iva Toteva
Telerik team
answered on 19 Jun 2012, 09:55 AM
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 >>

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