Centering and sizing UserForm controls

J

John T Ingato

I would like to format my form on the fly so that it looks as professional
as possible.

I am passing an list array to a form and filling a listbox with the array
items. I would like to make the listbox just big enough to completely hold
the text. Plus, I would like to center the listbox horizontally in the
form.

I have a proceedure that finds the longest text length in the list and then
set the list box width to the length of the string * fontsize:

fontsize = Me.listbox.Font.Size


With Me.listbox
.Top = Label1.Height + (Label2.Height * 2)
.Height = ( Me.listbox.ListCount ) * fontsize
.Width = Len(longestString) * fontsize
.Left = (frmPromptUser.Width - listbox.Width) / 2
End With

But the listbox is not high enough and it well too wide. Now I realize that
the width of each letter in a string is not as many point wide as is is high
and I also realize that the sum of the points of each item does not take
into account the separation between the items. Also the listbox is close...
but not centered. I have tried combinations of setting the autosize( not
available with a listbox) and integralHeight with no luck.


Any suggestions?
 
A

Andy Pope

Hi John,

The listbox has a TextAlign property, which you could set to Center.

I would use a label control to determine the size of a particular item
in the listbox. Set the labels font attributes to be the same as the
listbox. Set the Wordwrap to false and the Autosize to True.
The remember the largest width whilst looping through and trying each
list item in the label control.

Post back if you need further assistance.

Cheers
Andy
 
J

John T Ingato

Excellent Thanks. I actually was trying that with a combobox not a label,
but I see how that would be the solution , as a label always adjusts itself
to fit the new caption.

Cheers to you
 

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