Selecting a word from a text box

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

For instance:

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

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

+2


a source to share


4 answers


The Windows Forms TextBox does not have a SelectionChanged event, although the RichTextBox. You can use various hacks such as timer, mouse handling and event triggering to trigger the logic for changing the selection. Then use SelectedText as others have suggested.



+3


a source


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



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

+6


a source


From MSDN Code: Define 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);
}

      

+3


a source


TextBox

has a property SelectedText

that will return any text in the field.

+1


a source







All Articles