Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need an another timer 1

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
I've created a group of controls that act together as 1 unit. When the cursor moves to a control on the form that is not part of my group of controls, I need to hide some of the controls that are part of the group. To do this I need to either process the GotFocus event on every control on the form or set a timer event on the form. I'm currently using the timer technique. When the timer fires it checks to see if the active control is part of the group of controls. If not, it hides specific controls in the group.

But, since the group of controls I've created will become part of my library of routines that other programmers will be using, the timer technique could interfere with timer events on their forms. I could set a flag to indicate that the timer event was kicked off to check my stuff. The programmer would then have to write code to handle the flag.

If a programmer decides to use my "group" of controls, I want to make it as simple as possible to use (For example, I've created a wizard that will add the group of controls to the form). I don't want the programmer to have to do any additional coding to make my "group" of controls work properly.

So, does any one have another solution that will let me know when the active control is not part of the group of controls? I was hoping that there is another timer besides the form timer (ie a vba function or an activeX control) that I can use.
 
I assume that you already have created a custom "myControl" class and a custom "myControls" collection class to encapsulate all of the functionality, properties and methods of your controls. However, I wrote the below code as if you did not.

When you create your controls, I know of no way that you can identify them as your controls and make that permanent. So I will assume you use the tag property (and all bets are off if the user wants to modify the tag property). For a form or report you have custom properties that you could use, but I do not think that exists for controls.


So in your wizard when you add your controls to the form then use vba extensibility ( to write the following code to the form.

Code:
Public mch As MyControlHider

Private Sub Form_Load()
  Set mch = New MyControlHider
  mch.Init Me
End Sub
in the init procedure you have a second parmater to set the timer interval. If the user is already the timer interval you are stuck with their interval.

then build the following class module called "myControlHandler"

Code:
Private mMyControls As Collection
Private mNotMyControls As Collection
Private WithEvents mForm As Access.Form

Public Sub Init(theForm As Access.Form, Optional theTimerInterval As Double = 0.5)
  Dim ctrl As Access.Control
  Set mMyControls = New Collection
  Set mNotMyControls = New Collection
  Set mForm = theForm
  For Each ctrl In mForm.Controls
     If ctrl.Tag = "?" Then
       mMyControls.Add ctrl
     Else
       mNotMyControls.Add ctrl
     End If
  Next ctrl
  mForm.OnTimer = "[Event Procedure]"
  'If the user is using the timer already then your stuck with
  'his interval
 If CDbl(mForm.TimerInterval) = 0 Then
    mForm.TimerInterval = theTimerInterval
  Else
    MsgBox "The form timer is already set at " & mForm.TimerInterval
  End If
End Sub
Private Sub mForm_Timer()
  Dim actCtrl As Access.Control
  Dim ctrl As Access.Control
  Dim isMyControl As Boolean
  Set actCtrl = mForm.ActiveControl
  For Each ctrl In mMyControls
    If ctrl Is actCtrl Then
      isMyControl = True
      Exit For
    End If
  Next ctrl
  If Not isMyControl Then
    'assume your
    mMyControls(1).SetFocus
    For Each ctrl In mNotMyControls
       ctrl.Visible = False
    Next ctrl
  End If
End Sub

So by capturing the form's timer event the user can develop their own timer event on the form, and the class is basically transparent to the user.
 
Very nice code in such a short time. You're right, I did create a custom "myControl" class (but not a collection...nice idea). Instead of using the tag property, the wizard asks the user for the base name of the control (i.e. "mycontrol") and then I add prefixes to the controls (i.e. "cboMyControl, lstMyControl, lblMyControl, etc.).

But the form's timer event can be a problem. For example, on one form I might have the timer set to 1 minute intervals. That's way to long to wait to hide the "group" of controls. Consequently, the programmer would have to code around this problem. I don't want the programmer to mess with it.

However, I guess if the class set the timer to an odd value (i.e. TimerInterval=237), and then I could set it up to call "MyGroupTimer" function first, check to see if the TimerInterval is 237, if not, then call Form_Timer. The only thing the programmer would have to do is, instead of setting the TimerInterval to 0 set it to 237 (represented by a public constant).

Thanks for your help.

BTW, my subject line should read "Need another timer" not "Need an another timer
 
I think I would do it different then. I would still build a myControlHider class, but then I would build two other small classes. "NotMyControl" class and "NotMyControls" collection class.
When you instantiate a myControlHider inside the class it would then basically create an instance of NotMyControl for each control on the form that is not one of your controls, and add it to a collection of type NotMyControls. To see a demo of this I have a FAQ in the forms forum on simulating a control array.

In that way you can "listen" for the gotfocus event of every control that is not one of your controls.

Again the only code needed on a form would be
Code:
Public mch As MyControlHider

Private Sub Form_Load()
  Set mch = New MyControlHider
  mch.Init Me
End Sub

Also I would definately think you need a custom collection class to manage your "MyControls". The faq will demonstrate some of the advantages.

When I get a chance I will send a demo of this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top