检测粘贴到RichTextBox中 [英] Detecting a paste into a RichTextBox

查看:102
本文介绍了检测粘贴到RichTextBox中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以将文本粘贴到RichTextBox中时检测到或导致事件触发?或者,也许已经有某种事件在粘贴文本时触发?顺便说一下,这就是Winforms C#。

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.

推荐答案

因为并非所有人都可以使用Ctrl + V,而且还有其他方法可以将文本放入文本框(例如拖放),我走了一条不同的路线,如果有人正在寻找解决方案,我将在此处分享。

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;

,在TextChanged()事件中,我添加了以下内容:

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.

这篇关于检测粘贴到RichTextBox中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆