How to automatically copy clipboard text into textbox using timer

Wilfred Eghenedji 326 Reputation points
2023-10-01T14:44:33.0233333+00:00

Using VB.Net, am trying to get the text in clipboard into a specific textbox. Once text get into clipboard, I want it to be getting pasted and queuing automatically into the textbox, using timer. The below code was supposed to do as expected but only works fine if the clipboard text is a line of text. If the text is more than a line of text, the same text pastes continuously instead of just once into the textbox. See code.... Thank you.

Private Sub tmrClipboardHistory_Tick(sender As Object, e As EventArgs) Handles tmrClipboardHistory.Tick          

If My.Computer.Clipboard.ContainsText Then              
  If textbox1.Text.Contains(My.Computer.Clipboard.GetText) Then                 
   'do nothing             
  Else                 
   textbox1.Text += My.Computer.Clipboard.GetText()
   textbox1.Text += ControlChars.NewLine & ControlChars.NewLine             
  End If          
End If

End Sub    
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,867 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,726 questions
0 comments No comments
{count} votes

Accepted answer
  1. KOZ6.0 6,475 Reputation points
    2023-10-01T15:37:06.65+00:00

    You can use SetClipboardViewer to know about clipboard changes. You will receive a WM_DRAWCLIPBOARD message when the content changes.

    Imports System.Runtime.InteropServices
    
    
    Public Class Form1
    
        Private hwndNextChain As IntPtr
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Protected Overrides Sub OnHandleCreated(e As EventArgs)
            MyBase.OnHandleCreated(e)
            hwndNextChain = SetClipboardViewer(Me.Handle)
        End Sub
    
        Protected Overrides Sub OnHandleDestroyed(e As EventArgs)
            MyBase.OnHandleDestroyed(e)
            ChangeClipboardChain(Me.Handle, hwndNextChain)
        End Sub
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            Select Case m.Msg
                Case WM_DRAWCLIPBOARD
                    If hwndNextChain <> IntPtr.Zero Then
                        SendMessage(hwndNextChain, m.Msg, m.WParam, m.LParam)
                    End If
                    If Clipboard.ContainsText() Then
                        Dim clipboardText As String = Clipboard.GetText()
                        TextBox1.AppendText(clipboardText & vbCrLf)
                        Debug.WriteLine($"{Now} Change Clipboard")
                    End If
    
                Case WM_CHANGECBCHAIN
                    If m.WParam = hwndNextChain Then
                        hwndNextChain = m.LParam
                    ElseIf hwndNextChain <> IntPtr.Zero Then
                        SendMessage(hwndNextChain, m.Msg, m.WParam, m.LParam)
                    End If
            End Select
            MyBase.WndProc(m)
        End Sub
    
    
        <DllImport("User32.Dll")>
        Private Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
        End Function
    
        <DllImport("user32.dll", SetLastError:=True)>
        Private Shared Function SetClipboardViewer(hWndNewViewer As IntPtr) As IntPtr
        End Function
    
        <DllImport("user32.dll", SetLastError:=True)>
        Private Shared Function ChangeClipboardChain(hWndRemove As IntPtr, hWndNewNext As IntPtr) As Boolean
        End Function
    
        Private Const WM_DRAWCLIPBOARD As Integer = &H308
        Private Const WM_CHANGECBCHAIN As Integer = &HD
    
    End Class
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-10-01T15:15:14.1366667+00:00

    Hi

    Using the code below works for me.

    		With Timer1
    			.Interval = 1000 ' ever 1 second
    			.Enabled = True
    		End With
    		With TextBox1
    			.Multiline = True
    			.Font = New Font(.Font.FontFamily, 22)
    			.ScrollBars = ScrollBars.Both
    		End With
    ' ------------------------------------------
    	Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
     		If Not TextBox1.Text = Clipboard.GetText Then TextBox1.Text = Clipboard.GetText
     	End Sub 
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.