Hey guyz
Is this possible to get text automatically in textbox1 whenever any textbox is copied to the clipboard.

I am talking to get it automaticaly.

So if i copies abc then textbox1 value will be abc automatically (Not by clicking any button or form loading)

Recommended Answers

All 5 Replies

Member Avatar for Unhnd_Exception

Use at your own risk.

Search SetClipboardViewer on the msdn website for more information. This was quickly put together. It does monitor the clipboard.

Imports System.Runtime.InteropServices

Public Class Form1
    Private Const WM_DRAWCLIPBOARD As Integer = 776
    Private Const WM_CHANGECBCHAIN As Integer = 781

    Private fpChainedWindowHandle As IntPtr

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)

        If m.Msg = WM_DRAWCLIPBOARD Then
            'if the wm_drawclipboard is recieved the clipboard changed
            'do what you want and send the message to the next window in 
            'the chain
            Label1.Text = My.Computer.Clipboard.GetText
            SendMessage(fpChainedWindowHandle, m.Msg, m.LParam, m.WParam)

        ElseIf m.Msg = WM_CHANGECBCHAIN Then
            'Send to the next window 
            SendMessage(fpChainedWindowHandle, m.Msg, m.LParam, m.WParam)
            fpChainedWindowHandle = m.LParam

        End If

    End Sub

    <DllImport("User32.dll")> _
    Public Shared Function SetClipboardViewer(ByVal hWndNewViewer As IntPtr) As IntPtr
    End Function

    <DllImport("User32.dll")> _
    Public Shared Function SendMessage(ByVal Handle As IntPtr, _
                                 ByVal msg As Integer, _
                                 ByVal wParam As IntPtr, _
                                 ByVal lParam As IntPtr) As IntPtr
    End Function

    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        'add yourself to the clipboard viewer
        fpChainedWindowHandle = SetClipboardViewer(Me.Handle)
    End Sub

End Class

See if this helps.
1 Timer, 1 TextBox

Public Class Form1
   
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        With TextBox1
            '// check if Clipboad has Text and if Not TextBox has the Clipboard Text already.
            If Clipboard.ContainsText AndAlso Not .Text = Clipboard.GetText Then
                .Clear() : .Paste() '// clear for new data and paste to TextBox.
            End If
        End With
    End Sub
End Class

Thank you codeorder for solving the problem.
That worked fine.
Thanks Unhnd_Exption to.
But codeorder code looks much simpler.
Marked as resolved.

wait whaat ? :( ok i wrote a SUUUUPEEERRR loong essay! and copied it and pasted it tm my friends wall. and closed the internet down. then! i comback on and its not there! i get pissd! and now ive already copied n pasted somthing els! how do i get back to the essay! it cant have just...vanished! pleas help! what is all this coding stuff? were do i go? run? command prompt? help!

wait whaat ? :( ok i wrote a SUUUUPEEERRR loong essay! and copied it and pasted it tm my friends wall. and closed the internet down. then! i comback on and its not there! i get pissd! and now ive already copied n pasted somthing els! how do i get back to the essay! it cant have just...vanished! pleas help! what is all this coding stuff? were do i go? run? command prompt? help!

The topic discussed on this thread has nothing to do with your problem and you really should have started a new thread.

To answer your question: This code won't do for you, unless you are writting a program that monitors changes to the clipboard - while the program is running and not any change to the clipboard.
You could check your browsers history if you remember seeing your post in your friend's wall. If that didn't work, then your problem has only 1 real solution, rewrite the essay.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.