Quantcast
Channel: Telerik Forums RSS
Viewing all articles
Browse latest Browse all 94857

Selection problem: impossible to select more than one word

$
0
0

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.publicclassImprovedRichTextBox : RadRichTextBox
02.{
03.    privatestaticreadonlyDependencyProperty IsSuggestionConsistentProperty =
04.        DependencyProperty.Register(
05.            "IsSuggestionConsistent",
06.            typeof(bool),
07.            typeof(ImprovedRichTextBox),
08.            newPropertyMetadata(default(bool))
09.            );
10. 
11.    publicboolIsSuggestionConsistent
12.    {
13.        get{ return(bool)GetValue(IsSuggestionConsistentProperty); }
14.        set{ SetValue(IsSuggestionConsistentProperty, value); }
15.    }
16. 
17.    privateDocumentPosition _startPosition;
18.    privateDocumentPosition _previousPosition;
19. 
20.    // My custom added functionality
21.    protectedoverridevoidOnDocumentContentChanged()
22.    {
23.        base.OnDocumentContentChanged();
24. 
25.        var position = newDocumentPosition(Document.CaretPosition);
26. 
27.        var back = newDocumentPosition(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 = newDocumentPosition(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 = newDocumentPosition(position);
49. 
50.        if(!IsSuggestionConsistent)
51.        {
52.            _startPosition = newDocumentPosition(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.}

Viewing all articles
Browse latest Browse all 94857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>