将大文本数据复制到文本框中时检测输入线

时间:2013-06-09 09:12:25

标签: c# winforms copy-paste enter

我想复制任何大文本数据 来源(例如:其他app,word,记事本......) 并粘贴到我的app.now中的textBox我必须从复制的文本中检测enterLine。

我搜索了很多,但无法解决任何问题。

tanx很多

1 个答案:

答案 0 :(得分:1)

您可以通过专门为您的要求创建自定义文本框,在WinForms TextBox控件的默认“粘贴”事件上创建钩子,如下所示。

using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class MyTextBox : TextBox 
    {
        protected override void WndProc(ref Message m)
        {
            // Trap WM_PASTE:
            if (m.Msg == 0x302 && Clipboard.ContainsText())
            {
                var pastText = Clipboard.GetText().Replace('\n', ' ');
                if (pastText.Length > MaxLength)
                {
                    //Do Something 
                }
                else
                {
                    //Do Something 
                }
                this.SelectedText = pastText;
                return;
            }
            base.WndProc(ref m);
        }
    }
}