Solved

Got Focus and Got the content selected

Posted on 2003-12-10
12
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 4

Expert Comment

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

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 86

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 86

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 86

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA Shell can't Find Word document 11 155
Recommendation vb6 to vb.net or others 14 240
IF ELSE Statement in Excel Macro VBA 16 98
add criteria to query in VB, Access 2003 2 47
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

734 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