Solved

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

Posted on 2016-11-22
9
69 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 29

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cant save 3D 4 20
Problem!!! 8 33
How to make a Gridview column hidden (visibility false) in the HTML code? 2 16
Can I command line build a C# application? 6 33
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…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 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