Leaner Code with Multiple Controls?

G

Guest

I have several forms with many bound controls...
I want the bound control with focus to have a background color different
from the default and the label to be bold.
I want any bound control with a value that has been changed but not saved to
be bold.

Now I can do this for each control with code like this...

Private Sub ThisControl_GotFocus()
Me.ThisControl.BackColor = 13434828
Me.ThisControl.FontBold = True
End Sub

Private Sub ThisControl_LostFocus()
Me.ThisControl.BackColor = vbWhite
Me.ThisControl.FontBold = False
End Sub

Private Sub ThisControl_AfterUpdate()
Me.ThisControl.FontBold = True
End Sub

I'm not sure I want to take the time to do this for each and every control
on each and every form. I know I can create a common subroutine in module
and pass it parameters. But replacing one or two lines of code with a single
line calling a module doesn't seem like much of a savings.

Is there any way to have a global treatment of control properties and
control events so I am not rewriting the same code over and over again?
 
D

Douglas J. Steele

Unless you've got other code that you need to put for any of those three
events, create functions:

Function GF()
Me.ActiveControl.BackColor = 13434828
Me.ActiveControl.FontBold = True
End Function

Function LF()
Me.ActiveControl.BackColor = vbWhite
Me.ActiveControl.FontBold = False
End Function

Function AU()
Me.ActiveControl.FontBold = True
End Function

Now, with the form in Design mode, select all of the controls for which you
want that code to apply. In the Properties sheet, put =AU() for the
AfterUpdate property, =GF() for the GotFocus property and =LF() for the
LostFocus property.
 
G

Guest

Doug,

One thing though. The label for the activecontrol should be bold/unbold in
the GF() LF() functions.

Is there a way to reference the label???
 
D

Douglas J. Steele

Function GF()
Me.ActiveControl.BackColor = 13434828
Me.ActiveControl.FontBold = True
If Me.ActiveControl.Controls.Count > 0 Then
Me.ActiveControl.Controls(0).FontWeight = 700
End If
End Function

Function LF()
Me.ActiveControl.BackColor = vbWhite
Me.ActiveControl.FontBold = False
If Me.ActiveControl.Controls.Count > 0 Then
Me.ActiveControl.Controls(0).FontWeight = 400
End If
End Function
 
G

Guest

Thanks Doug works like a charm.

So I am guessing me.activecontrol.controls(0) is the label and
me.activecontrol.controls(1) is the activecontrol?

when would you have me.activecontrol.controls(2) or (3), etc.?

owp^3
 
D

Douglas J. Steele

No. ActiveControl is the active control.

Each control has associated with it a Controls collection. For text boxes,
list boxes and combo boxes, the only control that can be in its Controls
collection is the associated label. For frames, the Controls collection will
contain the label for the frame, each of the radio buttons, buttons or check
boxes within the frame and each of the labels associated with those
controls. For example, I have a frame on my form with 8 radio buttons. If
the frame is the ActiveControl, the label for the frame is
ActiveControls.Controls(0), the first radio button is
ActiveControls.Controls(1), the label for the first radio button is
ActiveControls.Controls(2) and so on up to the eighth radio button being
ActiveControls.Controls(15) and the label for the eighth radio button being
ActiveControls.Controls(16).

The reason why I'm checking whether the Controls collection count is greater
than 0 is that not all controls have a label associated with them.
 
G

Guest

Gotcha! Thanks again!

owp^3

Douglas J. Steele said:
No. ActiveControl is the active control.

Each control has associated with it a Controls collection. For text boxes,
list boxes and combo boxes, the only control that can be in its Controls
collection is the associated label. For frames, the Controls collection will
contain the label for the frame, each of the radio buttons, buttons or check
boxes within the frame and each of the labels associated with those
controls. For example, I have a frame on my form with 8 radio buttons. If
the frame is the ActiveControl, the label for the frame is
ActiveControls.Controls(0), the first radio button is
ActiveControls.Controls(1), the label for the first radio button is
ActiveControls.Controls(2) and so on up to the eighth radio button being
ActiveControls.Controls(15) and the label for the eighth radio button being
ActiveControls.Controls(16).

The reason why I'm checking whether the Controls collection count is greater
than 0 is that not all controls have a label associated with them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top