How to highlight a text or combo box?

L

Laurel

I want to highlight the contents of a combo box when the user clicks in it,
so they can easily replace what's there with new text. I want to mimic the
behavior of tabbing into the combo box. It looks like I should use
something like this in the click event

With cboFind
.SelStart = 0
.SelLength = Len(.Text)
End With

But that didn't work.. I tried putting it in the GotFocus event and the
Enter event, but that didn't highlight anything either. Is there any way to
accomplish this?

TIA
LAS
 
J

Jeff Boyce

Laurel

One approach would be to have the user click on the attached label, not on
the combo box.

Jeff Boyce
<Access MVP>
 
L

Laurel

When I do that, I get error 2185. "You can't reference a property or method
of a control unless the control has focus.") (That seems strange...
certainly you can modify properties of a control with its having focus...
but maybe not execute methods.)

Any other ideas? (I was having difficulty figuring out how the user would
intiuitively know to go there anyway.)
 
K

Ken Snell [MVP]

I have tested and found that you can make this code work in the MouseDown
event:

Private Sub cboFind_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Me.cboFind.SelStart = 0
Me.cboFind.SelLength = Len(Me.cboFind.Value)
End Sub

Note that this event occurs whenever a mouse button is depressed while the
mouse is over the combo box (clicking into the "textbox" area, clicking on
the down arrow, clicking on an item in the dropdown list, etc.). So note
that this will occur any time the user tries to use the mouse to change the
location of the cursor in the "textbox" area, which may not be what the user
wants to have happen. So you may need to set some flags to know that the
user is entering the combo box the first time (could use the Exit event to
set it to "first time" setting, and use the MouseDown event to set it to
"not first time" after the selection is done, and test for the flag in the
first step of the MouseDown event).


Private blnComboFirstTime As Boolean


Private Sub cboFind_Enter()
blnComboFirstTime = True
End Sub

Private Sub cboFind_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)
If blnComboFirstTime = True Then
Me.cboFind.SelStart = 0
Me.cboFind.SelLength = Len(Me.cboFind.Value)
blnComboFirstTime = False
End Sub
End Sub
 
L

Laurel

Well, you're right that this does cause the data to be highlighted, but it
turns out that it doesn't behave the way highlighted data does when it is
highlighted by tabbling to the field. That is, when you type a new letter
into it, it doesn't erase the highlighted portion. It just inserts the new
letter, leaving the previous data highlighted. Do you know what controls
that?
 
K

Ken Snell [MVP]

Well, now... this is an intriguing challenge.

I have looked in various places, and haven't found any info about this
behavior. I'm sorry...

--

Ken Snell
<MS ACCESS MVP>
 
J

Jeff Boyce

Laurel

The next step I'd take is to set a breakpoint in the code and see if it is
even being triggered. Your 2185 error message implies that the control
doesn't have the focus, even when you click on it.

Any chance you have the Enabled and/or Locked properties set to prevent
folks from getting in?

Good luck

Jeff Boyce
<Access MVP>
 
L

Laurel

Thanks. As you'll see from the thread, I've moved beyond this point. I
finally decided to use a hot key as Van T Dinh suggested in another thread
("navigation off a tab control").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top