Controling colors of controls in userform

TryingToLearn

Well-known Member
Joined
Sep 10, 2003
Messages
730
I have a userform with 24 text and combo boxes. Aside from using the enter and exit events, is there an easy way to have the active text/combo box be highlighted in a color?

TIA
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
TryingToLearn said:
I have a userform with 24 text and combo boxes. Aside from using the enter and exit events, is there an easy way to have the active text/combo box be highlighted in a color?

TIA

Hi TryingToLearn,

Sorry, as far as I know, there is NO easy way.
I made a class module for you. Please try this code.
If you do not understand, please ask me anything.

Code:
'--Userform1 modile Code

Option Explicit

Private WithEvents objForm As Class1

Private Sub UserForm_Initialize()
    Set objForm = New Class1
End Sub

Private Sub UserForm_Activate()
    If TypeName(ActiveControl) = "ComboBox" Or _
       TypeName(ActiveControl) = "TextBox" Then
        ActiveControl.BackColor = &HC0E0FF
    End If
    objForm.CheckActiveCtrl Me
End Sub

Private Sub objForm_GetFocus()
    ActiveControl.BackColor = &HC0E0FF
End Sub

Private Sub objForm_LostFocus(ByVal strCtrl As String)
    Me.Controls(strCtrl).BackColor = &HFFFFFF
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Set objForm = Nothing
End Sub

'--Class Module Code (named Class1)
Public Event GetFocus()
Public Event LostFocus(ByVal strCtrl As String)
Private strPreCtr As String

Public Sub CheckActiveCtrl(objForm As MSForms.UserForm)
    With objForm
        If TypeName(.ActiveControl) = "ComboBox" Or _
           TypeName(.ActiveControl) = "TextBox" Then
            strPreCtr = .ActiveControl.Name
            On Error GoTo Terminate
            Do
                DoEvents
                If .ActiveControl.Name <> strPreCtr Then
                    If TypeName(.ActiveControl) = "ComboBox" Or _
                       TypeName(.ActiveControl) = "TextBox" Then
                        RaiseEvent LostFocus(strPreCtr)
                        strPreCtr = .ActiveControl.Name
                        RaiseEvent GetFocus
                    End If
                End If
            Loop
            End If
        End With
Terminate:
    Exit Sub
End Sub
 
Upvote 0
First - thank you for the response.

Much easier to paste working code and learn from it than to try to understand the help files but...

After pasting your code, I came up the the following Compile Errors in the class module (named Class1)


Public Event GetFocus()
Public Event LostFocus(ByVal strCtrl As String)

Highlights the word EVENT and returns
Expected: identifier




RaiseEvent LostFocus(strPreCtr)
RaiseEvent GetFocus

Hightlights RaiseEvent and returns:
Expected: line number or label or statement or end of statement


Could this be because I'm in XL 97?
 
Upvote 0
Hi,

Now I don't have 97, so I'll try it on 97 tomorrow.

Or please try and modify anyone who has 97. :D
 
Upvote 0
Hi TryingToLearn, :p

Here are procedures for XL97 version.
Note:Before execute this code, change the letterUserform name in the "Public Sub GetFocus()" line to suit your needs:


Code:
'--- Code for an UserForm module---
Option Explicit

Private arrTxb() As New Class1
Private arrCmb() As New Class1

Private Sub UserForm_Activate()
    GetFocus
End Sub

Private Sub UserForm_Initialize()
    Dim obj As MSForms.Control, i As Long, j As Long
    For Each obj In Me.Controls
        If TypeName(obj) = "TextBox" Then
            ReDim Preserve arrTxb(i)
            Set arrTxb(i) = New Class1
            arrTxb(i).SetTextBox obj
            i = i + 1
        End If
        If TypeName(obj) = "ComboBox" Then
            ReDim Preserve arrCmb(j)
            Set arrCmb(j) = New Class1
            arrCmb(j).SetComboBox obj
            j = j + 1
        End If
    Next
End Sub

'--- Code for a Class Module Named Class1 ---
Option Explicit

Public WithEvents cls_txb As MSForms.TextBox
Public WithEvents cls_cmb As MSForms.ComboBox

Sub SetTextBox(ByVal txb As MSForms.TextBox)
    Set cls_txb = txb
End Sub

Sub SetComboBox(ByVal cmb As MSForms.ComboBox)
    Set cls_cmb = cmb
End Sub

Private Sub cls_txb_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
                                             ByVal X As Single, ByVal Y As Single)
    LostFocus objPre
End Sub

Private Sub cls_cmb_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
                                               ByVal X As Single, ByVal Y As Single)
    LostFocus objPre
End Sub

Private Sub cls_txb_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
                                         ByVal Shift As Integer)
    CallLoastFocus KeyCode, cls_txb
End Sub

Private Sub cls_cmb_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
                                          ByVal Shift As Integer)
    CallLoastFocus KeyCode, cls_cmb
End Sub

Private Sub CallLoastFocus(ByVal KeyCode As Long, ByVal MSFCtrl As MSForms.Control)
    If KeyCode = 13 Or KeyCode = 9 Or KeyCode = 40 Or KeyCode = 38 Then
        LostFocus MSFCtrl
    End If
End Sub

'--- Code for a Standard Module ---
Option Explicit

Public objPre As MSForms.Control

Public Sub LostFocus(ByVal obj As MSForms.Control)
    If TypeName(obj) = "TextBox" Or TypeName(obj) = "ComboBox" Then
        obj.BackColor = &HFFFFFF
    End If
    Application.OnTime Now, "GetFocus"
End Sub

Public Sub GetFocus()
    Dim objActive As MSForms.Control
    'Change Userform Name to suit.
    Set objActive = UserForm1.ActiveControl
    If TypeName(objActive) = "TextBox" Or _
       TypeName(objActive) = "ComboBox" Then
        objActive.BackColor = &HC0E0FF
    End If
    Set objPre = objActive
    Set objActive = Nothing
End Sub
 
Upvote 0
Whats up Tryingtolearn,
I don't know if this is the problem but, if you look at Colo's first code closely, it is broken into 2 sections. The first needs to be placed in the Userform module "'--- Code for an UserForm module---". The second needs to be placed in a Class module "Class Module Code (named Class1)".

I tried it and it works great. When you click on either the Combobox or the Textbox, the background is highlighted.

HTH
Noir
 
Upvote 0
Didn't have a chance to try new code before now (silly thing called sleep keeps interferring).

Works VERY NICELY! (y)
It's going to take me some time to absorb how it's done, but for now gets it working for me! I appreciate the extra work in making it compatible for xl97



I was under the impression that a public declaration in a class module would allow a variable to be used in ANY module and would pass the current value of it.
If I have in class1 :

Public WithEvents UFx As MSForms.UserForm

I would expect it to be available in the GetFocus sub so I can declare the current userform being activated. Doesn't seem to be the case for me sooooo.

How can I change the code to have this highlighting occur in ALL the userforms in my project?

TIA


Noir - it actually is broken down to 3 areas:

1. userform code
2. class module code
3. standard module code

so how'd ya get it to work???? :wink:
 
Upvote 0
Tryingtolearn,
I was referring to Colo's "first" code suggestion. It only has two areas. Since his first suggestion worked for me, i didn't even try the second.

Noir
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,628
Members
449,240
Latest member
lynnfromHGT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top