Solved

Determine ControlName which has just received Focus

Posted on 2003-12-05
8
335 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
Comment Utility
Hi dastaub,

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


Cheers!

Rajesh
0
 
LVL 24

Expert Comment

by:R_Rajesh
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Expert Comment

by:mccainz2
Comment Utility


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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 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

10 Experts available now in Live!

Get 1:1 Help Now