Solved

Got Focus and Got the content selected

Posted on 2003-12-10
12
260 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now