Search code examples
c#clipboardpastewndproc

Detecting paste from clipboard in ToolStripTextBox (C#)


I need to detect if a user pastes text from the clipboard into a ToolStripTextBox. Therefore I've created a custom control which inherits from ToolStripTextBox. Then I wanted to override WndProc to catch WM_PASTE message, but I noticed that there is no WndProc to override. For the standard TextBox the described method works fine.

Any suggestion what to do to detect paste from clipboard on ToolStripTextBox?


Solution

  • WndProc in ToolStripTextBox seems to be out of reach. I searched a bit out of curiosity and came across that answer - https://stackoverflow.com/a/4688745/168719

    If using ToolStripControlHost is not an option, there are still other clever solutions for scenarioes requiring custom handling of WndProc:

    http://bytes.com/topic/c-sharp/answers/279168-toolstriptextbox-right-click

    Nicholas Paldino [.NET/C# MVP]

    I just noticed that. In order to get around this, you can get the hosted TextBox by calling the TextBox property. Then, you should be able to create a class derived from NativeWindow which overrides the WndProc method to ignore the context menu message [or to intercept WM_PASTE, obviously...] When you get the textbox property, get the handle, and assign the handle to your overridden NativeWindow class.