[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2016-11-22
9
Medium Priority
?
106 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 36

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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 22

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

Independent Software Vendors: 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

865 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