|
-
Mar 9th, 2013, 12:58 PM
#1
[RESOLVED] How To: Center text in a listbox
I want to center only the 1st entry in a listbox; all other entries are left justified.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Mar 9th, 2013, 02:46 PM
#2
Re: How To: Center text in a listbox
Centre with respect to what ?
The width of the ListBox or the longest entry in the ListBox ?
-
Mar 9th, 2013, 03:15 PM
#3
Re: How To: Center text in a 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.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Mar 9th, 2013, 03:27 PM
#4
Re: How To: Center text in a 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.
-
Mar 9th, 2013, 03:42 PM
#5
Re: How To: Center text in a listbox
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
Last edited by Doogle; Mar 9th, 2013 at 04:01 PM.
-
Mar 9th, 2013, 04:42 PM
#6
Re: How To: Center text in a listbox
Why the 16? What does that number mean?
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Mar 9th, 2013, 06:11 PM
#7
Re: How To: Center text in a listbox
 Originally Posted by Doogle
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
I tried "Text1.Text". Centered perfectly
I tried XXXXX 1, XXXXX 10, XXXXX 100, XXXXX 1000. All left justified the same and not centerd. Does this have anything to do with the 16?
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Mar 10th, 2013, 06:07 AM
#8
Re: How To: Center text in a listbox
 Originally Posted by jmsrickland
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.
-
Mar 10th, 2013, 07:37 AM
#9
Re: How To: Center text in a listbox
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.
-
Mar 10th, 2013, 04:06 PM
#10
Re: How To: Center text in a listbox
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)
-
Mar 10th, 2013, 04:33 PM
#11
Re: How To: Center text in a listbox
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.
-
Mar 12th, 2013, 11:34 AM
#12
Re: How To: Center text in a 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...
If you don't know where you're going, any road will take you there...
My VB6 love-children: Vee-Hive and Vee-Launcher
-
Mar 12th, 2013, 02:51 PM
#13
Addicted Member
Re: How To: Center text in a listbox
Does it have to be centered or can you indent?
E.g
Code:
Me.List1.AddItem Space(10) & "Indented Text"
All men have an inherent right to life, the right to self determination including freedom from forced or compulsory labour, a right to hold opinions and the freedom of expression, and the right to a fair trial and freedom from torture. Be aware that these rights are universal and inalienable (cannot be given, taken or otherwise transferred or removed) although you do risk losing the aforementioned rights should you fail to uphold them e.g Charles Taylor; United Nations sources: http://www.un.org/en/documents/udhr/, http://www.ohchr.org/EN/Professional...ages/CCPR.aspx. Also Charles I was beheaded on the 30th of January of 1649 for trying to replace parliamentary democracy with an absolute monarchy, the same should happen to Dr Phil and Stephen Fry; source: http://www.vbforums.com/showthread.p...ute-Monarchism.
The plural of sun is stars you Catholic turkeys.
-
Mar 12th, 2013, 04:49 PM
#14
Re: How To: Center text in a listbox
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)
Last edited by jmsrickland; Mar 12th, 2013 at 05:01 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Mar 12th, 2013, 05:52 PM
#15
Re: How To: Center text in a listbox
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
-
Mar 12th, 2013, 06:18 PM
#16
Re: How To: Center text in a listbox
Thanks, Max, that is perfect. I didn't know about IIF
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Mar 12th, 2013, 07:22 PM
#17
Re: [RESOLVED] How To: Center text in a listbox
Or even
List1.AddItem Space$(9 - Len(CStr(i))) & "Server " & i
If you don't know where you're going, any road will take you there...
My VB6 love-children: Vee-Hive and Vee-Launcher
-
Mar 12th, 2013, 07:34 PM
#18
Re: [RESOLVED] How To: Center text in a listbox
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|