Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • Last Modified:

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
0
Massimo Scola
Asked:
Massimo Scola
  • 6
  • 5
1 Solution
 
als315Commented:
When you enter form, ActiveControl also is form. You should also store last control and set in to White. Look at sample
userform.xlsm
0
 
Massimo ScolaAuthor Commented:
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
0
 
als315Commented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Massimo ScolaAuthor Commented:
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
0
 
als315Commented:
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
0
 
Massimo ScolaAuthor Commented:
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?
0
 
Massimo ScolaAuthor Commented:
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

0
 
als315Commented:
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
0
 
Massimo ScolaAuthor Commented:
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.
0
 
als315Commented:
You should have ControlSource property filled if you like to see listbox value always. But there is other problem - it is also done with background and when you change background for whole control, selection becomes invisible.
May be better to use BorderColor for listbox
userform-listbox.xlsm
0
 
Massimo ScolaAuthor Commented:
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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now