Solved

Sendkeys  to  combobox wont work

Posted on 1998-05-06
20
668 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:vern12
  • 9
  • 6
  • 4
  • +1
20 Comments
 
LVL 7

Expert Comment

by:tward
ID: 1442127
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
 

Author Comment

by:vern12
ID: 1442128
Yes I can send strings, nothing wrong with code works everywhere
else, something special about combobox, ive tried various property settings, anybody?
0
 
LVL 18

Expert Comment

by:deighton
ID: 1442129
I had no problems with the following, hope it helps

Private Sub Command1_Click()

    Form1.Combo1.SetFocus
    SendKeys "hello"

End Sub

0
 
LVL 18

Expert Comment

by:deighton
ID: 1442130
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
 

Author Comment

by:vern12
ID: 1442131
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
 
LVL 18

Expert Comment

by:deighton
ID: 1442132
Ok i'll set to work on it - answer to follow!
0
 
LVL 5

Expert Comment

by:bin_huwairib
ID: 1442133
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
 
LVL 5

Expert Comment

by:bin_huwairib
ID: 1442134
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
 

Author Comment

by:vern12
ID: 1442135
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
 
LVL 18

Expert Comment

by:deighton
ID: 1442136
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:vern12
ID: 1442137
deighton, this would force combo1 to have focus always
yes... but the same problem persists. thank you
0
 
LVL 5

Expert Comment

by:bin_huwairib
ID: 1442138
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
 

Author Comment

by:vern12
ID: 1442139
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
 
LVL 5

Expert Comment

by:bin_huwairib
ID: 1442140
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
 

Author Comment

by:vern12
ID: 1442141
Ok Bin , using your code  if I sendkeys {right} instead of rnd
pointer does not move right, thanks,
0
 
LVL 5

Accepted Solution

by:
bin_huwairib earned 30 total points
ID: 1442142
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
 

Author Comment

by:vern12
ID: 1442143
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
 

Author Comment

by:vern12
ID: 1442144
Adjusted points to 30
0
 
LVL 5

Expert Comment

by:bin_huwairib
ID: 1442145
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
 

Author Comment

by:vern12
ID: 1442146
bin, sorry about my confusion, thanks for you patience and repost, and most of all the excellent answer, vern
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now