Get selected words from Rich-Text box from Add-in

Go To


I have a VSTO add-in which has a menu and button. In my infopath form, i have multiple rich-text box which would be edited by user. While editing, if user select a word or sentence in any of the rich-text box and click the button from add-in, i need to add those selected text in a dropdown.

How can i get the selected text from the rich-text box? The selected may be in any one of the available rich text box, so i need to know the context to identify the rich-text box and then get only the selected text.

2012-04-03 20:09
by Karthik


In your btn_Clicked event, try this code. It should grab the text starting where the mouse is and ending at the end of the current block.

var curCaret = richTextBox1.CaretPosition;
var curBlock = richTextBox1.Document.Blocks.Where(x => x.ContentStart.CompareTo(curCaret) == -1 && x.ContentEnd.CompareTo(curCaret) == 1).FirstOrDefault();
2012-04-09 21:13
by Meyer Denney