Select entire contents of Combo Box (1 Viewer)

BrettM

just a pert, "ex" to come
Local time
Today, 16:46
Joined
Apr 30, 2008
Messages
134
As far as I am aware, the following code should select the entire field of a Combo Box when used in the On Got Focus event.

Code:
    Me.Controls(Screen.ActiveControl.Name).SelStart = 0
    Me.Controls(Screen.ActiveControl.Name).SelLength = Len(Me.Controls(Screen.ActiveControl.Name))

If the code is stepped through then it works correctly.
If the code is run normally then it doesn't.

Can anyone suggest what is happening?

Regards Brett
 

jjturner

Registered User.
Local time
Today, 07:46
Joined
Sep 1, 2002
Messages
386
Just hazarding a guess here -

I'm usually suspicious of combo box references that don't include the .Value property

So if possible, I'd try to pop it in your Len expression just so the content is explicitly pointed to.

Hope that helps.

Regards,
John
 

BrettM

just a pert, "ex" to come
Local time
Today, 16:46
Joined
Apr 30, 2008
Messages
134
Thanks for the suggestion John. I delved deeper and found that Microsoft state that SelStart & SelLength work on the .Text portion of a Combo Box.

Same problem though. Here is the modified code. The Pause (2) is simply a 2 second wait that I have created. The whole field is selected for 2 seconds then the selection drops off.

Code:
Private Sub Box_Enter()
    Me.Controls(Screen.ActiveControl.Name).SelStart = 0
    Me.Controls(Screen.ActiveControl.Name).SelLength = Len(Me.Controls(Screen.ActiveControl.Name).Text)
    Pause (2)
End Sub

As I said, if I insert a break point and then step my way through the lines it works perfectly. There is no other event effecting the field or the form.

I have tested this code on a Text Box and it works great. Convert that Text Box to a Combo and it falls over. Convert it back and all is good again.

This is really baffling.
ps. This is the pause code if you want it.
Code:
Function Pause(intSecs As Integer)
    Dim Start As Variant
    Start = Timer
    Do While Timer < Start + intSecs
    DoEvents
    Loop
End Function
 

jjturner

Registered User.
Local time
Today, 07:46
Joined
Sep 1, 2002
Messages
386
Hi Brett,

Seems another poster has run into the same issue

My other thought is that you might want to experiment with the cursor behavior in the Options menu.

Or, further to the suggestions from the other post, maybe try using an event other than GotFocus . . .

Regards,
John
 

BrettM

just a pert, "ex" to come
Local time
Today, 16:46
Joined
Apr 30, 2008
Messages
134
The only event that works is the "On Mouse Up".

Ok... problem solved but I would still like to know what is different with "On Mouse Up" and the "On Click" events as far as a Combo Box goes... especially seeing a text box uses "On Click" to achieve the same result.

Any ideas?
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 07:46
Joined
Sep 12, 2006
Messages
15,743
it possible depends how the combo box is bound

typically the combo box is bound to a hidden column - so selecting the displayed text may not be meaningful in terms of the active record

its unusual to select a whole combo box xontents anyway.

just a thought
 

DCrake

Remembered
Local time
Today, 07:46
Joined
Jun 8, 2005
Messages
8,626
I still have not grasped the concept of what is trying to be acheived here. Can some one explain it to me?
 

BrettM

just a pert, "ex" to come
Local time
Today, 16:46
Joined
Apr 30, 2008
Messages
134
The concept...

The user input is for a time entry. I simply want to be able to give a user some choices (drop down list) hence the combo box - and the ability to start typing immediately. Having the user click somewhere within a time field made up of numbers and colons and then expect them to make edits without getting annoyed is not a great call.

I tried it with my staff for a few months and they constantly complained.

This now fixes the problem.
 

DCrake

Remembered
Local time
Today, 07:46
Joined
Jun 8, 2005
Messages
8,626
Right so when a combo has already got a value, lets say "10:00" and they want to change it to 10:30 so they click on the down arrow or press F4 then click the mouse in the first position after the colon. what should happen should fact that clicked on the value mean that the whole string is highlighted or not.

You may need to look at keyboard behaviour in your options.

David
 

BrettM

just a pert, "ex" to come
Local time
Today, 16:46
Joined
Apr 30, 2008
Messages
134
This is just a way to fix multiple users requests to have the whole field selected on mouse entry.

And, as simple users (hairdressers and beauty therapists) they have no idea about keyboard behaviors and even less understanding as to what all those keys with all the F's are for.

Easiest solution is to give them what they want. "On Mouse Up" does it.
 

jjturner

Registered User.
Local time
Today, 07:46
Joined
Sep 1, 2002
Messages
386
You might want to consider (perhaps at this point for any future time-editing app) to use a Spin Control in conjunction with a text box . . .

I'm doing that right now with one of my apps and it seems to work quite nicely - that way the user doesn't fuss at all with the direct time-string, and just increments or decrements according to their wishes.

John
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 07:46
Joined
Sep 12, 2006
Messages
15,743
You might want to consider (perhaps at this point for any future time-editing app) to use a Spin Control in conjunction with a text box . . .

I'm doing that right now with one of my apps and it seems to work quite nicely - that way the user doesn't fuss at all with the direct time-string, and just increments or decrements according to their wishes.

John

thats a good idea

time input is tricky, as the syntax has to be spot on - and you cant really have a time picker, as you can have a date picker

--------

one other idea I had was to let users enter on a 24 hr clock - i strip out ALL puctuation, then reinsert my own - ok as long as they dont exceeed 2359
 

BrettM

just a pert, "ex" to come
Local time
Today, 16:46
Joined
Apr 30, 2008
Messages
134
I like that idea. My usage though is for a timesheet...

7 days display with 6 login/logoff times per day. Lunch break display as well as dinner break display and total daily hours. That's 63 different variable fields.

...imagine displaying a spinner control for each login/logoff with 15 minute incriments, let alone the better option of a spinner for the hour and one for the minutes.

What I have discovered though is that using the "Button Up" ALWAYS selects the entire field no matter what. To allow for editing if required I have included a simple counter reset in the "Got Focus" event and use it in the "Button Up".

For those who are interested here is the final result...
Code:
Public ClickCount As Integer
 
Function OnlyTimeKeys(KeyAscii As Integer) As Integer
On Error Resume Next
    Select Case KeyAscii
    Case 8, 48 To 58 ' allow backspace, periods and digits
    Case 13, 27 ' Enter and ESC 
        me.tbHidden.SetFocus ' very small transparent control object
        Exit Function ' Do anything here you want
    Case Else: KeyAscii = 0 ' reject everything
    End Select
    OnlyTimeKeys = KeyAscii
    Me.Controls(Screen.ActiveControl.Name).Dropdown ' List of allowed times
End Function
 
Private Sub SelectAll()
    If ClickCount <> 0 Then Exit Sub
    'select all of control
    Me.Controls(Screen.ActiveControl.Name).SelStart = 0
    Me.Controls(Screen.ActiveControl.Name).SelLength = Len(Screen.ActiveControl.Name)
    ClickCount = 1
End Sub
 
' Combo Box example here is obviously named Combo
Private Sub Combo_KeyPress(KeyAscii As Integer)
    KeyAscii = OnlyTimeKeys(KeyAscii)
End Sub
 
Private Sub Combo_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    SelectAll
End Sub
 
Private Sub Combo_GotFocus()
    ClickCount = 0 'resets the counter for SelectAll()
End Sub

My original code does a heap of other stuff which I have stripped out but you get the idea.
...and can you tell that I like one line event coding?
 

jjturner

Registered User.
Local time
Today, 07:46
Joined
Sep 1, 2002
Messages
386
Not to bedevil you now that you have a more-or-less finished product, but if you use a GotFocus pointer to callup the values for each displayed control into a single (or a select few) 'editing' control(s), this would limit your overhead appreciably - which in turn would make a Spin control feasible, I should think.
 

BrettM

just a pert, "ex" to come
Local time
Today, 16:46
Joined
Apr 30, 2008
Messages
134
Not to bedevil you now that you have a more-or-less finished product, but if you use a GotFocus pointer to callup the values for each displayed control into a single (or a select few) 'editing' control(s), this would limit your overhead appreciably - which in turn would make a Spin control feasible, I should think.

It would, however I believe it would actually make it a little more complicated for my users and possibly decrease their interest if they had to click in the time field displayed and then have to work in say a popup that had a time spinner. It would also negate the ability for the field to auto-fill with the current time (to the nearest 15min) to allow a "set and forget" ability. The field dropdown also defaults to this current time for easy user adjustments.

As I said, my code does heaps of other stuff along with the offered solution.

I do like your idea though and will probably utilise it in another venture.
 

jjturner

Registered User.
Local time
Today, 07:46
Joined
Sep 1, 2002
Messages
386
I hear you. Loss of user interest due to a higher input threshold can be a killer. Sounds like this is a case where immediacy for the end-user trumps application efficiency for the developer ;)

Well, cheers from across the "big pond" and all the best for your project Brett :cool:
 

Users who are viewing this thread

Top Bottom