Solved

Got Focus and Got the content selected

Posted on 2003-12-10
12
262 Views
Last Modified: 2012-05-04
On a form, I have text box, combo box, etc. I would like to have a function that whenever that control got focus, it will be selected and highlighted, so user can start change value without selected it manually.

I can do it control by control like following, but would like to find some shortcut, any suggestions?

text1_Gotfocus ()
   SendKeys "+{END}", True
End

Thank You
0
Comment
Question by:z3c0
  • 5
  • 4
  • 3
12 Comments
 
LVL 4

Expert Comment

by:learning_t0_pr0gram
ID: 9912759
Text1_Gotfocus()
 SendKeys "{HOME}+{END}"
End Sub
0
 

Author Comment

by:z3c0
ID: 9912791
My question is I don't want to do this control by control, but would like have some form_ level function that make this work.

Thank You!

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 9913003
Add a Timer to Project and try this code out:

Option Explicit

Private lastcontrol As Object

Private Sub Form_Load()
    Set lastcontrol = Me
    Timer1.Interval = 50
End Sub

Private Sub Timer1_Timer()
    On Error GoTo noSuchProperty
   
    Dim curControl As Control
    Set curControl = Screen.ActiveControl
    If curControl.Name <> lastcontrol.Name Then
        Set lastcontrol = curControl
        curControl.SelStart = 0
        curControl.SelLength = Len(curControl.Text)
    End If
    Exit Sub
noSuchProperty:
End Sub

Regards,

Idle_Mind
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Expert Comment

by:learning_t0_pr0gram
ID: 9915267
but why use much more coding?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9915441
It's not more coding.  You place my code in only once and it works no matter how many more controls you add.  If you use the Got_Focus() Method you have to update all your existing controls and put it in for any new controls as well.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9915449
My scheme will also work for dynamically created controls.
0
 
LVL 4

Expert Comment

by:learning_t0_pr0gram
ID: 9915815
oooh, i see.. i didn't know he was using more then one text box
0
 

Author Comment

by:z3c0
ID: 9985524
Got a new problem with this one if you can help!

I have more than one form in the application, and put the timer in each of the form, if I open form1 and close form1, and then open form2, after 1 or 2 seconds of form2 showing up, form1 will be loaded again. It seems, the timer for the previous open form not stop with the form unload. Any idea?

Thanks a lot!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9985580
You could try turning the timer off in the form unload sub:

Private Sub Form_Unload()
    Timer1.Enabled = False
End Sub
0
 

Author Comment

by:z3c0
ID: 9985628
I did, but not working.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9985735
Try this out:

Option Explicit

Private lastcontrol As Object
Private gotFocus As Boolean

Private Sub Form_Load()
    Set lastcontrol = Me
    gotFocus = True
    Timer1.Interval = 50
End Sub

Private Sub Form_Unload(Cancel As Integer)
    gotFocus = False
End Sub

Private Sub Form_GotFocus()
    gotFocus = True
End Sub

Private Sub Form_LostFocus()
    gotFocus = False
End Sub

Private Sub Timer1_Timer()
    On Error GoTo noSuchProperty
   
    Dim curControl As Control
   
    If gotFocus Then
        Set curControl = Screen.ActiveControl
        If curControl.Name <> lastcontrol.Name Then
            Set lastcontrol = curControl
            curControl.SelStart = 0
            curControl.SelLength = Len(curControl.Text)
        End If
    End If
    Exit Sub
noSuchProperty:
End Sub

0
 

Author Comment

by:z3c0
ID: 9986784
Got it resolved. Thanks a lot for your help!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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