How to highlight text upon click/enter events (1 Viewer)

fugifox

Registered User.
Local time
Today, 13:16
Joined
Oct 31, 2006
Messages
95
I have a combo box and I want to highlight all its text text upon Click or Enter events. Doing so for GotFocus event is easy
Code:
Sub Text1_GotFocus ()
      text1.SelStart = 0          ' Start selection at beginning.
      text1.SelLength = Len(text1.Text)  ' Length of text in Text1.
End Sub
and I have already successfully tested.

But if I apply the above code to either Click or Enter events,
text is not highlighted, instead the cursor blinks at the position I clicked.
After some debugging I saw that text is highlighted indeed just after the execution of "text1.SelLength" command but right afterward is dehighlighted

To show you what I mean let assume the code below
Code:
Sub Text1_GotFocus ()
(1)      text1.SelStart = 0          
(2)      text1.SelLength = Len(text1.Text)  
(3)      msgbox "ok"
End Sub
(parenthesis is just line numbers)
I saw that text is highlighted indeed after the execution of line (2)
but after executing line (3) text is dehighlighted and cursor starts blinking as I wrote above.
Is this a bug or am I doing something wrong?
 

MarkK

bit cruncher
Local time
Today, 04:16
Joined
Mar 17, 2004
Messages
8,181
Ya. That's a bit weird. Probably Access runs it's GotFocus event handler after yours.
Try MouseUp and KeyUp for the control. By that time Access is done processing its text selection behaviors and won't overwrite yours.
 

missinglinq

AWF VIP
Local time
Today, 07:16
Joined
Jun 20, 2003
Messages
6,423
:confused: Two questions:

You say this deals with a combobox, but your control name, Text1, would seem to indicate a textbox instead: which is it?

If it actually is a combobox, what exactly are you trying to accomplish by hi-lighting the text in it?
 

fugifox

Registered User.
Local time
Today, 13:16
Joined
Oct 31, 2006
Messages
95
First of all thanx for the reply

:confused: Two questions:

You say this deals with a combobox, but your control name, Text1, would seem to indicate a textbox instead: which is it?

If it actually is a combobox, what exactly are you trying to accomplish by hi-lighting the text in it?

I used Text1 name inadvertently as I copy-pasted from another page.
It is a combo box indeed and I what I want to accomplish by highlighting it is user to be able to erase whole text by just hitting Delete/Backspace.
You see, I use the combobox to move to the corresponding records either by selecting a value from or by typing the first letters.

@lagbolt: Considering the above you can figure out that the mouse events don't match for my case.

Any other ideas?
 

MarkK

bit cruncher
Local time
Today, 04:16
Joined
Mar 17, 2004
Messages
8,181
What doesn't work about these?
Code:
Private Sub yourCombo_KeyUp(KeyCode As Integer, Shift As Integer)
   Me.yourCombo.SelStart = 0
   Me.yourCombo.SelLength = Len(Me.yourCombo.Text)
End Sub

Private Sub yourCombo_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
   Me.yourCombo.SelStart = 0
   Me.yourCombo.SelLength = Len(Me.yourCombo.Text)
End Sub
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 04:16
Joined
May 13, 2009
Messages
93
As with Excel, I have not been able to get the highlighting to work.
This is my work around.
I find it easier to set the value of the combo box, text box, etc. to nothing after returning focus in the "After_Update" event
Code:
Me.yourCombo.Value = ""
 

DCrake

Remembered
Local time
Today, 12:16
Joined
Jun 8, 2005
Messages
8,632
To save the user hitting delete or backspace you can just use

Me.Combo = "" on the GotFocus event. Its downfall is that it clear the selection every time even if you don't want it to.

For example if you have a combo box that is based on a query and in the default value you have "Click for Options" as soon as the user clicks on the combo the "Click for Options" phrase dissapears.

David
 

Rick Stanich

King and Supreme Ruler
Local time
Today, 04:16
Joined
May 13, 2009
Messages
93
As with Excel, I have not been able to get the highlighting to work.
This is my work around.
I find it easier to set the value of the combo box, text box, etc. to nothing after returning focus in the "After_Update" event
Code:
Me.yourCombo.Value = ""
I should have been more clear on this.
Example:
Code:
Private Sub sPartNumber_AfterUpdate()
If bOk = False Then 'bOk is Boolean
    Me.sPartRev.SetFocus 'set focus to any control
    Me.sPartNumber.SetFocus 'set focus to desired control
    Me.sPartNumber.Value = "" 'set value to "" (null)
End If
End Sub
I set a true or false value and if it evaluates to false then the value is set to "".
 

Users who are viewing this thread

Top Bottom