PDA

View Full Version : [SOLVED:] List box looses Blue 'highlight' when changing forecolor



mobucl
05-05-2016, 12:47 PM
Hi,

This is my first post so i hope i explain things correctly. I am using excell 2010. I have a problem with an application im trying to write which i cant figure out. I have simplified the code to demonstrate below:



Private Sub UserForm_Activate()
ListBox1.AddItem "bob"
ListBox1.AddItem "bill"
ListBox1.ListIndex = 0
ListValue = ListBox1.Value


ListBox1.ForeColor = RGB(0, 0, 0) ' blue highlight lost
ListBox1.ListIndex = 0 'this doesnt work!
ListValue = ListBox1.Value
End Sub


Here a ListBox is created in a UserForm. some values are added to it and then the list index is set and my selection is highlighted in blue....so far no problem.

However if i then use the forecolor command i loose the blue highlight....but the 'value' is still correct (ListValue variable checks this). ok no problem i thought....ill just use ListIndex again, but this doesnt work. The only way i can get it to work is to change the ListIndex to a different value and back again:


Private Sub UserForm_Activate()
ListBox1.AddItem "bob"
ListBox1.AddItem "bill"
ListBox1.ListIndex = 0
ListValue = ListBox1.Value


ListBox1.ForeColor = RGB(0, 0, 0) ' blue highlight lost
ListBox1.ListIndex = 1 'change the index
ListBox1.ListIndex = 0 'change it back again..blue is back!


ListValue = ListBox1.Value
End Sub


The same thing happens when i try using:


ListBox1.selected(0) = True

Can someone please let me know if there is another way to do this? I just want to be able to modify the forecolor and keep the blue highlight in place for the user to know what is selected (or at least not have to keep changing the ListIndex back and forth by a single number to do this). Is this problem with ListIndex/Selected a bug?

Any help would be much appreciated!

Matt

Leith Ross
05-05-2016, 02:07 PM
Hello Matt,

Your code work fine for me. I am running Windows 7 with Office 2010. Maybe you should upload a copy of your workbook.

SamT
05-05-2016, 05:16 PM
Why do you want the control to remain highlighted after the User uses it?

mobucl
05-05-2016, 11:25 PM
Hi Leith Ross, Thanks for replying. I have attached the workbook. If I run it as it is (with the lines commented out) i loose the blue highlight and ListBox1.ListIndex = 0 does not reset it. If you uncomment ListBox1.ListIndex = 1 then it does work.

Hi SamT, This is just an example code to make the problem clear. In my application the user changes a combobox selection which resets/changes the availablity of lots of list boxes to let them know variables that can be changed with the current selection. Changing the forecolor to 'grey out' what cant be changed is my aim, however i want the user to still be able to visualy identify that their selection in the greyed out (or reactivated) box is still valid.

Thanks

GTO
05-06-2016, 03:28 AM
Hi mobuci,

At least for me, where/when you change the ForeColor seems to be the trick:



Private Sub UserForm_Activate()
Me.ListBox1.ForeColor = &H0&
ListBox1.AddItem "bob"
ListBox1.AddItem "bill"
ListBox1.ListIndex = 0
ListValue = ListBox1.Value
End Sub


Hope that helps,

Mark

SamT
05-06-2016, 06:38 AM
availablity of lots of list boxes to let them know variables that can be changed with the current selection.

Blue is available, Enabled = False can't be changed by User

Sub Checkbox1_Change()
With Me
If .CheckBox1 = X Then
.CheckBox2.ForeColor = Blue
.Checkbox3.Enabled = False
Else
.CheckBox2.Enabled = False
.Checkbox3.ForeColor = Blue
End If
End With
End Sub

mobucl
05-10-2016, 03:02 AM
Hi GTO,

Sorry i have been away for a few days. My problem can be shown by repeating 2 lines in your code:


Private Sub UserForm_Activate()
Me.ListBox1.ForeColor = &H0&
ListBox1.AddItem "bob"
ListBox1.AddItem "bill"
ListBox1.ListIndex = 0
Me.ListBox1.ForeColor = &H0& 'redo forecolour
ListBox1.ListIndex = 0 'try and make blue selection box rehighlight!
End Sub


Now when you run this code you can see that the blue selection colour has disappeared from 'bob' when Forecolor is re-applied. However if i change ListBox1.ListIndex from 0 to 1 and back again it works:


Private Sub UserForm_Activate()
Me.ListBox1.ForeColor = &H0&
ListBox1.AddItem "bob"
ListBox1.AddItem "bill"
ListBox1.ListIndex = 0
Me.ListBox1.ForeColor = &H0&
ListBox1.ListIndex = 1
ListBox1.ListIndex = 0 ' now this works
End Sub


thanks

mobucl
05-10-2016, 03:25 AM
Hi SamT,

I ahve been away for a few days so sorry for the late reply! Thanks for your comment. I am not trying to allow the user to change the enabled status the program will do this. I have made another example to try and highlight this and the problem i have.

In the attached document you can see that When initialised ListBox1.ListIndex = 0 selects 'bob' and it is highlighted.

The user can then select 'Jane' or 'Sally' if the use selects Sally the ListBox is disabled and foreground colour becomes grey to indicate that this option is not available. But when the user selects 'Jane' it is enabled, however the blue highlighting indicating 'bob' was previously selected has disappeared. Even re-calling ListBox1.ListIndex = 0 does not reapply it (as mentioned before changing from 0 to 1 and then back again does).

I would like to be able to change the foreground colour and reapply the blue line highlight without resorting to a work around.

I hope that make sense?

Thanks

GTO
05-10-2016, 04:19 AM
Hi SamT,

... But when the user selects 'Jane' it is enabled, however the blue highlighting indicating 'bob' was previously selected has disappeared. Even re-calling ListBox1.ListIndex = 0 does not reapply it (as mentioned before changing from 0 to 1 and then back again does)...

FWIW, 're-calling' ListIndex = 0 is not changing anything as ListIndex is already 0. If you click on the list box below the two values (or tab to the list box), you can see that the first entry is selected with the little dotted line encircling it. Anyways, to your point, you are right in stating that the entry is not actually highlighted in blue though.

Would changing:




Private Sub UserForm_Activate()
ComboBox1.AddItem "Jane"
ComboBox1.AddItem "Sally"
ComboBox1.ListIndex = 0 '<---This changes the value of the combo box; hence, calling the box's
' change event before INIT is assigned the value 1. Of course we cannot
' yet set the value to 1, as we have not yet populated the ListBoxListBox1.AddItem "bob"
ListBox1.AddItem "bill"
ListBox1.ListIndex = 0 'BLUE SELECTION HIGHLIGHT APPLIED
ListValue = ListBox1.Value
INIT = 1
End Sub


To:



Private Sub UserForm_Activate()
ListBox1.AddItem "bob"
ListBox1.AddItem "bill"
ListBox1.ListIndex = 0 'BLUE SELECTION HIGHLIGHT APPLIED
ListValue = ListBox1.Value
INIT = 1
ComboBox1.AddItem "Jane"
ComboBox1.AddItem "Sally"
ComboBox1.ListIndex = 0
End Sub


Private Sub ComboBox1_Change()
If (ComboBox1.Value = "Sally") Then
ListBox1.Enabled = False
ListBox1.ForeColor = RGB(128, 128, 128)
ElseIf (ComboBox1.Value = "Jane") Then
ListBox1.Enabled = True
ListBox1.ForeColor = RGB(0, 0, 0)
If INIT = 1 Then
Debug.Print ListBox1.ListIndex
ListBox1.ListIndex = -1 ' As ListIndex is already 0, how about "deselecting" first?
ListBox1.ListIndex = 0
End If
End If
End Sub



...help?

Mark

mobucl
05-11-2016, 05:36 AM
Hi Mark,

I tried your suggestion and it works so thanks a lot for your help. Using ListIndex = -1 to deselect and then reselect after makes more sense than adding a value then taking it away.


Also for pointing out that 'recalling' ListIndex = 0 is not changing anything.

Still i find the behaviour of the blue highlighting strange. As you say nothing is changed - except the forecolor so why is the highlight lost when forecolor is changed?

Anyhow i can use your suggest to improve my application so thanks again (and also to SamT for his suggestions!)

Matt

GTO
05-11-2016, 06:20 AM
Hi Matt,
Thank you for the feedback, and I am sure on both Sam's and my part, you are most welcome. Happy coding:cloud9:

Mark