Solved

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

Posted on 2016-11-22
9
46 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 32

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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!
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 video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

20 Experts available now in Live!

Get 1:1 Help Now