0% found this document useful (0 votes)
30 views

Form Color Class

Uploaded by

Ferhat Durmuş
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Form Color Class

Uploaded by

Ferhat Durmuş
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

clsForm

CLASS MODULE
FORMCOLOR CLASS
ADD COLOR AND GLOW EFFECTS TO USER FORMS

OK

© 2012 Craig Hatmaker


clsForm

© 2012 Craig Hatmaker


clsForm
Table of Contents
Why Read This? .......................................................................................................................................................................................... 1
Installing App ............................................................................................................................................................................................. 1
Requirements......................................................................................................................................................................................... 1
Installing ................................................................................................................................................................................................. 1
Running .................................................................................................................................................................................................. 1
Components ............................................................................................................................................................................................... 2
Modules ................................................................................................................................................................................................. 2
modMain ............................................................................................................................................................................................ 2
modGeneral ....................................................................................................................................................................................... 2
Forms ..................................................................................................................................................................................................... 2
frmTest ............................................................................................................................................................................................... 2
Classes .................................................................................................................................................................................................... 3
clsForm ............................................................................................................................................................................................... 3
Code ....................................................................................................................................................................................................... 4
modMain.Launch ............................................................................................................................................................................... 4
clsForm.UserForm – Set Property ...................................................................................................................................................... 5
clsForm.Setup .................................................................................................................................................................................... 6
Appendix .................................................................................................................................................................................................... 8
Terms and Jargon ................................................................................................................................................................................... 8
Additional References and Documentation ........................................................................................................................................... 8

© 2017 Craig Hatmaker


clsForm
Why Read This?
End Users: With very little coding, add
color and dynamic effects to forms.
Developers: Learn about class modules
used for handling user form events.
Background
Excel’s user forms lack class (pun
intended). By default they are dull and
dead. They don’t have to be.
With a small class module, any Excel
user form can be “classy”. They can reflect
your company’s branding. They can even
add a little life by lighting up buttons as
the mouse moves over, just like Excel’s
ribbon controls.
No code required in your forms.
clsForm can apply color to any user form
without changing them at all. Read on to
learn how, and how to download all code
in a working example.

Installing App
Requirements Installing Running
This demo requires MS Excel 2007® 1. Download FormColorClass.zip 1. Open the folder
or later. VBA (macros) must be enabled. 2. Once downloaded, right click on the 2. Double click Forms.xls
zip file and use option Extract All 3. If Protected View displays click
3. Make note of folder’s path. Enable Editing.

4. If Security Warning displays click


Enable Content.

This app’s URL is: https://www.dropbox.com/s/hdwjhwa3w52e35n/FormColorClass.zip?dl=0

© 2015 Craig Hatmaker Page 1


clsForm
Components
Modules
Component Description
modMain This is a sample module showing one way to instantiate this class.
NOTE! This module is NOT needed in your projects that use clsForm.

Routine Description
Launch This is the main routine tied to the form button. For more information
on this see Code

modGeneral This is BXL’s general purpose library of routines common to all BXL apps. Because this is a general
purpose library, please see the module for documentation on each routine. Only two are used in this
project. modGeneral.DspErrMsg and modGeneral.Color()
NOTE! This module is required in your projects that use clsForm.

Routine Description
Color This function stores and returns colors for various workbook elements.

DspErrMsg For more on BXL error handling see BXL Error Handling

Forms
Component Description
frmTest This a sample selection form to demonstrate clsForm. It is important
to note this form has no color added and no functions to add color. It
is a normal form. All coloring is added by clsForm.
NOTE! This form is NOT needed in your projects that use clsForm.

© 2015 Craig Hatmaker Page 2


clsForm
Classes
Component Description
clsForm clsForm is an event handler that provides glowing effects and other user form control events. This is the
heart of this demo.

Property Description
UserForm This handles a bit of a quirk with Excel’s user forms. UserForms as a
UserForm object type exposes events we need to turn off button glowing
effects. But UserForm as a UserForm object type does not expose
properties such as top, width, etc. UserForm as a generic Object object
type does.
For more information on this see Code

Frame This control, like the userform itself, is control container. When the
mouse hovers over a control container, all buttons and images in the
control container must have their glow effects turned off.

Image This control can glow when the mouse hovers over it if its picture has
transparent portions.

Multipage This control, like the userform itself, is control container. When the
mouse hovers over a control container, all buttons and images in the
control container must have their glow effects turned off.

CommandButton This control can glow when the mouse hovers over it.

Event Description
Class_Terminate Handles housekeeping when the class goes out of scope.

oMsForm_MouseMove Turns off button glow when mouse hovers over the form’s surface

oFrame_MouseMove Turns off button glow when mouse hovers over a frame’s surface

oCmdBtn_MouseMove Turns on button glow when mouse hovers over a button

oImage_MouseMove Turns on button glow when mouse hovers over an image

oMltPag_MouseMove Turns off button glow when mouse hovers over a multipage’s surface

Routine Description
Setup Initializes backgrounds, fonts and positions.

UnGlow Turns off all controls glow effects within a container.

© 2015 Craig Hatmaker Page 3


clsForm
Code
Quick Intro to Classes and Code Overview
Class module routines, unlike normal module routines, can do nothing until the class is “instantiated”. “Instantiated” means to
create an instance. To create an instance we use the “New” keyword on the class module which creates a class object based on the
class module and assigns the object to a variable. We will see how this is done in modMain.Launch.
The word “instance” implies there could be several instances of class objects based on our class module and this is one of the
many advantages of classes. All we have to do to create a second instance is use the “New” keyword again and assign the new class
object to a second variable. In this particular class, the ability to have multiple instances is critical. In modMain.Launch we will see
how to create a single instance of our class and for this instance, assign a userform to it.
In clsForm.Setup we will see how this class creates multiple instances of itself with each instance assigned one of the user form’s
controls. It does not matter how many controls are on the userform. clsForm will create as many instances of itself as there are
controls.
NOTE! This is a bit of an odd approach but I chose this approach to have one, and only one class module rather than a set of class
modules, one for the form, and others for each control type.

modMain.Launch
This routine instantiates clsForm, links it to frmTest, and displays frmTest. It is just four lines of code which is all we need to link
clsForm to any userform we have.
We require a variable to Sub Launch()
instantiate our class and keep it in Static FormClass As clsForm
memory. As long as the variable
exists in memory, our class
continues functioning.
Static is a keyword that
declares a variable that doesn’t
get destroyed when a subroutine
ends; thus, we can use a static
variable to keep our class In
memory as long as our project is
running.
New creates an instance of Set FormClass = New clsForm
our class which we assign to our
static variable for safe keeping.
We assign our user form Set FormClass.UserForm = frmTest
frmTest to our class UserForm
property. At this point, our class
will now handle frmTest’s events
for us.
All that remains to do is show frmTest.Show False
our user form. End Sub

© 2015 Craig Hatmaker Page 4


clsForm
clsForm.UserForm – Set Property
Setting this property invokes a process to setup all controls for this userform.
In modMain.Launch we set Public Property Set UserForm(ByVal oUserForm As Object)
clsForm’s UserForm property to ' Description:Attach Userform to Class
frmTest. This is the code that runs ' Inputs: oUserForm User form
when that happens. ' Outputs: *None
' Requisites: Routines Me.Setup
This code block is a standard ' Example: *None
BXL documentation block. For ' Note! Object type "UserForm" does not expose all form properties.
' Examples include: Top, Left, Width, Height, and Caption.
more on BXL standards see BXL ' A generic object will. But a generic object does not expose
Coding Best Practices ' any events. So to expose events AND all properties
' we pass the user form as a generic "Object" (for properties)
' and assign it to an MsForms.UserForm object (for events).

' Date Ini Modification


' 06/21/13 CWH Initial Programming

' Declarations
Const cRoutine As String = ".Set UserForm"

For more on BXL error ' Error Handling Initialization


On Error GoTo ErrHandler
handling see BXL Error Handling
As mentioned earlier this ' Initialize Variables
If dicCtrls Is Nothing Then _
class creates several instances of Set dicCtrls = CreateObject("Scripting.Dictionary")
itself, one instance for each
userform control. To hold these
instances, I use a dictionary
object. dicCtrls is declared at the
top of this class making it a
“global” variable for all routines in
this class. We will learn more
about this in clsForm.Setup.
As mentioned earlier, ' Procedure
Set oForm = oUserForm
userform objects expose events Set oMsForm = oForm
but not properties and generic Setup oForm
objects expose properties but not
events. So to take full control
over a userform, we store the
userform in both object types
before call clsForm.Setup.
For more on BXL error ErrHandler:
Select Case Err.Number
handling see BXL Error Handling Case Is = NoError: 'Do nothing
Case Else:
Select Case DspErrMsg(cModule & "." & cRoutine)
Case Is = vbAbort: Stop: Resume 'Debug mode - Trace
Case Is = vbRetry: Resume 'Try again
Case Is = vbIgnore: 'End routine
End Select
End Select

End Property

© 2015 Craig Hatmaker Page 5


clsForm

clsForm.Setup
This initializes all control colors, creates instance of this class object for each control, and positions the userform in the center of
the screen.
This code block is a standard Private Function Setup(ByVal oForm As Object) As Boolean
BXL documentation block. For ' Description:Set backgrounds, fonts, and position
more on BXL standards see BXL ' Inputs: oForm User Form (as Object for Properties)
Coding Best Practices ' Outputs: Me Success/Failure
' Requisites: Classes clsCmd
' Example: ?Setup(oForm)

' Date Ini Modification


' 06/21/13 CWH Initial Programming

This code block is a standard ' Declarations


Const cRoutine As String = "Setup"
BXL declaration block. For more Dim oCtrl As Control
on BXL standards see BXL Coding Dim oPage As Control
Best Practices Dim clsCtrl As clsForm

For more on BXL error ' Error Handling Initialization


On Error GoTo ErrHandler
handling see BXL Error Handling
Here we add a background ' Procedure
With oForm
color or picture to the form. .BackColor = Color(crForm)
If Not IsNull(Color(crSkin)) And Not Color(crSkin) Is Nothing Then
.Picture = Color(crSkin)
.PictureAlignment = 2 'fmPictureAlignmentCenter
.PictureSizeMode = IIf(.Height * 34 < .Picture.Height And _
.Width * 34 < .Picture.Width, 0, 1)
End If
.ForeColor = Color(crFormFont)
End With
Next we turn our attention to For Each oCtrl In oForm.Controls
With oCtrl
all controls on our userform. Select Case TypeName(oCtrl)
Most TextBoxes are white Case Is = "TextBox"
If .Name = "txtErrMsg" Then
with black fonts. Most BXL forms .BackStyle = fmBackStyleTransparent
also have an error message .ForeColor = Color(crFormFont)
TextBox which uses a transparent Else
.BackColor = Color(crLight)
background so as to not visually
.ForeColor = Color(crLightFont)
confuse it with input TextBoxes. End If

This assigns ComboBoxes Case Is = "ComboBox", "ListBox"


.BackColor = Color(crLight)
white backgrounds and black .ForeColor = Color(crLightFont)
fonts.
This assigns Command- Case Is = "CommandButton"
Set clsCtrl = New clsForm
Buttons to new instances of this Set clsCtrl.CommandButton = oCtrl
class so the new instances can Set dicCtrls(oCtrl.Name) = clsCtrl
handle mouseover events and .BackColor = Color(crButton)
.ForeColor = Color(crButtonFont)
turn glowing on.

© 2015 Craig Hatmaker Page 6


clsForm
This assigns Frames to new Case Is = "Frame"
Set clsCtrl = New clsForm
instances of this class so they can Set clsCtrl.Frame = oCtrl
handle mouseover events turning Set dicCtrls(oCtrl.Name) = clsCtrl
glowing effects off for any .BackColor = Color(crForm)
.ForeColor = Color(crDark)
CommandButtons or Images in
the frame.
This assigns Image Controls Case Is = "Image"
Set clsCtrl = New clsForm
to new instances of this class so Set clsCtrl.image = oCtrl
the new instances can handle Set dicCtrls(oCtrl.Name) = clsCtrl
mouseover events and turn .BackColor = Color(crButton)
'.ForeColor = Color(crButtonFont)
glowing on.
.BackStyle = 0 'Transparent
This assigns TabStrips the Case Is = "TabStrip"
.BackColor = Color(crForm)
UserForm’s colors. .ForeColor = Color(crDark)
This assigns MultiPage Case Is = "MultiPage"
Set clsCtrl = New clsForm
Controls to new instances of this Set clsCtrl.Multipage = oCtrl
class so the new instances can Set dicCtrls(oCtrl.Name) = clsCtrl
handle mouseover events and .BackColor = Color(crForm)
.ForeColor = Color(crDark)
turn glowing off for any
For Each oPage In .Pages
CommandButtons or Images in oPage.BackColor = Color(crForm)
the frame. oPage.ForeColor = Color(crFormFont)
If Not IsEmpty(Color(crSkin)) Then
This also MultiPage controls oPage.Picture = Color(crSkin)
and their Pages the Userform’s End If
Next
colors.
This assigns SpinButtons and Case Is = "SpinButton", "ScrollBar"
.BackColor = Color(crButton)
ScrollBars button colors without .ForeColor = Color(crButtonFont)
glow effects.
This assigns Labels, Case Is = "Label", "CheckBox", _
"OptionButton", "ToggleButton"
CheckBoxes, OptionButtons, and .BackStyle = fmBackStyleTransparent
ToggleButtons the UserForm’s .BackColor = Color(crForm)
colors. .ForeColor = Color(crFormFont)
End Select
End With
Next

This positions the UserForm oForm.StartUpPosition = 3 'Windows Default


With Application
in the center of the screen when oForm.Left = .Left + .Width / 2 - oForm.Width / 2
it starts up. oForm.Top = .Top + .Height / 2 - oForm.Height / 2
End With

For more on BXL error ErrHandler:


Select Case Err.Number
handling see BXL Error Handling Case Is = NoError: 'Do nothing
Case Else:
Select Case DspErrMsg(sRoutine)
Case Is = vbAbort: Stop: Resume 'Debug mode - Trace
Case Is = vbRetry: Resume 'Try again
Case Is = vbIgnore: 'End routine
End Select
End Select

End Function

© 2015 Craig Hatmaker Page 7


clsForm
Appendix
Terms and Jargon
BXL Beyond Excel – My trademark.
XL Excel – Microsoft’s trademark.

Additional References and Documentation


References are now online at: https://sites.google.com/site/beyondexcel/home/references

© 2015 Craig Hatmaker Page 8

You might also like