vba userform resizing listbox height problem

User avatar
Cah
3StarLounger
Posts: 293
Joined: 26 Mar 2010, 10:53

vba userform resizing listbox height problem

Post by Cah »

I have the following code for a CorelDraw X6 userform:

Code: Select all

Private Sub UserForm_Initialize()

    'populate listboxes for each page
    h = 0
    
    For Each l In ActivePage.AllLayers
       
        If l.name <> "Grid" And l.name <> "Guides" And l.name <> "Desktop" And l.name <> "Document Grid" And l.name <> "TEMP" Then

            frmSetLayers.ListOn.AddItem l.name
            frmSetLayers.ListOff.AddItem l.name
            h = h + 15
            
        End If
    
    Next l
    
    MsgBox h

    frmSetLayers.ListOn.Height = h
    frmSetLayers.ListOff.Height = h

    h = h + 25

    frmSetLayers.cmdOK.Top = h
    
    h = h + 60
          
    frmSetLayers.Height = h

End Sub
This code creates two listboxes (ListOn and ListOff) populated with the layer names taken from the the active CorelDraw Page. The msgbox shows the height each listbox will be resized to. With the msgbox the form displays correctly but without it I'm getting weird issues. One or both of the listboxes (usually ListOn) fails to resize and instead shows scrollbars. I think it might be a graphics card issue. Has anyone else seen this kind of problem? What can I do to resolve it? Thanks.

User avatar
HansV
Administrator
Posts: 78631
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: vba userform resizing listbox height problem

Post by HansV »

I assume that this is the UserForm_Initialize event procedure for frmSetLayers. Does it make a difference if you refer to it as "Me" in the code?

Code: Select all

Private Sub UserForm_Initialize()
    'populate listboxes for each page
    h = 0

    For Each l In ActivePage.AllLayers
        Select Case l.Name
            Case "Grid", "Guides", "Desktop", "Document Grid", "TEMP"
                ' Skip these
            Case Else
                Me.ListOn.AddItem l.Name
                Me.ListOff.AddItem l.Name
                h = h + 15
        End Select
    Next l

    MsgBox h

    Me.ListOn.Height = h
    Me.ListOff.Height = h

    h = h + 25

    Me.cmdOK.Top = h

    h = h + 60

    Me.Height = h
End Sub
Best wishes,
Hans

User avatar
Cah
3StarLounger
Posts: 293
Joined: 26 Mar 2010, 10:53

Re: vba userform resizing listbox height problem

Post by Cah »

Your assumption is correct, but unfortunately using "Me" does not make any difference.

User avatar
HansV
Administrator
Posts: 78631
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: vba userform resizing listbox height problem

Post by HansV »

The code works OK for me in Word or Excel (after modifying it slightly for those applications of course), and I don't think the graphics card could cause this. Does it help if you insert a line

Code: Select all

    Me.Repaint
immediately above the End Sub?
Best wishes,
Hans

User avatar
Cah
3StarLounger
Posts: 293
Joined: 26 Mar 2010, 10:53

Re: vba userform resizing listbox height problem

Post by Cah »

Thanks, I should have mentioned that I tried Me.repaint. It didn't help. The only thing that does help is inserting the msgbox. I tired pausing the script at that point but that made no difference either. Perhaps it is a CorelDraw vba bug. If I have one listbox rather than two then I also don't have a problem.

User avatar
HansV
Administrator
Posts: 78631
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: vba userform resizing listbox height problem

Post by HansV »

Hmm, now I'm getting the problem in Excel too.
Try inserting a line

Code: Select all

    DoEvents
below the loop (i.e. below the line Next l), instead of the MsgBox line.
Best wishes,
Hans

User avatar
Cah
3StarLounger
Posts: 293
Joined: 26 Mar 2010, 10:53

Re: vba userform resizing listbox height problem

Post by Cah »

Thanks Hans, that appears to be working. Any idea as to why it is needed?

User avatar
HansV
Administrator
Posts: 78631
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: vba userform resizing listbox height problem

Post by HansV »

I assume that it's a timing problem. Displaying a message box or calling DoEvents gives VBA a moment to breathe and to catch up with the changes.
Best wishes,
Hans