Solved

Determine ControlName which has just received Focus

Posted on 2003-12-05
8
340 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
[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
  • 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 86

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
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 86

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
 
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 86

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

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…
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 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 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…

751 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