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

How to detect onSelectionChanged?

1 Answer 88 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Jeffrey Tucker
Top achievements
Rank 1
Jeffrey Tucker asked on 29 Aug 2011, 08:47 PM
I'm building a clinical report writer (emits docx format document) that has a set of special tokens that begin with "[!" and end with "!]".  When the user changes the selection I need to get notification of the event.  If the selection is inside one of these tokens, show a special editor (modeless) to customize runtime text substitutions.  How can I setup to get that notification?  I already tried Left mouse down and it does not even get called..
 private void radRichTextBox1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (radRichTextBox1.Document != null && radRichTextBox1.Document.Selection != null)
    {
         DocumentSelection selection = radRichTextBox1.Document.Selection;
         string selectedText = selection.GetSelectedText();
    }
}

1 Answer, 1 is accepted

Sort by
0
Boby
Telerik team
answered on 01 Sep 2011, 09:06 AM
Hi Jeffrey,
You can use DocumentSelection.SelectionChanged event. Note that  if RadRichTextBox's document is changed you have to unsubscribe from the event hander for the old document's selection and subscribe for the new one:
public MainPage()
{
    InitializeComponent();
 
    this.radRichTextBox1.DocumentChanging += (s, e) =>
    {
        if (this.radRichTextBox1.Document != null)
        {
            this.radRichTextBox1.Document.Selection.SelectionChanged -= Selection_SelectionChanged;
        }
    };
 
    this.radRichTextBox1.DocumentChanged += (s, e) =>
    {
        if (this.radRichTextBox1.Document != null)
        {
            this.radRichTextBox1.Document.Selection.SelectionChanged += Selection_SelectionChanged;
        }
    };
 
    this.radRichTextBox1.Document.Selection.SelectionChanged += Selection_SelectionChanged;
}
 
private void Selection_SelectionChanged(object sender, EventArgs e)
{
    if (this.radRichTextBox1.Document.Selection.GetSelectedText() == "test")
    {
        MessageBox.Show("test");
    }
}
 
private void Button_Click(object sender, RoutedEventArgs e)
{
    // if the document is changed, you have to subscribe for the event of the new selection
    this.radRichTextBox1.Document = new Telerik.Windows.Documents.Model.RadDocument();
}

We will consider exposing such event directly on RadRichTextBox to simplify scenarios like yours.
Don't hesitate to contact us if you have other questions.

Best wishes,
Boby
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
RichTextBox
Asked by
Jeffrey Tucker
Top achievements
Rank 1
Answers by
Boby
Telerik team
Share this question
or