Solved

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

Posted on 2016-11-22
9
78 Views
Last Modified: 2016-11-22
If user selected some text in textbox and then removed or replaced, how to determine than selected word was removed(replaced)
0
Comment
Question by:ybt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 41898027
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.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 41898029
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...
0
 

Author Comment

by:ybt
ID: 41898040
This is winform and  textBox.SelectedText != "" is not helpful, because user can unselect this text and there is nothing to check.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 41898059
Compare with previous content - that's what Stefan and I told you already.
0
 

Author Comment

by:ybt
ID: 41898090
Compare function gives only if strings equal or not
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 41898110
OK, I will try to explain possible algorithm...

1. You detected that something was selected in a text box - save current content.
2. Then you do some operation, e.g., click "Delete" or Right arrow,  - selection gone. Compare new text in the textbox and saved version.

Try something like:

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (textBox1.SelectedText != string.Empty)
            {
                _textBoxContent = this.textBox1.Text;
                System.Diagnostics.Debug.WriteLine("case ak");
            }
            else if (!_textBoxContent.Equals(string.Empty) )
            {
                System.Diagnostics.Debug.WriteLine("case bk");
                if (!textBox1.Text.Equals(_textBoxContent))
                {
                    MessageBox.Show("Changed");
                }
                else
                {
                    MessageBox.Show("Same");
                }
                _textBoxContent = string.Empty;
            }
            else
            {
                _textBoxContent = string.Empty;
                System.Diagnostics.Debug.WriteLine("case else k");
            }
        }

Open in new window


Note: code might need adjustments
0
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 41898123
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
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 41898136
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...
0
 

Author Comment

by:ybt
ID: 41898166
Yes, algorithm is interesting and TextChanged Event I tried to use, not helpful.
Thank you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question