tags:

views:

74

answers:

4

I'm looking for a method to get the value for a selected word from textbox.

For example:

I have textbox.Text =" How are you";

when I select "are" I should get message with selected word MessegeBox.Show(selectedWord);

+6  A: 

The Windows Forms TextBox control has a SelectedText property which will return the selected portion of the TextBox's value.

http://msdn.microsoft.com/en-us/library/aa288415%28VS.71%29.aspx

Nathan Taylor
+3  A: 

From MSDN's Code: Determining the Selected Text in a TextBox Control (Visual C#)

private void button1_Click(object sender, System.EventArgs e)
{
    textBox1.Text = "Hello World";
    textBox1.Select(6, 5);
    MessageBox.Show(textBox1.SelectedText);
}
Jay Riggs
+1  A: 

TextBox has a SelectedText property which will return whatever text is selected in the box.

MusiGenesis
+3  A: 

Windows Forms TextBox doesn't have a SelectionChanged event, although the RichTextBox control does. You could use a variety of hacks such as a timer, handling mouse up and key up events to trigger your selection change logic. Then use the SelectedText as others have suggested.

Josh Einstein