Solved

Got Focus and Got the content selected

Posted on 2003-12-10
12
264 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
Independent Software Vendors: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

691 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