none
Copy text and show in the vb application RRS feed

  • Question

  • I am trying to find solution, how need copy text and these text  go to my application textBox area. I am trying to explain. If I select text e.g In the nopated program  and make copy "ctrl+C" then this select text show  to my application textBox area.  Picture below. But I don't know how need to do it. Maybe someone can get me on the road, how to do it? The application write languages C#

    Wednesday, August 1, 2018 1:07 PM

All replies

  • Hi

    Difficult to understand your question.

    Here is some code - this code, while running, will show the ClipBoard current Text. So, when you Ctrl-C (or any text copy - ie context menu Copy etc) from somewhere, then it will show up in the TextBox of this application.

    ' Form1 with TextBox1
    ' and Timer1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Clipboard.Clear()
        Timer1.Interval = 100
        Timer1.Enabled = True
      End Sub
      Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        TextBox1.Text = Clipboard.GetText
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Proposed as answer by Alex Li-MSFT Thursday, August 2, 2018 2:20 AM
    Wednesday, August 1, 2018 1:22 PM
  • If you want to monitor Clipboard, you must use APIs (P/Invoke) like AddClipboardFormatListener
    • Edited by Castorix31 Wednesday, August 1, 2018 1:32 PM
    Wednesday, August 1, 2018 1:31 PM
  • Hello,

    Add the following class to your project.

    Imports System.Runtime.InteropServices
    
    Public NotInheritable Class ClipboardNotification
        ''' <summary>
        ''' Occurs when the contents of the clipboard is updated.
        ''' </summary>
        Public Shared Event ClipboardUpdate As EventHandler
        Private Shared _form As New NotificationForm()
    
        ''' <summary>
        ''' Raises the <see cref="ClipboardUpdate"/> event.
        ''' </summary>
        ''' <param name="e">Event arguments for the event.</param>
        Private Shared Sub OnClipboardUpdate(ByVal e As EventArgs)
            Dim handler = ClipboardUpdateEvent
            If handler IsNot Nothing Then
                handler(Nothing, e)
            End If
        End Sub
        ''' <summary>
        ''' Hidden form to recieve the WM_CLIPBOARDUPDATE message.
        ''' </summary>
        Private Class NotificationForm
            Inherits Form
    
            Public Sub New()
                NativeMethods.SetParent(Handle, NativeMethods.HWND_MESSAGE)
                NativeMethods.AddClipboardFormatListener(Handle)
            End Sub
    
            Protected Overrides Sub WndProc(ByRef m As Message)
                If m.Msg = NativeMethods.WM_CLIPBOARDUPDATE Then
                    OnClipboardUpdate(Nothing)
                End If
                MyBase.WndProc(m)
            End Sub
        End Class
    End Class
    
    Friend NotInheritable Class NativeMethods
    
        Private Sub New()
        End Sub
    
        ' See http://msdn.microsoft.com/en-us/library/ms649021%28v=vs.85%29.aspx
        Public Const WM_CLIPBOARDUPDATE As Integer = &H31D
        Public Shared HWND_MESSAGE As New IntPtr(-3)
    
        ' See http://msdn.microsoft.com/en-us/library/ms632599%28VS.85%29.aspx#message_only
        <DllImport("user32.dll", SetLastError:=True)>
        Public Shared Function AddClipboardFormatListener(ByVal hwnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function
    
        ' See http://msdn.microsoft.com/en-us/library/ms633541%28v=vs.85%29.aspx
        ' See http://msdn.microsoft.com/en-us/library/ms649033%28VS.85%29.aspx
        <DllImport("user32.dll", SetLastError:=True)>
        Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
        End Function
    End Class
    

    In your form (either the load or shown event) setup the code as shown below.

    Public Class Form1
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            AddHandler ClipboardNotification.ClipboardUpdate,
                AddressOf ClipboardNotification_ClipboardUpdate
        End Sub
    
        Private Sub ClipboardNotification_ClipboardUpdate(sender As Object, e As EventArgs)
            If Clipboard.ContainsText(TextDataFormat.Text) Then
                TextBox1.Text = Clipboard.GetText()
            End If
        End Sub
    End Class
    
    Run your program, copy some text and it in this case would appear in TextBox1.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, August 1, 2018 1:45 PM
    Moderator
  • Thank, You suggest very help for me. I wrote a shorter code than you wrote. My code:

         private void timer1_Tick(object sender, EventArgs e){   

            textBox1.Text = Clipboard.GetText(); }


    Thursday, August 2, 2018 10:07 AM
  • Thank, You suggest very help for me. I wrote a shorter code than you wrote. My code:

         private void timer1_Tick(object sender, EventArgs e){   

            textBox1.Text = Clipboard.GetText(); }


    You really should avoid using a Timer altogether, what I suggested and Castorix31 suggested is a much cleaner way to go.

    In generally timers take up resources in your application that are generally unnecessary and a nuscane when debugging code.  


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, August 2, 2018 10:16 AM
    Moderator