Solved

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

Posted on 2013-05-24
11
471 Views
Last Modified: 2013-05-27
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
Comment
Question by:Massimo Scola
  • 6
  • 5
11 Comments
 
LVL 40

Expert Comment

by:als315
ID: 39195304
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
 

Author Comment

by:Massimo Scola
ID: 39195455
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
 
LVL 40

Expert Comment

by:als315
ID: 39195601
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

Author Comment

by:Massimo Scola
ID: 39196253
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
 
LVL 40

Expert Comment

by:als315
ID: 39196422
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
 

Author Comment

by:Massimo Scola
ID: 39196571
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
 

Author Comment

by:Massimo Scola
ID: 39196575
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
 
LVL 40

Expert Comment

by:als315
ID: 39196822
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
 

Author Comment

by:Massimo Scola
ID: 39196837
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
 
LVL 40

Accepted Solution

by:
als315 earned 500 total points
ID: 39197394
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
 

Author Closing Comment

by:Massimo Scola
ID: 39199113
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
question about excel functionality 3 33
A Table within a User Form 24 48
Add checkboxes based on table column 10 30
vba copy paste previous line 3 16
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question