Listbox Height Question

N

Norm Lundquist

This is a follow-up to a previous question about How to set a listbox "Top".
(It should have been HEIGHT!)

The following code is in the Initialize event of a user form:
ListBox1.Height = Count * 12 + 2
' For some reason, we must access the height.
MsgBox "ListBox1.Height from init = " & ListBox1.Height, vbInformation,
"List Box Height"
' t = ListBox1.Height

"Count" is the number of items that has been loaded into ListBox1. For some
reason, setting the height does not work unless it is accessed - like in the
"MsgBox" line. I tried setting t = ListBox1.Height, but that does not work.

The height of the listbox is either done correctly when the MsgBox statement
is used; OR it is set to 49.2.

Any ideas on what's happening???

THANK YOU!
 
J

JLGWhiz

The ListBox1.Height = Count * 12 + 2 is apparently counting the number of
items and multiplying by 12 points which would be the font row height, plus 2
points so that it will all display. You do not need the Count to adjust the
height. You can set the height to any number or points that you want with:

ListBox1.Height = 150

sets the height to 150 points. Same syntax for width.

With the code you posted, the list box will adjust itself based on the
number of items, so that it neither hides items nor leaves a big empty space.
 
N

Norm Lundquist

I wish it was doing that! When I step thru the code, it works fine. But
when I run it, the height is set to 49.2 and never changes!! The only times
it works is: 1) when I step thru the code and debug ListBox1.Height; or 2)
when I use the MsgBox statement. Only under those two cases does it work
correctly. All other times the height is set to 49.2.

Norm
 
J

JLGWhiz

Can you post the entire initialize code? Maybe the problem lies in another
part that I can't see. For instance, where does Count get its value?
ListIndex?
 
N

Norm Lundquist

The listbox top is 96, ColumnCount=2, height=50, left=30,
locked=false,MultiSelect=2, width=224.

The two buttons under it are height=24, locked=false, top=162.

Here's the entire code:

Option Explicit


Private Sub UserForm_Initialize()
Dim i As Integer
Dim Count As Integer
Dim t As Double

On Error GoTo 0

' Load the listbox with 6 test values.
ListBox1.Clear
Count = 0
For i = 1 To 6
ListBox1.AddItem
ListBox1.Column(0, Count) = i
ListBox1.Column(1, Count) = "Test case #" & i
Count = Count + 1
Next i

' Following is the line that I can't figure out what's going on!
' In debug mode, the height gets set to 74.
' But when run normally, it remains 49.2.

' By using debug or the MsgBox line that follows, then AND ONLY THEN
does it work!

ListBox1.Height = Count * 12 + 2
' For some reason, we must access the height.
' MsgBox "ListBox1.Height from init = " & ListBox1.Height, vbInformation,
"List Box Height"

cmdPrint.Top = 96 + ListBox1.Height + 16
cmdClose.Top = 96 + ListBox1.Height + 16
ufJanelPrintRange.Height = 96 + ListBox1.Height + 16 + cmdClose.Height +
16 + 20
ufJanelPrintRange.StartUpPosition = 2

'MsgBox "Form.Height = " & ufJanelPrintRange.Height & vbNewLine & _
' "Listbox.Height = " & ListBox1.Height & vbNewLine & _
' "Button.Top = " & cmdPrint.Top & vbNewLine & _
' "Count = " & Count, vbInformation, "Form Height"
End Sub


Private Sub cmdPrint_Click()
MsgBox "Form.Height = " & ufJanelPrintRange.Height & vbNewLine & _
"Listbox.Height = " & ListBox1.Height & vbNewLine & _
"Button.Top = " & cmdPrint.Top, vbInformation, "Form Height"
End Sub


Private Sub cmdClose_Click()
Unload Me
End Sub
 
J

JLGWhiz

Try modifying the line to this:

ListBox1.Height = (Count * 12) + 2

If that does not work, maybe adding a short delay will. Try this and then
repost if it still won't set to 74. It has to be a timing problem.
 
N

Norm Lundquist

I found the problem. The "IntegralHeight" was set to True!! For some
reason, the height cannot be changed "right away". It wants to show full
rows, so that's why it was changing the height from 50 down to 49.2. Then it
would let VBA change it until a MsgBox statement was issued or some other
code! (I still don't know why and under what conditions it would change.)

But after changing the IntegralHeight to False, then I could change it just
fine. I should say that it wasn't being changed!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top