5

I've created a Userform in Excel VBA, on which there is an unbound Listbox that has its MultiSelect property set to Extended. When that listbox receives focus by any means other than clicking a list item, all items in that list appear with the dotted focus rectangle around them.

Here is some code that shows the phenomenon beside another listbox with MultiSelect set to Single for comparison. Create a Userform, put two Listboxes on it, and add the code to the form. When you launch the form, tab between listboxes to see what I've described.

Private Sub UserForm_Activate()
    ListBox1.MultiSelect = fmMultiSelectSingle
    ListBox2.MultiSelect = fmMultiSelectExtended

    Dim i As Integer
    For i = 1 To 15
        ListBox1.AddItem String(i, Chr(i + 64))
        ListBox2.AddItem String(i, Chr(i + 64))
    Next
End Sub

Is there a way to remove the focus rectangles or prevent their appearing?

Thanks,

5
  • The above property seems to do 'dotted' focus as well. Only if these features were freely avaialbe :) Out of curiosity, you are looking for perfection in the UI design or is there any other concern? I know the dotted lines are annoying, but there has to be something else...which matters to you.
    – bonCodigo
    Nov 14, 2012 at 18:08
  • The listbox works exactly as I intend it to. The extra lines are the only annoyance I'm wondering about. I just find it odd that multiple items (each list item) is marked as having keyboard focus. I'm pretty sure it's a drawing artifact. If there's no way to clean it up, aside from subclassing the control, then I can live with it.
    – Phil R
    Nov 14, 2012 at 18:20
  • I'm seeing odd behaviour with this if you set the ListIndex property after filling the ListBox - i.e. ListBox2.ListIndex = ... Using -1 and 0 puts the focus on everything; 1, 2 and 3 put the focus on a single line and 4 or higher puts the focus on everything. This is with Excel 2003 though. Also different effects if the ListBox MultiSelect property is set in the properties pane in the UserForm design screen rather than via VBA
    – barrowc
    Nov 14, 2012 at 22:37
  • Now I'm seeing alternating behaviour from one run to the next with no changes to VBA or to the properties pane
    – barrowc
    Nov 14, 2012 at 22:46
  • Likewise they interchange or rather pass the focus to each other. The promising solution I saw on Web so far involved subclassing. if a highly reputed Stackian could open a bounty, we would see more traction :-)
    – bonCodigo
    Nov 15, 2012 at 5:24

1 Answer 1

2

I have experimented with your code in Excel 2010 and confirm your observation. If I create two list boxes, enter the code provided, start the form and press tab to focus on ListBox2, the dotted lines appear around all rows.

If I create the two list boxes as before, manually set ListBox2/Properties/Multiselect to 2 - fmMultiSelectExtended, run and tab to ListBox2 the nasty lines disapperar.

For me this is rather stable, the form now survives multiple window activation changes, jumpng back/forth etc.

don't ask me why ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.