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

How to set Document.Selection programmatically ?

5 Answers 112 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Michel
Top achievements
Rank 1
Michel asked on 12 Dec 2012, 07:40 PM
Hi,
I have a custom control inheriting from RadRichTextBox.
I modify the document with RadRichTextBox commands, but sometimes, the RadRichTextBox selection is lost when I click on (for example) a colorpicker, a combobox...
So, I memorize selection when my mouse leave control like this :

private void TextArea_MouseLeave(object sender, EventArgs e)
        {
        if ((SelectionRange)this.Document.Selection.Ranges.First != null)
            {
                startPositionMemory = ((SelectionRange)this.Document.Selection.Ranges.First).StartPosition;
                stopPositionMemory = ((SelectionRange)this.Document.Selection.Ranges.First).EndPosition;
                positionMemory = this.Document.Selection.Ranges.First.StartPosition.Location;
            }
       }

It seems to work well.
Then, I try to set selection just before command executes :

private void TextArea_CommandExecuting(object sender, CommandExecutingEventArgs e)
        {
            if (this.Document.Selection.Ranges.Count == 0 && startPositionMemory != null && stopPositionMemory != null)
            {
                this.Document.CaretPosition.SetPosition(positionMemory);
                this.Document.Selection.SetSelectionStart(startPositionMemory);
                this.Document.Selection.AddSelectionEnd(stopPositionMemory);
            }
        }

Document.Selection is highlighted, but command does not apply.
What is the problem with this code ?
Thank you for your response !

Michel

5 Answers, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 14 Dec 2012, 11:02 AM
Hello Michel,

We are not quite sure what is your setup. The only way to clear the selection using our predefined UI we could think of is to enter HeaderFooterEditMode. However, I tested the code you pasted and it looks like when closing the mode the selection, as you observe, is set again and the command (ExitHeaderFooterEditModeCommand) is executed as expected.

That said, can you please elaborate on your overall scenario or even better open a support ticket and send us a sample project we can use for testing.

Thank you for your cooperation!
 
All the best,
Petya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Michel
Top achievements
Rank 1
answered on 14 Dec 2012, 01:15 PM
Hi Petya,
My main issue is the TextArea (inherited from RadRichTextBox) selection lost.
I try to modify TextArea content properties (like highlightcolor) with the help of a RadPropertyGrid, but (most of the time) when I clicked on a colorpicker in the RadPropertyGrid, RadDocument.Selection becomes empty.
Here's the template I used with RadPropertyGrid :

<DataTemplate x:Key="textAreaBackgroundTemplate">
                <telerik:HighlightColorPicker AutomaticColor="Transparent" HorizontalAlignment="Left" HorizontalContentAlignment="Left" Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/highlight.png" NoColorText="No color" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeFontHighlightColorCommand}" SelectedColor="Yellow" telerik:StyleManager.Theme="Office_Blue" />
</DataTemplate>

TextArea_MouseLeave and TextArea_CommandExecuting are just trials of workarounds.
For example, in my scenario, if I select text in TextArea, I can change highlightcolor from the RadPropertyGrid, but if I will change again highlightcolor the same way, nothing happens.

Hope this helps for your understanding of my problem (it will be difficult for me to send project, because my actual project is composed of many components).

Michel
0
Petya
Telerik team
answered on 18 Dec 2012, 06:04 PM
Hello Michel,

If I understand correctly, you are binding a RadPropertyGrid to RadRichTextBox's Commands. Can you please verify that?

I tried reproducing the loss of selection, but to no avail. I am also attaching the repro project for your reference. Will you please take a look at it and tell us what is different in your setup?

I'm looking forward to your reply.
 
Greetings,
Petya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Michel
Top achievements
Rank 1
answered on 19 Dec 2012, 03:10 PM
Hi Petya,
Thanks for your example.
I had a "OnLostFocus" event attached on my RadRichTextBox.The lost of focus seems to be linked with this event, since I've removed it I have no problem anymore...
Michel
0
Petya
Telerik team
answered on 21 Dec 2012, 05:05 PM
Hello Michel,

I am glad the problem is resolved!

If you experience other difficulties feel free to contact us again.
 
Kind regards,
Petya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
RichTextBox
Asked by
Michel
Top achievements
Rank 1
Answers by
Petya
Telerik team
Michel
Top achievements
Rank 1
Share this question
or