Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SIMPLE: Resizing a listbox to fit the items 1

Status
Not open for further replies.

perplexd

Programmer
May 9, 2002
154
US
Hi all,
I'm trying to resize a listbox (and its frame and form) to fit the items in the listbox...simple I'm sure, so I must be going about it in the wrong way. Can anyone point me in the right direction? The code I'm using at the moment is below...

Thanks in advance

With frm.List

For i = 0 To (.ListCount - 1)
If TextWidth(.List(i)) > NewWidth Then
NewWidth = TextWidth(.List(i))
End If
Next i

If NewWidth > .Width Then
WidthDiff = (NewWidth - .Width + 10)
frm.Width = frm.Width + WidthDiff
frm.fra.Width = frm.fra.Width + WidthDiff
.Width = (NewWidth + 10)
End If
End With
 
What is the .BorderStyle of the Form? If its not already sizable, then I would give that a shot. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
The form borderstyle is sizeable...

Its not just the form which isn't resizing, none of the objects are.
 
You might try using the .Move Method

If NewWidth > .Width Then
WidthDiff = (NewWidth - .Width + 10)
Me.Move Me.Left, Me.Top, (Me.Width + WidthDiff), Me.Height
Frame1.Move Frame1.Left, Frame1.Top, (Frame1.Width + WidthDiff), Frame1.Height
.Move .Left, .Top, (.Width + WidthDiff), .Height
End If


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Looking more closely...

Me.TextWidth = 2880 (in one case)
List.Width = 3975

Hence it doesn't resize. However the text is actually wider than the width of the box. It is as though they are working on two different scales...

Is this correct and if so how can I convert both items to use the same scale or what is the ratio between the two scales?

Thanks again.
 
The .TextWidth method uses the current font settings for the form. If the font settings for the ListBox are different, then you will have too different scales - one for the form, and one for the listbox. Set the font settings for the form, to be the same as the font settings for the listbox, at least during the loop when you're calling the .TextWidth method. You can save the font settings into temp variables, set the form to be the same as the listbox, execute the loop with .TextWidth, and then reset the form back to its normal settings.
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thank you CajunCenturion...exactly right!...It works perfectly now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top