A Ctrl + V problem, Ctrl Problem

Source: Internet
Author: User

A Ctrl + V problem, Ctrl Problem

I am afraid there is nothing more familiar with computer shortcuts than Ctrl + C and Ctrl + V.

Recently I made a small program with a text box on the interface. The task is to copy the content from other places and paste it into the text box, and then display it in the Self-processed format.

To facilitate Problem description, for example, after simplifying other functions, I copied a text "C ", then press Ctrl + V on the text box and put it in the text box. Now, what the program wants to do is to make the text box show only what I want to display, no matter what you copy. In this way, the text box is displayed as "ha" after it is pasted on the text box ".

At that time, it was not easy. In memory, you only need to judge whether ctrl + v is pressed in the KeyDown event of the text box and then perform corresponding processing, because at that time there was already a paste menu, pressing ctrl + v and clicking paste menu actually do the same thing.

The simplified interface is as follows:

The KeyDown event of the text box and the method for obtaining strings

 

1 private void textBox1_KeyDown (object sender, KeyEventArgs e) 2 {3 if (e. control & e. keyCode = Keys. v) // press ctrl + v 4 {5 textBox1.Text = GetText (); // to simplify the operation, only one ha 6} 7} 8 private string GetText () is returned () 9 {10 string s = "ha"; 11 // a series of processing operations are omitted, and the processed strings should be returned at last... 12 return s; 13}

 

Then run the command. The result should be as expected. After pressing ctrl + v on the text box, there should be only one "Haha" word in the text box, but the actual result is unexpected, I copied a text "c" and press ctrl + v.

At that time, I was shocked. Under what circumstances, "c" was also pasted in. Later I thought that there was a Handled attribute in the event parameter, indicating whether the event was Handled or not, and I may have forgotten to set it, then modify the Code as follows:

 

1 private void textBox1_KeyDown (object sender, KeyEventArgs e) 2 {3 if (e. control & e. keyCode = Keys. v) // whether to press ctrl + v4 {5 textBox1.Text = GetText (); // to simplify the operation, only one ha 6 e is returned. handled = true; 7} 8}

Then run the command again. The result is the same. If you do not believe it, try it yourself. The Handled property does not seem to work in the press event.

Then, output the text box content under the KeyDown, KeyPress, and KeyUp events to see when the system adds the copied content to the text box and test the code.

 

1 private void textBox1_KeyDown (object sender, KeyEventArgs e) 2 {3 if (e. control & e. keyCode = Keys. v) // press ctrl + v 4 {5 textBox1.Text = GetText (); // to simplify the operation, only one ha 6 e is returned. handled = true; 7 Console. writeLine ("KeyDown:" + textBox1.Text); 8} 9} 10 11 private void textBox1_KeyPress (object sender, KeyPressEventArgs e) 12 {13 Console. writeLine ("KeyPress:" + textBox1.Text); 14} 15 16 private void textBox1_KeyUp (object sender, KeyEventArgs e) 17 {18 Console. writeLine ("KeyUp:" + textBox1.Text); 19}

 

Run. Copy the text "c" and press ctrl + v on the text box. The VS output is as follows:

The output window shows that the copied text "c" is inserted into the text box before the KeyUp event after the KeyPress event.

This is really unpleasant. I cannot control what the system did after the KeyPress event and before the KeyUp event. But the problem still has to be solved.

Solution 1:

Now that the system has pasted the copied content into the text box and emptied the clipboard content, I can see how you can put it.

 

1 private void textBox1_KeyDown (object sender, KeyEventArgs e) 2 {3 if (e. control & e. keyCode = Keys. v) // whether to press ctrl + v4 {5 textBox1.Text = GetText (); // to simplify the operation, only one ha 6 Clipboard is returned. clear (); 7} 8}

Although this method is easy to achieve the effect, it is simple and rude, and the content in the clipboard must be used in real applications. If so, then the user will find that after pasting in the program, and then paste it to other places, there will be no content, so I should explain various reasons to them, I am too lazy, this method is not required.

 

Solution 2:

Since the value of the text box has been set in the cut version before KeyUp, can I set my value to the text box under the KeyUp event?

The answer is yes. Place the code under the KeyDown event to the KeyUp event.

 

1 private void textBox1_KeyUp (object sender, KeyEventArgs e) 2 {3 if (e. control & e. keyCode = Keys. v) // whether to press ctrl + v4 {5 textBox1.Text = GetText (); // to simplify the operation, only one ha 6} 7 is returned}

After testing, although this is a big solution, there is a drawback, that is, when you want to display the processed value in the text box during the operation, you have to be skillful in the operation, press ctrl + v before you press V, in this way, the text box will display "ha". Otherwise, only the text you copied will be displayed, and the text you processed will not be displayed, in addition, the copied text + processed text (similar to "c Ha") is displayed before the processed value is displayed in the text box, it clearly shows the process of modifying the value of a text box.


However, this skillful operation reminds me of the joke that the classic programmer bet with God. It seems that this is not a good method.

Solution 3:

I found an article on the internet saying that Using richTextBox to use the Handled attribute in the KeyDown event is acceptable, so I put the original code under the KeyDown event and tried to test the result.

 

1         private void richTextBox1_KeyDown(object sender, KeyEventArgs e)2         {3             if (e.Control && e.KeyCode == Keys.V)4             {5                 richTextBox1.Text = GetText();6                 e.Handled = true;7             }8         }

 


However, for me to change the control, the "price" is still a little too large to change...

 

Solution 4:

This is also the final method. First, define a bool variable to indicate whether to press the ctrl + v shortcut key and set it to true in the KeyDown event, in the KeyPress event, determine whether to set the value of the text box and reset the variable to false, so that you do not have to consider technical operations, it is not obvious that the text box first displays the pasted text and then the set value.

1 private bool isCtrlv = false; // whether to press ctrl + v 2 private void textBox1_KeyDown (object sender, KeyEventArgs e) 3 {4 if (e. control & e. keyCode = Keys. v) // press ctrl + v 5 {6 isCtrlv = true; 7} 8} 9 10 private void textBox1_KeyPress (object sender, KeyPressEventArgs e) 11 {12 if (isCtrlv) // If you press ctrl + v13 {14 textBox1.Text = GetText (); 15 e. handled = true; 16} 17 isCtrlv = false;

 

I did not expect a simple ctrl + v operation to be so tortuous that it should be smooth sailing and follow the results I imagined.

If you haven't done a good test before, it's a pitfall. If you have any better solutions, please share with us ....

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.