Solved

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

Posted on 2013-05-24
11
469 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 39

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 39

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 39

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 39

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 39

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dropbox in Windows Server 2008 4 33
Left trim cells in column A Excel vba 2 35
And OR formula 5 25
Merging spreadsheets 8 46
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
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…

831 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