I am trying to make a program that can copy strings from notepad and paste it into specific text boxes in my windows form app.
For example, Lets say I have a text box with 5 different strings each one in a different line. So my program contains 5 text boxes.
I want with a press of a single button string from notepad line 1 goes to text box 1 , string from line 2 to textbox 2 etc...
Imports System.Threading
Public Class Form1
Const MOUSEEVENTF_LEFTDOWN As UInteger = &H2
Const MOUSEEVENTF_LEFTUP As UInteger = &H4
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
'TextBox1.Text = e.Location.ToString()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox1.Text = MousePosition.ToString()
End Sub
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As UInteger, ByVal dx As UInteger, ByVal dy As UInteger, ByVal dwData As UInteger, ByVal dwExtraInfo As Integer)
Public Sub LeftClick()
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
Thread.Sleep(100) 'Wait required
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Windows.Forms.Cursor.Position = New Point(40, 78)
LeftClick()
LeftClick()
Thread.Sleep(200)
SendKeys.Send("^c")
Me.BringToFront()
TextBox2.Clear()
Thread.Sleep(200)
TextBox2.Paste()
'SendKeys.Send("^v")
Thread.Sleep(300)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Windows.Forms.Cursor.Position = New Point(41, 97)
LeftClick()
LeftClick()
Thread.Sleep(200)
SendKeys.Send("^c")
Thread.Sleep(200)
Me.BringToFront()
TextBox3.Clear()
Thread.Sleep(200)
TextBox3.Paste()
'SendKeys.Send("^v")
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Windows.Forms.Cursor.Position = New Point(32, 116)
LeftClick()
LeftClick()
Thread.Sleep(200)
SendKeys.Send("^c")
Thread.Sleep(200)
Me.BringToFront()
TextBox4.Clear()
Thread.Sleep(200)
TextBox4.Paste()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Windows.Forms.Cursor.Position = New Point(28, 133)
LeftClick()
LeftClick()
Thread.Sleep(200)
SendKeys.Send("^c")
Thread.Sleep(200)
Me.BringToFront()
TextBox5.Clear()
Thread.Sleep(200)
TextBox5.Paste()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Windows.Forms.Cursor.Position = New Point(23, 151)
LeftClick()
LeftClick()
Thread.Sleep(200)
SendKeys.Send("^c")
Thread.Sleep(200)
Me.BringToFront()
TextBox6.Clear()
Thread.Sleep(200)
TextBox6.Paste()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Button1.PerformClick()
Thread.Sleep(5000)
Button2.PerformClick()
Thread.Sleep(5000)
Button3.PerformClick()
Thread.Sleep(5000)
Button4.PerformClick()
Thread.Sleep(5000)
Button5.PerformClick()
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
End Sub
End Class
I was trying something like that but doesn't work. If I press manually the buttons sometimes did the job but the most of them it will copy paste only the first string to all text boxes.
As long as I have been using VB (30 years) the SendKeys functionality has been notoriously unreliable. Although it has gotten better over the years, it's still pretty random. Here's part of the warning from the current version (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys?view=net-5.0):
The SendKeys class is susceptible to timing issues, which some developers have had to work around. The updated implementation is still susceptible to timing issues, but is slightly faster and may require changes to the workarounds. The SendKeys class tries to use the previous implementation first, and if that fails, uses the new implementation. As a result, the SendKeys class may behave differently on different operating systems.
Specifically, timing and focus are problems, much of what is supposed to happen in response to a keystroke requires response and processing by the active application which can override the expected behavior, or even just take too long to decide not to override the builtin windows behavior. My number one recommendation for all uses of SendKeys has always been: Try to find any other way to do this without SendKeys. Two different ways are described/linked in the comments which you might consider.
If you decide to stick with SendKeys, then these are the only suggestions I can think if that might help:
Add another Thread.Sleep
immediately after the (^c).
Send the (^c) twice with a wait in-between. Or even have the code run the whole routine twice with a Sleep in-between (yes, I know how dumb this is).
Try using SendKeys.SendWait
instead.
Try adding the following section to your App.config file:
<appSettings>
<add key="SendKeys" value="SendInput"/>
</appSettings>
Note, there is some indication that (3) and (4) may not work together. Try both ways and see.