How to set Document.Selection programmatically ?

6 posts, 0 answers
  1. Michel
    Michel avatar
    6 posts
    Member since:
    Nov 2012

    Posted 12 Dec 2012 Link to this post

    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
  2. Petya
    Admin
    Petya avatar
    975 posts

    Posted 14 Dec 2012 Link to this post

    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.

  3. DevCraft banner
  4. Michel
    Michel avatar
    6 posts
    Member since:
    Nov 2012

    Posted 14 Dec 2012 Link to this post

    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
  5. Petya
    Admin
    Petya avatar
    975 posts

    Posted 18 Dec 2012 Link to this post

    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.

  6. Michel
    Michel avatar
    6 posts
    Member since:
    Nov 2012

    Posted 19 Dec 2012 Link to this post

    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
  7. Petya
    Admin
    Petya avatar
    975 posts

    Posted 21 Dec 2012 Link to this post

    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.

Back to Top
DevCraft banner