Sendkeys to combobox wont work

Using Sendkeys to a combobox on a another form wont work,
ok for everything else, only first character is shown
following Sendkeys are replace over top of first position
Im currently using a mirrored textbox, but is there a way?
THANK YOU IN ADVANCE SORRY ABOUT THE LOW POINTS
vern12Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

twardCommented:
It might help if you show some of the code that you are using.  I believe you can sendkeys a whole string if needed and maybe that would work for you if you are doing it one at a time.
0
vern12Author Commented:
Yes I can send strings, nothing wrong with code works everywhere
else, something special about combobox, ive tried various property settings, anybody?
0
deightonprogCommented:
I had no problems with the following, hope it helps

Private Sub Command1_Click()

    Form1.Combo1.SetFocus
    SendKeys "hello"

End Sub

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

deightonprogCommented:
Heres a bit more - if you put wait = true as shown it works better

Private Sub Command1_Click()

    Form1.Combo1.SetFocus
    'use wait  = true to ensure success
    SendKeys "hello", True
    Form2.SetFocus

End Sub

0
vern12Author Commented:
thanks deighton, I can send strings too
problem is if you send one character or string with sendkey and then another , the second overwrites the first, this does not happen with a textbox only a combobox.
0
deightonprogCommented:
Ok i'll set to work on it - answer to follow!
0
bin_huwairibCommented:
vern12,

The following code will add a randome characters to combo1 on form2.

Do the following:

1 - Add form1 and form2 to project1.
2 - Add command1 to form1.
3 - Add combo1 to form2.
4 - Paste this code in form1:

Private Sub Form_Load()
 Form2.Show
End Sub

Private Sub Command1_Click()
 Form2.Combo1.SetFocus
 Randomize
 SendKeys "{END}" & Chr(Int((72 - 50 + 1) * Rnd + 50))
End Sub


Best regards
Bin Huwairib
0
bin_huwairibCommented:
vern12,

I forget to mention that one of combo box behavior is that text will be selected automatically when the control got the focus, that is why the next character will always replace the pervious one.

If you would like to get ride of {END} in SendKeys method as another solution, you have to add the following code in the combo box GotFocus event in form2:

Private Sub Combo1_GotFocus()
 Combo1.SelStart = Len(Combo1.Text)
 Combo1.SelLength = Len(Combo1.Text)
End Sub

And form1 code will look like this:

Private Sub Command1_Click()
 Form2.Combo1.SetFocus
 Randomize
 SendKeys Chr(Int((72 - 50 + 1) * Rnd + 50))
End Sub

Private Sub Form_Load()
 Form2.Show
End Sub


Best regards
Bin Huwairib
0
vern12Author Commented:
Thank you bin huwairib
RIGHT,LEFT,DEL,INS, could not work with your example.
If I use the mouse to click 4th position of comdobox text
focus (and following SendKeys) is sent to end of text
not 4th position. Help.
0
deightonprogCommented:
Hope this code fragment helps - if you have focus don't go to end


   If Form1.ActiveControl <> "combo1" Then
   
        Form1.combo1.SetFocus
       
    End If
   
    SendKeys "{end}" + "h", True
       
    If Form1.ActiveControl <> "combo1" Then
   
        Form1.combo1.SetFocus

    End If
   
    SendKeys "{END}" & "e"
0
vern12Author Commented:
deighton, this would force combo1 to have focus always
yes... but the same problem persists. thank you
0
bin_huwairibCommented:
vern12,

Do the following:

1 - Add form1 and form2 to project1.
2 - Add module1 to project1.
3 - Add command1 to form1.
4 - Add combo1 to form2.
5 - Add timer1 to form2 then set Interval property to 0 and set Enabled property to False.

===== Paste this code in form1:

Private Sub Form_Load()
 Form2.Show
End Sub

Private Sub Command1_Click()
 Form2.Combo1.SetFocus
 Randomize
 SendKeys Chr(Int((72 - 50 + 1) * Rnd + 50))
End Sub

===== Paste this code in form2:

Private Sub Combo1_GotFocus()
 Combo1.SelStart = S
 Timer1.Enabled = True
End Sub

Private Sub Combo1_LostFocus()
 Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
 Combo1.SetFocus
 Randomize
 SendKeys Chr(Int((72 - 50 + 1) * Rnd + 50))
End Sub

Private Sub Timer1_Timer()
 S = Combo1.SelStart
End Sub

===== Paste this code in module1:

Public S As Integer


Best regards
Bin Huwairib
0
vern12Author Commented:
bin
string ends up inverted, right , left, tab does not work
also can`t know position of last mouse click, say to start at
position3
I've tried looking for combo1.setposition or something
Thank YOU  so much ,
vern, hey I got 5 xtra point today,


0
bin_huwairibCommented:
vern12,

What are you talking about?

Don't get confused, check my recent answer.

This example is working fine with me I'm sure that you miss somthing, please tell me exactly what you did.


Bin Huwairib
0
vern12Author Commented:
Ok Bin , using your code  if I sendkeys {right} instead of rnd
pointer does not move right, thanks,
0
bin_huwairibCommented:
vern12,

By mistake I include Private Sub Command1_Click() event twice in form1 and form2, please remove the one in form2 and change the one in form1 to the following:

Private Sub Command1_Click()
 Form2.Combo1.SetFocus
 Randomize
 SendKeys "{Right}"
End Sub

This will do you job definitely.


Best regards
Bin Huwairib
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vern12Author Commented:
bin any keys sent by your solution always go to the end of the text string within the combo box and not my last pointed position from either a mouse click or direction keys (be it the keyboard or by Senkeys), that is I cannot add text to the center of the combot box string, as i can when sending the keys to a textbox.  

please note in the code to follow form2.combo1.setfocus
is replaced with form2.setfucus to remember our last focus,
add a text box to the second form. Now we can click on the combo box or a textbox to send keys, and see the difference for
yourself. Ive tried variations of {end} but I cant correct this.
 
Using a keyboard I can add  text to the 3rd position within a string of a combo box, but not with a mouse click and sendkey
which works with the text box perfectly. I enclosed the code
for you to see. Please check this behaviour.
Thank Kindly vern12

Private Sub Form_Load()
 Form2.Show
End Sub
Private Sub Command1_Click()
 'Form2.Combo1.SetFocus
 Form2.SetFocus
 Randomize
 'SendKeys "{END}" & Chr(Int((72 - 50 + 1) * Rnd + 50))
 SendKeys Chr(Int((72 - 50 + 1) * Rnd + 50))
End Sub

Private Sub Command2_Click()
 'Form2.Combo1.SetFocus
 Form2.SetFocus
 'SendKeys "{END}" & "{Right}"
 SendKeys "{Right}"
End Sub

Private Sub Command3_Click()
 'Form2.Combo1.SetFocus
 Form2.SetFocus
 'SendKeys "{END}" & "{Left}"
 SendKeys "{Left}"
End Sub

Private Sub Command4_Click()
 'Form2.Combo1.SetFocus
 Form2.SetFocus
 Randomize
 'SendKeys "{END}" & Chr(Int((72 - 50 + 1) * Rnd + 50))
 SendKeys Chr(Int((72 - 50 + 1) * Rnd + 50))
End Sub

'form2
Private Sub Combo1_GotFocus()
 Combo1.SelStart = Len(Combo1.Text)
 Combo1.SelLength = Len(Combo1.Text)
End Sub
   
0
vern12Author Commented:
Adjusted points to 30
0
bin_huwairibCommented:
vern12,

I think that you are really confused, becuase if you had looked at my second answer you will notice that I've added a Timer1_Timer and Combo1_GotFocus and Combo1_LastFocus events in form2 to keep track for cursor position on combo1, I will repost my answer again, so please read it carefully this time.

== Paste this code in form1:
Private Sub Form_Load()
 Form2.Show
End Sub

Private Sub Command1_Click()
 Form2.Combo1.SetFocus
 Randomize
 SendKeys Chr(Int((72 - 50 + 1) * Rnd + 50))
End Sub

Private Sub Command2_Click()
 Form2.Combo1.SetFocus
 SendKeys "{Right}"
End Sub

Private Sub Command3_Click()
 Form2.Combo1.SetFocus
 SendKeys "{Left}"
End Sub

== Paste this code in form2:
Private Sub Combo1_GotFocus()
 Combo1.SelStart = S
 Timer1.Enabled = True
End Sub

Private Sub Combo1_LostFocus()
 Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
 S = Combo1.SelStart
End Sub

== Paste this code in module1:
Public S As Integer


Best regards
Bin Huwairib
0
vern12Author Commented:
bin, sorry about my confusion, thanks for you patience and repost, and most of all the excellent answer, vern
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.