-1

I was trying to override the onpaste method of the winform textbox using the following method:

Override Paste Into TextBox

When the paste happens I don't want windows to handle it I want my code to. So I did the following:

        if (m.Msg == WM_PASTE)
        {
            var evt = Pasted;
            if (evt != null)
            {
                evt(this, new ClipboardEventArgs(Clipboard.GetText()));
            }
        }
        else
        {

            base.WndProc(ref m);
        }

Is this a safe way to get win32 code to not handle the paste or is there some case I am not seeing?

Community
  • 1
  • 1
SamFisher83
  • 3,937
  • 9
  • 39
  • 52

1 Answers1

0

You should probably use this pattern to define a Pasted event:

public event ClipboardEventHandler Pasted;

protected virtual void OnPasted(ClipboardEventArgs e)
{
    if (Pasted != null)
        Pasted(this, e);
}

Then you call OnPasted instead of calling the event directly so derived classes can control the functionality.

Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102