Trapping Ctrl-V Paste into RichTextBox and stripping the formatting - critiques please

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
 
C

Claes Bergefall

That's the same way I did it
A few notes though:

1. The "If msg.Msg = WM_KEYDOWN or..." statement shouldn't
really be necessary. AFAIK it will always evaluate to true.

2. There is another key combination to paste things: Shift+Insert
You probably want to handle that aswell.

3. ClipBoard.GetDataObject returns Nothing if there is no data
on the clipboard. You need to check for that.

/claes


Kevin said:
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,
Kevin

'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
Boolean
Const WM_KEYDOWN As Integer = &H100
Const WM_SYSKEYDOWN As Integer = &H104

If msg.Msg = WM_KEYDOWN Or msg.Msg = WM_SYSKEYDOWN Then
Select Case keyData
Case Keys.Control Or Keys.V
If
Clipboard.GetDataObject().GetDataPresent(DataFormats.Text, True) Then
 
G

Guest

Perfect! Thank you for the tips, Claes! I definitely would not have thought of those until a couple months from now when my program would have crashed on the end user..

So here's the new code (may not be the most elegant, but it works.

'If the user control is set to Remove Formatting on Paste = True
' this function traps the Ctrl-V and Shift-Ins pastes
' 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
Dim bRemoveClipboardFormatting As Boolean = Fals

If mbRemoveFormattingOnPaste The
If msg.Msg = WM_KEYDOWN Or msg.Msg = WM_SYSKEYDOWN The
Select Case keyDat
Case Keys.Control Or Keys.
bRemoveClipboardFormatting = Tru
Case Keys.Shift Or Keys.Inser
bRemoveClipboardFormatting = Tru
End Selec
End I
End I

If bRemoveClipboardFormatting The
If Not Clipboard.GetDataObject Is Nothing The
If Clipboard.GetDataObject().GetDataPresent(DataFormats.Text, True) The
Clipboard.SetDataObject(Clipboard.GetDataObject.GetData(DataFormats.Text, True)
End I
End I
End I

Return MyBase.ProcessCmdKey(msg, keyData
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top