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

Selection problem: impossible to select more than one word

9 Answers 47 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Art
Top achievements
Rank 1
Art asked on 27 Apr 2015, 07:46 AM

I'm trying to create an extended control, which will display tips for writing. And everything is good, but the last step was faced with a problem.

Typing more then one word causes selection like "\r\n".

For example, type "Lorem" - it's ok. But type "Lorem " (with space) - broken.

 

01.public class ImprovedRichTextBox : RadRichTextBox
02.{
03.    private static readonly DependencyProperty IsSuggestionConsistentProperty =
04.        DependencyProperty.Register(
05.            "IsSuggestionConsistent",
06.            typeof (bool),
07.            typeof (ImprovedRichTextBox),
08.            new PropertyMetadata(default(bool))
09.            );
10. 
11.    public bool IsSuggestionConsistent
12.    {
13.        get { return (bool)GetValue(IsSuggestionConsistentProperty); }
14.        set { SetValue(IsSuggestionConsistentProperty, value); }
15.    }
16. 
17.    private DocumentPosition _startPosition;
18.    private DocumentPosition _previousPosition;
19. 
20.    // My custom added functionality
21.    protected override void OnDocumentContentChanged()
22.    {
23.        base.OnDocumentContentChanged();
24. 
25.        var position = new DocumentPosition(Document.CaretPosition);
26. 
27.        var back = new DocumentPosition(position);
28. 
29.        if (!back.MoveToPrevious())
30.        {
31.            IsSuggestionConsistent = false;
32.            return;
33.        }
34. 
35.        if (IsSuggestionConsistent)
36.        {
37.            var i = _previousPosition.CompareTo(back);
38. 
39.            var end = new DocumentPosition(position);
40.            end.MoveToLastPositionInDocument();
41. 
42.            if (i != 0 && (end.CompareTo(position) != 0 || end.CompareTo(_previousPosition) != 0))
43.            {
44.                IsSuggestionConsistent = false;
45.            }
46.        }
47. 
48.        _previousPosition = new DocumentPosition(position);
49. 
50.        if (!IsSuggestionConsistent)
51.        {
52.            _startPosition = new DocumentPosition(back);
53.            IsSuggestionConsistent = true;
54.        }
55. 
56.        var selection = Document.Selection;
57. 
58.        // KEY feature: begin
59.        var selectionStart = _startPosition;
60. 
61.        // KEY feature: actual end of typing phrase
62.        var selectionEnd = position;
63. 
64.        //
65.        // The problem area is here:
66.        //
67. 
68.        selection.SetSelectionStart(selectionStart);
69.        selection.AddSelectionEnd(selectionEnd);
70. 
71.        // Everything is good while typing a word like "Lorem"
72.        //
73.        // But it will break if you put a space... Actually, it should be "Lorem ipsum", but catches only "\r\n"
74. 
75.        Debug.WriteLine("|" + selection.GetSelectedText() + "|");
76.        selection.Clear();
77.    }
78.}

9 Answers, 1 is accepted

Sort by
0
Accepted
Tanya
Telerik team
answered on 28 Apr 2015, 01:53 PM
Hi Art,

The DocumentPosition is a complex object, which depends on the RadDocument's state and when you are modifying the position and the document at the same time it is possible to experience some issues. We looked at the code and modified it to work and you can find it in the attached file, but we are not sure what exactly the end goal is. Could you give us more details about the scenario and what you need to do on the DocumentContentChanged event? Do you want to select the last word, the whole text/sentence or something else? We might be able to provide advice on another, more applicable approach. 

As to the modifications in the code, when the DocumentPosition is initialized, now it is using another constructor with additional TrackDocumentChangeEvents parameter, which specifies that the DocumentPosition should be kept in sync with the text even if the latter is changed.

Additionally, note that the implementation of such scenario could lead to some performance issues due to the frequent execution of the code in the DocumentContentChanged event handler.

Regards,
Tanya
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Art
Top achievements
Rank 1
answered on 28 Apr 2015, 02:49 PM

I'm trying to do something like IntelliSense in VisualStudio. It catches user input from start (when the caret was placed and text input occurs) to last typed letter (while typing sequentially). This sequence later will be transferred to the search engine for a dynamic autocompletion. If the database has no query matches, then it set IsSuggestionConsistent to false.

When the caret moved out, the start position will be changed to a new location of the caret (sets IsSuggestionConsistent into false too).

Great! I just paid attention to this document state with your example. And I found another (perhaps, the last) problem...

If you type "custom test" and place the cursor back to "cus|" (for example), then type any letter and space. In theory, sequence should have "2_" (with space), but in all gaps, it starts a new countdown from last typed space.

 But! We typed first "custom test" with space and it goes without break like "custom" and " test".

0
Art
Top achievements
Rank 1
answered on 29 Apr 2015, 07:49 AM
There is "correct" insertion (pic. 1) and "broken" (pic. 2)
0
Art
Top achievements
Rank 1
answered on 29 Apr 2015, 01:20 PM
Demo app screen in the attached file
0
Tanya
Telerik team
answered on 01 May 2015, 02:01 PM
Hi Art,

Just to let you know that we are currently looking into the case and as soon as we have some information to share will write you back.

Regards,
Tanya
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Accepted
Tanya
Telerik team
answered on 08 May 2015, 12:23 PM
Hello Art,

We thoroughly investigated the case and noticed that there is a bug with the DocumentPosition in the RadRichTextBox, which is preventing us to give you a suitable suggestion. When you type a space, the position jumps back and changes its expected location.

At this point I could not give you a specific time-frame when this issue will be fixed, but I logged it in our backlog and you could track our progress on the matter by following the related public item in the feedback portal. Your Telerik points were updated as a token of appreciation for this report. 

I am afraid that I am not able to give you a workaround of this issue.

Please, excuse us for the inconvenience.

Regards,
Tanya
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Art
Top achievements
Rank 1
answered on 08 May 2015, 12:41 PM

Thank you for your support.

Ok, I'll be waiting for the bug fix.

0
Art
Top achievements
Rank 1
answered on 15 Sep 2015, 07:17 AM
Hello, any workarounds? =) Approved for 4 months.
0
Tanya
Telerik team
answered on 17 Sep 2015, 03:04 PM
Hello Art,

This issue is not scheduled for fixing yet and I am not able to say when a fix might be available. The best I could suggest you at this point is to subscribe to the feedback item I gave you in my previous post in order to receive notifications when its status changes.

Regards,
Tanya
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
RichTextBox
Asked by
Art
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Art
Top achievements
Rank 1
Share this question
or