hook on default “Paste” event of WinForms TextBox control

While I would normally not suggest dropping to low level Windows API, and this may not be the only way of doing this, it does do the trick:

using System;
using System.Windows.Forms;

public class ClipboardEventArgs : EventArgs
{
    public string ClipboardText { get; set; }
    public ClipboardEventArgs(string clipboardText)
    {
        ClipboardText = clipboardText;
    }
}

class MyTextBox : TextBox
{
    public event EventHandler<ClipboardEventArgs> Pasted;

    private const int WM_PASTE = 0x0302;
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_PASTE)
        {
            var evt = Pasted;
            if (evt != null)
            {
                evt(this, new ClipboardEventArgs(Clipboard.GetText()));
                // don't let the base control handle the event again
                return;
            }
        }

        base.WndProc(ref m);
    }
}

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        var tb = new MyTextBox();
        tb.Pasted += (sender, args) => MessageBox.Show("Pasted: " + args.ClipboardText);

        var form = new Form();
        form.Controls.Add(tb);

        Application.Run(form);
    }
}

Ultimately the WinForms toolkit is not very good. It is a thin-ish wrapper around Win32 and the Common Controls. It exposes the 80% of the API that is most useful. The other 20% is often missing or not exposed in a way that is obvious. I would suggest moving away from WinForms and to WPF if possible as WPF seems to be a better architected framework for .NET GUIs.

Leave a Comment