Results 1 to 18 of 18

Thread: [RESOLVED] How To: Center text in a listbox

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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 ?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    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.

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How To: Center text in a listbox

    Quote Originally Posted by Doogle View Post
    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.

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: How To: Center text in a listbox

    Quote Originally Posted by jmsrickland View Post
    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.

  9. #9
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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.

  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    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)

  11. #11
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    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.

  12. #12
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,272

    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

  13. #13
    Addicted Member Witis's Avatar
    Join Date
    Jan 2011
    Location
    VB Forums Online Freedom Mode: Operational
    Posts
    213

    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.

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  15. #15
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    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

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  17. #17
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,272

    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

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] How To: Center text in a listbox

    Nice


    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
  •  



Click Here to Expand Forum to Full Width