I want to center only the 1st entry in a listbox; all other entries are left justified.
Printable View
I want to center only the 1st entry in a listbox; all other entries are left justified.
Centre with respect to what ?
The width of the ListBox or the longest entry in the ListBox ?
Well since it's the 1st entry which implies that when it is added to the listbox there are no other entries in the listbox to compare to so it means center with respect the the width of the listbox.
Used a fixed font and determine how many characters the list can hold then pad with spaces to get the desired position or if you don't want use a fixed font then you will need to determine how wide the text is and pad with spaces. Would be easiest with a fixed font but either will work, of course using the fixed font will only be exact on words with an even number of letters in them. Odds will be off by 1/2 a character space.
Something to play with:
Code:Private Sub CentreFirstItem(lst As ListBox)
Dim strData As String
strData = lst.List(0)
lst.List(0) = Space(lst.Width \ (16 * lst.FontSize)) & Trim$(strData)
End Sub
Why the 16? What does that number mean?
I almost picked it out of thin air ! I got the idea from this: http://www.dreamincode.net/forums/to...reen-size-vb6/ where it discusses changing the Font size. I guess you'll have to 'play' with the value to get it to work with the characters you're likely to use. I don't think it's anything like a universal solution.
Have you considered a FlexGrid instead? Nothing says you can't use one with a single column in it. Plus a "heading" can be put into a fixed row at the top, or if you still want it you can set alignment, font, etc. cell by cell in non-fixed rows.
No need for funky tricks like using a monospaced font and padding with spaces.
Or, if you are insistent on a listbox, you can place an invisible label on the form, set it's autosize property to true and use the same fixed font as the listbox, set the label to one character long. Then you can find the width of one character. And, as you know the width of the listbox, to find out how many spaces to add to the beginning of the text on the first line so it will be centered should be a cinch.
To find the width of one character, do this:
Label1.Font = List1.Font 'label1 is an invisible autosize label
Label1.Caption = "A" 'one letter wide
letterWidth = Label1.Width 'find the width of one letter (same as label1.width)
You can try this...it gets it 'pretty close' to center (within a half width of a one character):
as posted above....label1 and list1 have the same fixed font. Label1 can be invisible, but must be set to autosize.
Private Sub Form_Load()
Dim letterWidth As Double, x As Integer, centerListBox As Integer, maxnumLettersInListbox As Integer
Dim numSpacesNeeded As Integer, myText As String
letterWidth = Label1.Width
centerListBox = List1.Width / 2
maxnumLettersInListbox = List1.Width / letterWidth
List1.AddItem ("Hello")
List1.ListIndex = 0
numSpacesNeeded = (maxnumLettersInListbox - Len(List1.Text)) / 2
For x = 1 To numSpacesNeeded
myText = " " & myText
Next x
myText = myText & List1.Text
List1.RemoveItem (0)
List1.AddItem (myText)
List1.AddItem ("Good bye, ya'll")
End Sub
EDIT: PS----this ASSUMES the first line is shorter than the width of the listbox.
I'm curious as to why there is a wish to do this in the first place? I can only assume that the first item is meant to look like a header for the rest of the list? Or is, in some other way, behaviourally different? That being the case, consider whether there is not a better way to represent this, or as dilettante suggests, a better control for the job. Trying to calculate the central position in ways that have been suggested here means making a lot of assumptions about the user's environment...
Does it have to be centered or can you indent?
E.g
Code:Me.List1.AddItem Space(10) & "Indented Text"
The text will always be one of the following:
Server 1
'
'
Server 9
Server 10
'
'
Server 99
Server 100
'
'
Server 255
Using MS Sans Serif Regular @ 8 points
I tried padding spaces but couldn't get it to come out right
Yes, it is for appearance only and these are header like labels for the rest of the entries
EDIT: BTW the width of the listbox is 120 (Form is in Pixels)
hmm not sure why this would be hard if it will only contain Server #
Code:Private Sub Form_Load()
Dim i As Integer
'List1.Width = 120 * IIf(Me.ScaleMode = vbPixels, 1, 15)
For i = 0 To 999
List1.AddItem IIf(i > 9, IIf(i > 99, Space(7), Space(8)), Space(9)) & "Server " & i
Next
End Sub
Thanks, Max, that is perfect. I didn't know about IIF
Or even
List1.AddItem Space$(9 - Len(CStr(i))) & "Server " & i
Nice