Is there some way I can detect or cause an event to fire whenever text is pasted into a RichTextBox? Or maybe there is already some kind of event that fires when text is pasted? This is Winforms C#, by the way.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Icemanminds answer does not work properly, this does show you when you have pasted but has side effects.
You will also enter the if block in text changed if you do this, with an empty textbox 1. paste atleast two lines of text 2. go up to the top line with the arrow keys, hit space then backspace 3. with the arrow keys comes back to the bottom line and hit enter You are now informed of a paste when it did not happen.
Edit: Here is a fix for the problem
Code:
I'm not a C# expert by any means, in fact it's been 3 years since I touched the language! However, you might want to use the ProcessCmdKey method to listen for
CTRL + V
.http://msdn.microsoft.com/en-us/library/system.windows.forms.control.processcmdkey(v=vs.71).aspx
Here is another similar approach to detect that a cut or paste has occurred that changes the number of characters in the box (It does not detect if the same number of characters were pasted as were highlighted): First create a class level member to hold the current length
Now mark the length when the user enters the box:
Then use the TextChanged event:
Because not all people may use Ctrl+V and because there are other ways to get text into a text box (such as drag and drop), I went a different route, which I will share here in case anyone else is looking for a solution.
What I did was create a field in my class:
and in the TextChanged() event I added the following:
I went under the assumption that if more then 2 characters were entered into the text box at a time, then text must have been pasted, because how else can someone input more then 2 characters at a time? So anyway, this worked for me. Thanks to everyone who tried to help.