please help with clipboard viewer

  • Thread starter Thread starter nick
  • Start date Start date
N

nick

hi
I'm tryin to write a program that keeps track of the contents of the
clipboard to a textbox, but i keep getting "An unhandled exception of
type 'System.NullReferenceException' occurred in
WindowsApplication4.exe" error. here is the sample code

Public Class Form1
Inherits System.Windows.Forms.Form
Public Declare Function SetClipboardViewer Lib "user32" Alias
"SetClipboardViewer" (ByVal hwnd As Long) As Integer
Public Declare Function ChangeClipboardChain Lib "user32" Alias
"ChangeClipboardChain" (ByVal hwnd As Integer, ByVal hWndNext As
Integer) As Integer
Dim hWndClipBoard = SetClipboardViewer(Me.Handle.ToInt32)
Dim strPastedText As String

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
ChangeClipboardChain(Me.Handle.ToInt32, hWndClipBoard)
End Sub
Protected Overrides Sub WndProc(ByRef m As
System.Windows.Forms.Message)
Const WM_DRAWCLIPBOARD = &H308
Dim data As IDataObject = Clipboard.GetDataObject()
If m.Msg = WM_DRAWCLIPBOARD Then
MessageBox.Show("Here")
If data.GetDataPresent(GetType(String)) Then
strPastedText =
DirectCast(data.GetData(GetType(String)), String)
TextBox1.Text = strPastedText
End If
End If
MyBase.WndProc(m)
End Sub
End Class
 
Hi Nick,
I think that when you are changing the folowing lines it wil work like a
clipboard viewer (when there is a textbox of course).
(I did not look for the solution, just changed the errors)
To overcome this kind of problems, the best you can do is to set "Option
Strict On" in the header of your progam,
Option Strict on
Public Class Form1
Inherits System.Windows.Forms.Form
Public Declare Function SetClipboardViewer Lib "user32" Alias
"SetClipboardViewer" (ByVal hwnd As Long) As Integer
Public Declare Function ChangeClipboardChain Lib "user32" Alias
"ChangeClipboardChain" (ByVal hwnd As Integer, ByVal hWndNext As
Integer) As Integer
Dim hWndClipBoard as Integer
Remove Dim strPastedText As String
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
ChangeClipboardChain(Me.Handle.ToInt32, hWndClipBoard)
End Sub
Protected Overrides Sub WndProc(ByRef m As
System.Windows.Forms.Message)
Dim strPastedText As String
hWndClipBoard = SetClipboardViewer(Me.Handle.ToInt32)
Const WM_DRAWCLIPBOARD as Integer = &H308
Dim data As IDataObject = Clipboard.GetDataObject()
If m.Msg = WM_DRAWCLIPBOARD Then
Remove MessageBox.Show("Here")
If data.GetDataPresent(GetType(String)) Then
strPastedText =
DirectCast(data.GetData(GetType(String)), String)
TextBox1.Text = strPastedText
End If
End If
MyBase.WndProc(m)
End Sub
End Class

I hope this helps,
Cor
 
Cor said:
Hi Nick,
I think that when you are changing the folowing lines it wil work like a
clipboard viewer (when there is a textbox of course).
(I did not look for the solution, just changed the errors)
To overcome this kind of problems, the best you can do is to set "Option
Strict On" in the header of your progam,
Option Strict on
I hope this helps,
Cor
Hi Cor. now it wont even run, i get "Option Strict On requires all
variable declarations to have an 'As' clause." error, thanks for the
help, if you or anyone else got any other suggestions please let me
know
 
Nick,
You have to make all the changes I did make in the text.
The lines with a change have no > before them.
Option strict on shows only the errors..
For me it did work.
Cor
 
Hi,

I have a Clipboard Viewer example on my site. It uses SetClipboardViewer and
is written in C#.

See http://www.radsoftware.com.au/web/CodeZone/

Ross Donald
Rad Software

| Hi Nick,
| I think that when you are changing the folowing lines it wil work like a
| clipboard viewer (when there is a textbox of course).
| (I did not look for the solution, just changed the errors)
| To overcome this kind of problems, the best you can do is to set "Option
| Strict On" in the header of your progam,
| Option Strict on
| > Public Class Form1
| > Inherits System.Windows.Forms.Form
| > Public Declare Function SetClipboardViewer Lib "user32" Alias
| > "SetClipboardViewer" (ByVal hwnd As Long) As Integer
| > Public Declare Function ChangeClipboardChain Lib "user32" Alias
| > "ChangeClipboardChain" (ByVal hwnd As Integer, ByVal hWndNext As
| > Integer) As Integer
| Dim hWndClipBoard as Integer
| Remove Dim strPastedText As String
| >
| > Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
| > System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
| > ChangeClipboardChain(Me.Handle.ToInt32, hWndClipBoard)
| > End Sub
| > Protected Overrides Sub WndProc(ByRef m As
| > System.Windows.Forms.Message)
| Dim strPastedText As String
| hWndClipBoard = SetClipboardViewer(Me.Handle.ToInt32)
| Const WM_DRAWCLIPBOARD as Integer = &H308
| > Dim data As IDataObject = Clipboard.GetDataObject()
| > If m.Msg = WM_DRAWCLIPBOARD Then
| Remove MessageBox.Show("Here")
| > If data.GetDataPresent(GetType(String)) Then
| > strPastedText =
| > DirectCast(data.GetData(GetType(String)), String)
| > TextBox1.Text = strPastedText
| > End If
| > End If
| > MyBase.WndProc(m)
| > End Sub
| > End Class
|
| I hope this helps,
| Cor
|
|
 
Back
Top