Selecting the text of a combo box

  • Thread starter Thread starter Carl Imthurn
  • Start date Start date
C

Carl Imthurn

I have a combo box that, when the user clicks on the text area of it, I want the text to become selected.

Here's my code:

Private Sub cboSelectAClinic_GotFocus()

MsgBox "running the GotFocus event"
cboSelectAClinic.SelStart = 0
cboSelectAClinic.SelLength = Len(cboSelectAClinic.Text)

End Sub

Here's what happens:

When I select one of the combo box entries via the drop down functionality, everything works fine -- the text portion becomes selected. Then, naturally, as soon as I hit <tab> or click somewhere else on the form, it becomes unselected.
So far so good.
The user then clicks up in the text area of the combo box and it does *not* select the existing text -- the cursor is merely positioned in the text area of the combo box. The user can, of course, manually select the whole text area and start typing, but
that is exactly what I was hoping to accomplish with the above code.
When the user clicks in the text area, the msgbox appears, so the event is definitely firing, but simply not doing what I believe it should be doing . . .

I've tried a number of things without success, including:

cboSelectAClinic.SelLength = 17 ' hard-coding the length of this particular combo box entry -- same result as above
cboSelectAClinic.SelStart = Len(cboSelectAClinic.Text) ' same result as above
using the Enter event instead
Pounding on the keyboard with my fists
Bashing my head against the monitor

What am I missing?

Thanks in advance --

Carl
 
Carl, you have provided that rarest of things: a clear description of the
problem, & of what you have tried in order to fix it!!

I don't have Access here to check, so these are just some suggestions off
the top of my head.

- Change the msgbox to debug.print. This ensures that moving the focus to
the msgbox, from the gotfocus event, is not causing any internal confusion!

- Change the selstart/length values to place the cursor at a defined
position within the field; eg. to between the 2nd and 3rd characters. If
that >works<, the problem is not with the selstart/length as such: it is
something to do with selecting the whole field, compared to placing the
cursor at a defined position within the field.

- Disable all the other code in the form's code module, by using #IF FALSE
THEN ... #ENDIF brackets. (Note the crosshatches. These are compile-time
IFs, not runtime IFs. You can use them to disable any contiguous set of
lines in the code module - across &/or within procedures.) If it then
works<, there may be some other code which is interfering with the text
selection. Re-enable the other code bit by bit, to find the culprit area.

- If all else fails, I'd try the same code with a simple textbox. Perhaps
there is something different about selecting the text entry in a combo box?

HTH,
TC


Carl Imthurn said:
I have a combo box that, when the user clicks on the text area of it, I
want the text to become selected.
Here's my code:

Private Sub cboSelectAClinic_GotFocus()

MsgBox "running the GotFocus event"
cboSelectAClinic.SelStart = 0
cboSelectAClinic.SelLength = Len(cboSelectAClinic.Text)

End Sub

Here's what happens:

When I select one of the combo box entries via the drop down
functionality, everything works fine -- the text portion becomes selected.
Then said:
So far so good.
The user then clicks up in the text area of the combo box and it does
*not* select the existing text -- the cursor is merely positioned in the
text area of the combo box. The user can, of course, manually select the
whole text area and start typing, but
that is exactly what I was hoping to accomplish with the above code.
When the user clicks in the text area, the msgbox appears, so the event is
definitely firing, but simply not doing what I believe it should be doing .
.. .
I've tried a number of things without success, including:

cboSelectAClinic.SelLength = 17 ' hard-coding the length of this
particular combo box entry -- same result as above
 
TC --

Thanks for your reply. I tried all of the suggestions you made with the following results:
- Change the msgbox to debug.print. This ensures that moving the focus to
the msgbox, from the gotfocus event, is not causing any internal confusion!

Same result -- the cursor is being positioned in the text area, but the text is not being selected.
- Change the selstart/length values to place the cursor at a defined
position within the field; eg. to between the 2nd and 3rd characters. If
that >works<, the problem is not with the selstart/length as such: it is
something to do with selecting the whole field, compared to placing the
cursor at a defined position within the field.

I did the following:

cboSelectAClinic.SelStart = 3
cboSelectAClinic.SelLength = 5

When I select an entry using the dropdown, the 3rd through the 7th characters are selected, which makes sense.
However, again, when I move off of the combo box and click back into it, no selecting happens -- just like was happening before.
- Disable all the other code in the form's code module, by using #IF FALSE
THEN ... #ENDIF brackets. (Note the crosshatches. These are compile-time
IFs, not runtime IFs. You can use them to disable any contiguous set of
lines in the code module - across &/or within procedures.) If it then
selection. Re-enable the other code bit by bit, to find the culprit area.

Same result as above.
- If all else fails, I'd try the same code with a simple textbox. Perhaps
there is something different about selecting the text entry in a combo box?

I believe that this is the issue -- and I don't know if there is a workaround for it.
Also, one further thing I discovered is that the click event of a combo box does *NOT* fire when you merely click into the text area of that combo box -- it only happens when you click onto the dropdown arrow.

Thanks again for your time - I appreciate it.

Carl
 
Hi Carl

I gather that it works once, but not twice, even though GotFocus is
firing on each occasion, & there is no other code in the form.

Sorry, but if that's the case, I'm out of ideas!

Maybe someoine else has some thoughts.

Cheers,
TC
 
Hi,

I believe this behaviour is by design. When a user clicks inside a text box
portion of a combo box, they are in effect hoping to edit the value.

If they wish to have the whole value selected then they would use tab or an
Accelorator key.

While I note that most users don't use the tab key as much as they should.

If you wish to overide this behaviour then use the MouseUp Event as well.

HTH

--

Cheers
Mark

Free Access/Office Add-Ins at:
http://mphillipson.users.btopenworld.com/

TC said:
Hi Carl

I gather that it works once, but not twice, even though GotFocus is
firing on each occasion, & there is no other code in the form.

Sorry, but if that's the case, I'm out of ideas!

Maybe someoine else has some thoughts.

Cheers,
TC



Carl Imthurn <[email protected]> wrote in message
box does *NOT* fire when you merely click into the text area of that combo
box -- it only happens when you click onto the dropdown arrow.
 
Hi Mark --

Thank you so much -- that solved my problem. I don't know why using the MouseUp event never occurred to me, but that's exactly what I needed. It works perfectly now.

Thank you both for the time you took to help me.

Carl
 
Back
Top