Link to home
Start Free TrialLog in
Avatar of ybt
ybt

asked on

c# find out if selected(highlighted) text in textbox was replaced or removed

If user selected some text in textbox and then removed or replaced, how to determine than selected word was removed(replaced)
Avatar of ste5an
ste5an
Flag of Germany image

When this is all information you've got: Then there is no way.

The normal approach would be comparing the old value with the current value. Which means that you store old value in some variable and compare this variable with the current content.
First, is it in winform or ASP?

Second: a) find out if something was selected, e.g.
private void textBox_KeyUp(object sender, KeyEventArgs e) {
    if (textBox.SelectedText != "")
        MessageBox.Show("Selected");
}

Open in new window


b) e.g., compare text before and after...
Avatar of ybt
ybt

ASKER

This is winform and  textBox.SelectedText != "" is not helpful, because user can unselect this text and there is nothing to check.
Compare with previous content - that's what Stefan and I told you already.
Avatar of ybt

ASKER

Compare function gives only if strings equal or not
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi ybt,

How is TextChanged Event !!

Occurs when the Text property value changes. It occurs when the text is modified in a TextBox. With it  make a program dependent on the value of a TextBox when the user types.

Regards,
Tapan Pattanaik
If you select text and then unselect your event won't be fired. You don't even know if something was selected.
However, this event could be used in conjunction with KeyUp etc.

Also, we need to remember that some operations can be done by a mouse, so we need to code mouse events as well...
Avatar of ybt

ASKER

Yes, algorithm is interesting and TextChanged Event I tried to use, not helpful.
Thank you