Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-05-24
11
Medium Priority
?
497 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 

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 2000 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

Industry Leaders: 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!

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

718 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