locked
Auto copy and paste

    Pregunta

  • I have created a basic database view, but I owuld like the ability to add a button that will copy all selected text, open notepad, and copy the contents of the selected text into it.  Is this possible?

    Thanks in advanced for any help.

    sábado, 05 de agosto de 2006 14:24

Respuestas

  • The theory is to copy the text:

     

    Clipboard.SetText(text)

     

    Then to launch a notepad process:

     

    System.IO.Diagnostics.Process.Start("notepad")

     

    To paste, you will have to setfocus on the notepad window and then sendkeys to paste (CTRL + V)

     

    SendKeys:

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait.aspx

     

     

    This last one (focus window, then sendkeys)  may not be the best way (bad practice).

    sábado, 05 de agosto de 2006 15:39
  • Here is an example.



    imports System.Runtime.InteropServices
    ...
    ... 'Globally set:
     
    <DllImport("user32.dll", SetLastError:=True> private shared function FindWindow(byval lpClassName as String, byval lpWindowName as String) as Intptr
    End Function
     
    <DllImport("user32.dll", SetLastError:=True> private shared function SetForegroundWindow(byval hWnd as IntPtr)as boolean
    End Function
     
    ..
    ..
    'Code you wish to execute:
     
    Clipboard.SetText("text")
    Process.Start("notepad")
    Dim theHandle as IntPtr = FindWindow(Nothing, "Untitled - Notepad")
    If theHandle <> Intptr.Zero Then
       Dim theForegroundWindow as Boolean = SetforegroundWindow(theHandle)
       if theForegroundWindow = True then
          SendKeys.Send("^V")
       end if
    End if

     

     

    remember, this is bad practice (I think so anyway, since you are trying to find the window which has "untitled - notepad" in as the title, and there could be several instances!)

    The code should work but not 100%

     

    does this help at all?

    sábado, 05 de agosto de 2006 16:07

Todas las respuestas

  • The theory is to copy the text:

     

    Clipboard.SetText(text)

     

    Then to launch a notepad process:

     

    System.IO.Diagnostics.Process.Start("notepad")

     

    To paste, you will have to setfocus on the notepad window and then sendkeys to paste (CTRL + V)

     

    SendKeys:

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait.aspx

     

     

    This last one (focus window, then sendkeys)  may not be the best way (bad practice).

    sábado, 05 de agosto de 2006 15:39
  • Here is an example.



    imports System.Runtime.InteropServices
    ...
    ... 'Globally set:
     
    <DllImport("user32.dll", SetLastError:=True> private shared function FindWindow(byval lpClassName as String, byval lpWindowName as String) as Intptr
    End Function
     
    <DllImport("user32.dll", SetLastError:=True> private shared function SetForegroundWindow(byval hWnd as IntPtr)as boolean
    End Function
     
    ..
    ..
    'Code you wish to execute:
     
    Clipboard.SetText("text")
    Process.Start("notepad")
    Dim theHandle as IntPtr = FindWindow(Nothing, "Untitled - Notepad")
    If theHandle <> Intptr.Zero Then
       Dim theForegroundWindow as Boolean = SetforegroundWindow(theHandle)
       if theForegroundWindow = True then
          SendKeys.Send("^V")
       end if
    End if

     

     

    remember, this is bad practice (I think so anyway, since you are trying to find the window which has "untitled - notepad" in as the title, and there could be several instances!)

    The code should work but not 100%

     

    does this help at all?

    sábado, 05 de agosto de 2006 16:07
  • This works for textbox selected text:

    Imports System.IO

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If TextBox1.SelectionLength > 0 Then

    File.WriteAllText("mqqzzxy.txt", TextBox1.SelectedText)

    Dim myProcess As Process

    myProcess = Process.Start("mqqzzxy.txt")

    End If

    End Sub

    End Class

    lunes, 07 de agosto de 2006 4:14
  • I am trying something similar.

     

    I would start in a textbox on a program, then need to open a diffgerent app I made containing a richtextbox. type in text. then be able click a button to select it all, copy to clipboard, then paste it in the original programs textbox. then terminate prgm that i copied the text from.

     

    still can not get it to work.

    I willt ry modify some of this code.

    .

     

    sábado, 16 de septiembre de 2006 0:43
  • Charlie,

    I remembered a set of VB programs that talk to each other.

     I downloaded them from

    somewhere, but I can't find the URL, I just have the zip

    file. (It's a little older version; but VB EE converts it with

    no problem.)

    I have tryed it and it works, but I have not taken the time

    to read the code and see what's happening.

    I have posted it to my web area for a while.

    Download http://users.adelphia.net/~gcumbia/apptalk_Code.zip

    sábado, 16 de septiembre de 2006 4:02
  • I still couldnt get it to work, I tried something diff but no dice again :(

     

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'Clear the clipboard

    My.Computer.Clipboard.Clear()

    'Copy text from rich text box onto the clipboard

    My.Computer.Clipboard.SetText(RichTextBox1.Rtf, TextDataFormat.Rtf)

    ' change to prior application, focus should be in text area was in originaly when opened the new app textbox

    My.Computer.Keyboard.SendKeys("%(Tab)", True)

    ' was my ctrl V attempt using this technique

    ' My.Computer.Keyboard.SendKeys("^(v)", True)

    ' Paste from clipboard onto new text area

    Dim PasteText As String

    PasteText = My.Computer.Clipboard.GetText(TextDataFormat.Rtf)

    I have tried find and setforeground for focus, apps, send input, sendkeys, etc etc etc etc.

     

    Nothing works.

     

     

    sábado, 16 de septiembre de 2006 20:47