Detecting paste from clipboard in ToolStripTextBox (C#)
Question
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.
OTHER TIPS
If you are in Windows[Windows forms / WPF] , you can use Clipboard to detect the data.
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
{
richTextBox1.Paste();
MessageBox.Show("You have data in clipboard")
}
(Edited to include WPF)
ToolStripTextBox is a host control containing a standard text box, you would need to do as you have described but replace the ToolStripTextBox.TextBox with your textbox, unfortunately it's a read only property.
So you'll need to derive a MyToolStripTextBox from ToolStripControlHost to be able to change the type of control it hosts.