By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,579 Members | 1,765 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,579 IT Pros & Developers. It's quick & easy.

Check Clipboard via Timer

P: n/a
Appreciate some help. Want to continuously check clipboard and if text
present unhide and display in a text. The following code can find nothing
in the clipboard even though I paste information there from Notepad.
\\
Public Sub btnStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnStart.Click
Dim t As New System.Timers.Timer(2000)
AddHandler t.Elapsed, AddressOf TimerFired
t.Enabled = True
End Sub
Public Sub TimerFired(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs)
Dim d As IDataObject = Clipboard.GetDataObject()
If (Clipboard.GetDataObject.GetDataPresent(DataFormat s.Text)) Then
TextBox1.Text =
Clipboard.GetDataObject.GetData(DataFormats.Text). ToString()
End If
End Sub
//
If I debug the TimerFired sub is repeatedly called but there is nothing in
the Clipboard.

Ed

Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi,

You have to use the setclipboardviewer api to register yourself as a
clipboard viewer. The api will return the hwnd to the old viewer save that
value in a form level variable. You will recieve WM_DRAWCLIPBOARD message
when the clipboard changes override wndproc to recieve message. Make sure
you call mybase.wndproc in the override or you app will not work. When
closing program set the clipboard viewer back to the old one.

Api declares

Declare Function SetClipboardViewer Lib "user32" Alias "SetClipboardViewer"
(ByVal hwnd As Integer) As Integer

Declare Function ChangeClipboardChain Lib "user32" Alias
"ChangeClipboardChain" (ByVal hwnd As Integer, ByVal hWndNext As Integer) As
Integer

In form load or where ever want to start monitoring clipboard

hWndClipBoard = SetClipboardViewer(Me.Handle.ToInt32)

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

If m.Msg = WM_DRAWCLIPBOARD Then

MessageBox.Show("Here")

End If

MyBase.WndProc(m)

End Sub

Ken
------------------
"Ed Bitzer" <ed******@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Appreciate some help. Want to continuously check clipboard and if text
present unhide and display in a text. The following code can find nothing
in the clipboard even though I paste information there from Notepad.
\\
Public Sub btnStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnStart.Click
Dim t As New System.Timers.Timer(2000)
AddHandler t.Elapsed, AddressOf TimerFired
t.Enabled = True
End Sub
Public Sub TimerFired(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs)
Dim d As IDataObject = Clipboard.GetDataObject()
If (Clipboard.GetDataObject.GetDataPresent(DataFormat s.Text)) Then
TextBox1.Text =
Clipboard.GetDataObject.GetData(DataFormats.Text). ToString()
End If
End Sub
//
If I debug the TimerFired sub is repeatedly called but there is nothing in
the Clipboard.

Ed


Nov 21 '05 #2

P: n/a
Ken, Will start the leaning curve playing with that API. Am very
surprised that's necessary with net because thought that was the purpose of
the Clipboard namespace. The following code works great activated by a
button click - but does not work if activated by the timer..
\\
If Clipboard.GetDataObject.GetDataPresent(DataFormats .Text) Then
TextBox1.Text =
Clipboard.GetDataObject.GetData(DataFormats.Text). ToString()
End If
//

Ed
Nov 21 '05 #3

P: n/a
Ken
In form load or where ever want to start monitoring clipboard hWndClipBoard = SetClipboardViewer(Me.Handle.ToInt32)


hWndClipBoard is not declared when I attempt to test - what am I missing?

Ed
Nov 21 '05 #4

P: n/a
"Ed Bitzer" <ed******@yahoo.com> schrieb:
In form load or where ever want to start monitoring clipboard

hWndClipBoard = SetClipboardViewer(Me.Handle.ToInt32)


hWndClipBoard is not declared when I attempt to test - what am I missing?


\\\
Dim hWndClipBoard As Int32
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #5

P: n/a
Ken,

The API certainly works as advertised and does not require the timer that I
used in a similar VB6 program to pick up a copy - The API picks up the
"change" when something is copied to the clipboard. I am using this in
small utility I use myself to copy bits and pieces of code or other
information, usually gathered from forums, and accumulates is a temporary
file which I can then edit and often import into a askSam database. I think
I wrote my first version in Pascal many moons ago, after than in VB. I'm
retired and trying to keep up with computer technology and writing a bit of
code, now in VB.Net -- keeps me out of the taverns<g>. I do thank you
guys.

Ed

Nov 21 '05 #6

P: n/a
Hi,

This is how I do it. Example is for getting bitmap but should work
for text.

Dim data As IDataObject

data = Clipboard.GetDataObject()

If data.GetDataPresent(GetType(System.Drawing.Bitmap) ) Then

bm = CType(data.GetData(GetType(System.Drawing.Bitmap)) , Image)

End If

Ken

----------------------------

"Ed Bitzer" <ed******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Ken, Will start the leaning curve playing with that API. Am very
surprised that's necessary with net because thought that was the purpose of
the Clipboard namespace. The following code works great activated by a
button click - but does not work if activated by the timer..
\\
If Clipboard.GetDataObject.GetDataPresent(DataFormats .Text) Then
TextBox1.Text =
Clipboard.GetDataObject.GetData(DataFormats.Text). ToString()
End If
//

Ed

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.