Solved

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

Posted on 2016-11-22
9
62 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
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 29

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
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 41898059
Compare with previous content - that's what Stefan and I told you already.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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

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 29

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 54
IO.FileSystemWatcher dies when used in Powershell 1 45
Is setting this variable making a duplicate in memory 4 30
Device Location 4 13
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now