?
Solved

Got Focus and Got the content selected

Posted on 2003-12-10
12
Medium Priority
?
266 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 500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

777 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