Detecting a paste into a RichTextBox

2019-08-05 10:12发布

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.

4条回答
祖国的老花朵
2楼-- · 2019-08-05 10:18

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:

    protected override void OnSelectionChanged(EventArgs e)
    {
        base.OnSelectionChanged(e);
        int cavetOffset = SelectionStart - m_nLastCavetPos;
        int sizeOffset = Text.Length - m_nLastKnownSize;
        if (sizeOffset > 0)
        {
            if (sizeOffset == 1)
                Console.WriteLine("Typed \"" + (Text.Substring(SelectionStart - cavetOffset, sizeOffset)) + "\" At Position " + (SelectionStart - cavetOffset));
            else if (sizeOffset > 1)
                Console.WriteLine("Pasted \"" + (Text.Substring(SelectionStart - cavetOffset, sizeOffset)) + "\" At Position " + (SelectionStart - cavetOffset));
        }
        else if (sizeOffset == 0)
        {
            Console.WriteLine("Moved Caret to " + SelectionStart + " From " + m_nLastCavetPos);
        }
        else
        {
            if (sizeOffset == -1)
                Console.WriteLine("Backspaced at " + (SelectionStart - cavetOffset));
            if (sizeOffset < -1)
                Console.WriteLine("HiliteDelete at " + ((SelectionStart - cavetOffset)+-sizeOffset) + " deleted " + (-sizeOffset) + " characters");
        }
        m_nLastKnownSize = Text.Length;
        m_nLastCavetPos = SelectionStart;
    }
查看更多
再贱就再见
3楼-- · 2019-08-05 10:21

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

查看更多
乱世女痞
4楼-- · 2019-08-05 10:21

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

private int _LastTextLength = 0; 

Now mark the length when the user enters the box:

private void txtNoteDetails_Enter(object sender, EventArgs e)
{
    _LastTextLength = txtNoteDetails.Text.Length;
}

Then use the TextChanged event:

private void txtNoteDetails_TextChanged(object sender, EventArgs e)
{
    if (Math.Abs(txtNoteDetails.Text.Length - _LastTextLength) > 2)
    {
        //Do your thing
    }
}
查看更多
Deceive 欺骗
5楼-- · 2019-08-05 10:32

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:

int _lastPosition = 0;

and in the TextChanged() event I added the following:

if (SelectionStart - _lastPosition > 2)
{
    // Text was pasted into text box
}
_lastPosition = SelectionStart;

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.

查看更多
登录 后发表回答