MVVM and the TextBox’s SelectedText property

There’s no straightforward way to bind SelectedText to a data source, because it’s not a DependencyProperty… however, it quite easy to create an attached property that you could bind instead. Here’s a basic implementation : public static class TextBoxHelper { public static string GetSelectedText(DependencyObject obj) { return (string)obj.GetValue(SelectedTextProperty); } public static void SetSelectedText(DependencyObject obj, string … Read more

How to get global screen coordinates of currently selected text via Accessibility APIs.

You can use the accessibility APIs for that. Make sure that the “Enable access for assistive devices” setting is checked (in System Preferences / Universal Access). The following code snippet will determine the bounds (in screen coordinates) of the selected text in most applications. Unfortunately, it doesn’t work in Mail and Safari, because they use … Read more

How to get selected text from a textbox control with JavaScript

OK, here is the code I have: function ShowSelection() { var textComponent = document.getElementById(‘Editor’); var selectedText; if (textComponent.selectionStart !== undefined) { // Standards-compliant version var startPos = textComponent.selectionStart; var endPos = textComponent.selectionEnd; selectedText = textComponent.value.substring(startPos, endPos); } else if (document.selection !== undefined) { // Internet Explorer version textComponent.focus(); var sel = document.selection.createRange(); selectedText = sel.text; … Read more