Link to home
Start Free TrialLog in
Avatar of Massimo Scola
Massimo ScolaFlag for Switzerland

asked on

Change background color of a control on Enter/Exit in frame

Hello

I have a question about the background property of controls.

I've got a userform with various controls. To make the userform look nice, I put the controls into frame containers. The code I have works when the user enters the control but not when he/she exits it. When the userform is closed, I get an error message. Here is the code:

Private Sub BackColorSet()
    If TypeOf Me.ActiveControl Is Frame Then
        Me.Controls(ActiveControl.Name).ActiveControl.BackColor = RGB(255, 0, 0)
    Else
        Me.ActiveControl.BackColor = RGB(255, 0, 0)
    End If
End Sub

Private Sub BackColorRemove()
    If TypeOf Me.ActiveControl Is Frame Then
        Me.Controls(ActiveControl.Name).ActiveControl.BackColor = RGB(255, 255, 255)
    Else
        Me.ActiveControl.BackColor = RGB(255, 255, 255)
    End If
End Sub

Private Sub TextBoxFrame1_Enter()
BackColorSet
End Sub

Private Sub TextBoxFrame1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
BackColorRemove
End Sub 

Open in new window


Any ideas why this doesn't work?

Massimo
UF.PNG
userform.xlsm
Avatar of als315
als315
Flag of Russian Federation image

When you enter form, ActiveControl also is form. You should also store last control and set in to White. Look at sample
userform.xlsm
Avatar of Massimo Scola

ASKER

OK, now I understand. So the textbox and the form are ActiveControl.

The code works only if there is 1 control in the form.
If there are several controls, it won't work.
How do I store two controls with your code?
userform3.xlsm
UF2.PNG
You can see in sample, that it is not very good idea - to have many controls in one frame in your case. Sometimes (I think it is one of many Excel's bugs) you have no enter event in control, but you have this event in frame. It is working, but sometimes.
userform3.xlsm
Sorry for the late reply

As your code works perfectly well in text and comboboxes , it has some issues with the listbox.

Although the color of the listbox is changed when the focus is set to this control, the value is lost when the user exits the control. I tried the following code:

Private Sub ListBoxFrame3_Enter()
BackColorSet "ListBoxFrame3"
End Sub

Private Sub ListBoxFrame3_Click()
value = Me.ListBoxFrame3.value
End Sub

Private Sub ListBoxFrame3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Me.ListBoxFrame3.value = value
End Sub

Open in new window

I used a module variable called value, which is a string.The value of the listbox is not set when the user leaves this control. Any ideas?
userform-listbox.xlsm
There are different events: Frame enter and listbox enter. Sometimes you have no listbox enter event, only frame enter. I think it is an Excel problem and you can't do anything with it. The only way - do not use frames with multiple controls. Try to use labels (look at sample). You can see - it is working without any change in code. The only difference with yours sample - frame was changed to label
userform-listbox.xlsm
I had heard of the problems with frames before, but not in the context with controls. It was about performance and that not using frames could improve the rendering/opening of userforms.

I tried your example (thanks a lot) but when a listbox item is selected, the value is still cleared once the listbox's focus is lost. Or am I missing something?
I also tried this code and it still won't work:

 Private Sub Listbox_Enter()
Me.Listbox.BackColor = vbRed
End Sub

Private Sub Listbox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Me.Listbox.BackColor = vbWhite
End Sub

Open in new window

I'm not sure in expected result. Can you explain task? Value from listbox is stored in variable. You can put it into cell also.
userform-listbox.xlsm
I'd like to keep the item selected in the listbox.

The problem is that, as soon as I set the focus to another control, the value is deselected. I thought that this could be resolved by putting the value (event: onclick) into a variable. When the user exits the control, I would set the value of the listbox based on the value of the variable. But that doesn't seem to work.

Is there a way to keep an item in the listbox selected when the user changes the focus to another control? I don't understand why this simple task doesn't work on this form.
ASKER CERTIFIED SOLUTION
Avatar of als315
als315
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi

That's a pitty but maybe also a limitation of the VBA userforms. It's important for the user to know in which control the cursor is located. That's why I think that changing the border color is ok too.

Thanks for your help.

Massimo