Solved

Determine ControlName which has just received Focus

Posted on 2003-12-05
8
336 Views
Last Modified: 2010-08-05
Current Code:
mName = Trim(TxtName)   'mName is a memory variable   TxtName is a Textbox on a Frame on a Form.
TxtName.BackColor = &HC0C0FF
TxtName.BorderStyle = 1
I have dozens of Textboxes on a Frame; I have lots of redundant code behind each text box.  I have gotten rid of the redundant code
by passing in the name of the textbox to a procedure which contains the previous redundant code behind each text box.  The redundant
code is now:
Call HighLiteMe(TxtName)  'where Txtname is the textbox.

Is there a way to accomplish the following:
Whenever an object on the frame gets focus, if it is a textbox, do this to the text box.  If the program could determine the name of the
Textbox, I would then be able to determine where to send the focus next.


0
Comment
Question by:dastaub
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 24

Assisted Solution

by:R_Rajesh
R_Rajesh earned 100 total points
ID: 9885472
Hi dastaub,

screen.activecontrol.name gives you the name of the activecontrol


Cheers!

Rajesh
0
 
LVL 24

Expert Comment

by:R_Rajesh
ID: 9885501
dastaub,

why not use the gotfocus event

Private Sub Text1_GotFocus()
MsgBox "i go focus"
End Sub


Rajesh
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 400 total points
ID: 9885677
There is no global GotFocus() or LostFocus() event.  I think the closest you are going to get is to use a timer and constantly check the current control to see if it is a textbox.  Then see if it is different the last text box and act accordingly.

Create a project and add a timer and as many labels as you like.  The code below assumes you have at least one TextBox named Text1.

I saw little increase in  CPU usage with this method.

Regards,

Idle_Mind

' Code Follows
Option Explicit

Private lastTextBox As TextBox

Private Sub Form_Load()
    Set lastTextBox = Text1
    HighLiteMe Text1
    Timer1.Interval = 100
End Sub

Private Sub Timer1_Timer()
    Dim curControl As Control
    Set curControl = Screen.ActiveControl
    If TypeOf curControl Is TextBox Then
        If curControl.Name <> lastTextBox.Name Then
            UnHighLiteMe lastTextBox
            Set lastTextBox = curControl
            HighLiteMe curControl
        End If
    End If
End Sub

Private Sub HighLiteMe(ByRef curTextBox As TextBox)
    curTextBox.BackColor = &HC0C0FF
End Sub

Private Sub UnHighLiteMe(ByRef curTextBox As TextBox)
    curTextBox.BackColor = &HFFFFFF
End Sub
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9885684
Oops...

>> Create a project and add a timer and as many labels as you like

What I meant to say was:

Create a project and add a timer and as many TEXTBOXES as you like

Idle_Mind
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:mccainz2
ID: 9886233


You con monitor all you textboxes  with a single got_focus event...

1. add a single textbox to your form
2. In its properties set its index to 0
3. Now add more textboxes to your form  but do so by copying the original textbox , this will result in an array of textboxes
4. Now add the code to the got_focus event for your textbox control array

Private Sub Text1_GotFocus(Index As Integer)
    MsgBox "textbox " & Index & " has focus"
End Sub
 
the index will tell you what box has focus and you can simply pass the index to your function.
0
 

Author Comment

by:dastaub
ID: 9888686
example 1  - inside a mouse up procedure
Set curControl = Screen.ActiveControl
Call HighLiteMe(curControl)

example 2  - inside a procedure
   Set curControl = Screen.ActiveControl
   If TypeOf curControl Is TextBox Then
      If curControl.Name = "TxtMajorComplaint" Then
        Code...
        Code...
      End If
   End If

both codes work, they eliminate the need to hardcode in the name of the active textbox and also can determine the name
of the active text box.  Those were my two goals.  
Thank You.
0
 

Author Comment

by:dastaub
ID: 9888700
in my comment date 12/06/2003 08:57 AM PST, I forgot to add that you have to
declare in the general area:
Dim curControl As Control
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9888703
>> example 1  - inside a mouse up procedure

Don't forget that you can change controls without using the mouse.  Focus may change when you press the TAB key.

Idle_Mind
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

867 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

16 Experts available now in Live!

Get 1:1 Help Now