Getting the Clipboard paste change event in VB.NET

caeanis

Freshman
Joined
Sep 6, 2006
Messages
40
I have to do some basic data entry in a web app I don't have administrative access to. I am pulling the data to be entered from my own vb form app. What I'd like to do is monitor the paste event (if there is such) of the clipboard to automatically load the next piece of data into the clipboard so that I can right click and paste with out having go back to my form each time. Is this possible? I've seen a view examples in C# on how to monitor the clipboard change event, but am not familiar with C#. Thanks in advance.
 
Ok I found some code that allows me to access the win32 clipboard and functions however it doesn't appear to be working. I added the code below to the form1.vb module. Does this appear to be correct?
<code>
#Region " Definitions "
'Constants for API Calls...
Private Const WM_DRAWCLIPBOARD As Integer = &H308
Private Const WM_CHANGECBCHAIN As Integer = &H30D
Private Const WM_PASTE As Integer = &H302
'Handle for next clipboard viewer...
Private mNextClipBoardViewerHWnd As IntPtr

'API declarations...
Declare Auto Function SetClipboardViewer Lib "user32" (ByVal HWnd As IntPtr) As IntPtr
Declare Auto Function ChangeClipboardChain Lib "user32" (ByVal HWnd As IntPtr, ByVal HWndNext As IntPtr) As Boolean
Declare Auto Function SendMessage Lib "User32" (ByVal HWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Long
#End Region

#Region " Contructor "
Public Sub New()
'To register this form as a clipboard viewer...
mNextClipBoardViewerHWnd = SetClipboardViewer(Me.Handle)
InitializeComponent()
End Sub
#End Region

#Region " Message Process "
'Override WndProc to get messages...
Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case Is = WM_DRAWCLIPBOARD 'The clipboard has changed...
'##########################################################################
' Process Clipboard Here :)........................
'##########################################################################
SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam, m.LParam)

Case Is = WM_CHANGECBCHAIN 'Another clipboard viewer has removed itself...
If m.WParam = CType(mNextClipBoardViewerHWnd, IntPtr) Then
mNextClipBoardViewerHWnd = m.LParam
Else
SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam, m.LParam)
End If
Case Is = WM_PASTE ' The clipboard contents were pasted...
Select Case intCtrl
Case 0
Clipboard.SetText(txtStrNum.Text)
intCtrl = 1
Case 1
cmdParse.PerformClick()
intCtrl = 2
Case 2
If isOptyTeam = False Then
isOptyTeam = True
Else
Clipboard.SetText(txtOptyName.Text)
intCtrl = 3
End If
Case 3
Clipboard.SetText(txtDue.Text)
intCtrl = 4
Case 4
End Select
End Select

MyBase.WndProc(m)
End Sub
#End Region

#Region " Dispose "
#End Region
</code>
 
Ok I see what I did wrong, WM_PASTE is a message sent from the app to the window or control I'm pasting into. It is NOT a notification which is why it doesn't trigger as a notification. So, it appears that it isn't possible, at least not the way I'm trying to do it.
 
Very interesting! GetClipboardSequenceNumber... Sound promising. I figured out how to capture the paste event from within the textbox control, by overriding winproc, but now the custom text control doesn't appear on my form. Sigh. :(
 
Back
Top