Excel VBA: Frame doesn't appear in front of ListBox

206 Views Asked by At

I have a Frame containing other controls, and I have ListBox. All of these are within a Userform. Initially, the Frame is invisible, and there is a button which toggles its visibility. I want the Frame and its contents to appear in front the ListBox, however, this doesn't happen.

I've tried bringing the Frame to the front and sending the ListBox to the back using the Order part of the Format menu, but that hasn't changed anything. I added.

'Bring it to the front
DatePickerFrame.ZOrder (0)
    
'Send the ListBox to the back
SearchResultsListBox.ZOrder (1)

to my Userform's initialisation, which led to this when toggling the visibility of the Calendar Frame on:

this when toggling the visibility of the Calendar Frame on

but also led to this when toggling the visibility of the Calendar Frame off:

this when toggling the visibility of the Calendar Frame off

I also cut and paste this code into the button that toggles the Calendar's visibility, but it led to the same behaviour.

2

There are 2 best solutions below

0
Notus_Panda On BEST ANSWER

For some reason (I'm yet to figure out why), when you turn something visible while it overlaps something else, the overlap remains..

Start:
enter image description here

When clicking the button:
enter image description here

When clicking it again (overlap remains):
enter image description here

The code I used to play around with that finally worked to show only the listbox when putting the frame back to invisible:

Private Sub CommandButton2_Click()
    Frame1.Visible = Not Frame1.Visible
    ListBox1.Visible = False 'this is absolutely necessary
    ListBox1.Visible = True
    Frame1.ZOrder (IIf(Frame1.Visible, 0, 1)) 'needs to come after making the listbox (in)visible.
    'ListBox1.ZOrder (IIf(Frame1.Visible, 1, 0)) 'not necessary since you want the frame to be in front
End Sub

If that's not the issue, let me know and I'll check some more :)

1
GSerg On

The VBA controls are mostly lightweight. They are not really windows, they are simply drawn on the form.

This visual artifact is a side effect of the MSForms library failing to repaint the surface of other lightweight controls after you hide a control. (They probably didn't think someone would want an overlap, and the form does clear its own background.)

Add Me.Repaint:

Private Sub CommandButton1_Click()
    DatePickerFrame.ZOrder 0
    DatePickerFrame.Visible = Not DatePickerFrame.Visible
    Me.Repaint
End Sub