G
Guest
In my program I cannot allow anything but plain text to be pasted into my inherited RichTextBox control. (The user can use my subset of RichText tools - if I let them paste anything they could paste in tables and all sorts of things, which would not work for me.
It was simple to create a pop-up menu that had Paste Unformatted Text as its only paste option, but the harder thing to control is Ctrl-V pasting. I went googling looking for the answer, and scraped together my own solution below (VB.Net code.) So, this is an example for anyone who has a similar problem (pasting formatted text as plain text in the Ctrl-V.) Also, please reply if you have any suggestions for improvements, or if I'm not using the GetDataObject calls and AutoConvert options correctly.
Thanks
Kevi
'This function traps the Ctrl-V paste, changing the clipboard contents to plain text
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolea
Const WM_KEYDOWN As Integer = &H10
Const WM_SYSKEYDOWN As Integer = &H10
If msg.Msg = WM_KEYDOWN Or msg.Msg = WM_SYSKEYDOWN The
Select Case keyDat
Case Keys.Control Or Keys.
If Clipboard.GetDataObject().GetDataPresent(DataFormats.Text, True) The
Clipboard.SetDataObject(Clipboard.GetDataObject.GetData(DataFormats.Text, True)
End I
End Selec
End I
Return MyBase.ProcessCmdKey(msg, keyData
End Function
It was simple to create a pop-up menu that had Paste Unformatted Text as its only paste option, but the harder thing to control is Ctrl-V pasting. I went googling looking for the answer, and scraped together my own solution below (VB.Net code.) So, this is an example for anyone who has a similar problem (pasting formatted text as plain text in the Ctrl-V.) Also, please reply if you have any suggestions for improvements, or if I'm not using the GetDataObject calls and AutoConvert options correctly.
Thanks
Kevi
'This function traps the Ctrl-V paste, changing the clipboard contents to plain text
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolea
Const WM_KEYDOWN As Integer = &H10
Const WM_SYSKEYDOWN As Integer = &H10
If msg.Msg = WM_KEYDOWN Or msg.Msg = WM_SYSKEYDOWN The
Select Case keyDat
Case Keys.Control Or Keys.
If Clipboard.GetDataObject().GetDataPresent(DataFormats.Text, True) The
Clipboard.SetDataObject(Clipboard.GetDataObject.GetData(DataFormats.Text, True)
End I
End Selec
End I
Return MyBase.ProcessCmdKey(msg, keyData
End Function