Results 1 to 12 of 12

Thread: Right justify text in list box?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Right justify text in list box?

    It seems difficult to right justify text in a text box.

    Does anybody have some clues? I think I could write the code with a few hints.

    Is there some way to get the length of a string in twips or some unit other than characters?
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  2. #2
    Junior Member Guile.NET's Avatar
    Join Date
    Mar 2003
    Location
    www.voodoochat.com
    Posts
    31
    Set the textbox property 'Alignment' to "Right Justify".
    The arrow shot by the archer may, or may not, kill a single person. However, stratagems devised by a wise man, can kill even babes in the womb.

  3. #3
    Addicted Member
    Join Date
    Nov 2002
    Posts
    155
    But he said listbox in the title.

  4. #4
    Fanatic Member laserman's Avatar
    Join Date
    Jan 2002
    Location
    Wales U.K
    Posts
    775

    Right justify text in list box?
    The Listbox does not have a right justify the texbox does .which one do you want?if its the textbox then look at the answer give by Guile

  5. #5
    Junior Member Guile.NET's Avatar
    Join Date
    Mar 2003
    Location
    www.voodoochat.com
    Posts
    31
    I paid more attention to the post, then the title. <Shrug>
    The arrow shot by the archer may, or may not, kill a single person. However, stratagems devised by a wise man, can kill even babes in the womb.

  6. #6
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    your thread subject asks about a list box and the body of your quesiton asks about a text box. you need to decide which one you want to know about because they are not the same. If you really want to know about a text box, then as Guile.NET has stated, it is trivial.

    if you want to know about a list box, then (1) there is probably some way to do it through subclassing and/or API calls, but I don't know how, or (2) you can fake it by using a fixed width font such as courier and then putting a variable number of spaces onto the front of each item to make them all come out the same length

  7. #7
    Addicted Member
    Join Date
    Nov 2002
    Posts
    155
    Guv's abilities are well enough known to lead me to think it's a typo, cuz I'm quite sure he knows how to justify in a text box.

  8. #8
    Junior Member Guile.NET's Avatar
    Join Date
    Mar 2003
    Location
    www.voodoochat.com
    Posts
    31
    Without even knowing Guv, or his abilities, I'm thinking the same thing.

    In fact, I didn't even realise there _was_ a mistake, I just assumed he is new to VB, and wasn't aware of the property value.

    That being said... no, I don't know how to Right Justify text in a List Box. =)
    The arrow shot by the archer may, or may not, kill a single person. However, stratagems devised by a wise man, can kill even babes in the womb.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    Sorry about the typo. It is a list box that I want to right justify.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    create a new project and add a listbox to it..
    put this code in and run the project. You should be
    able to work with this.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     List1.AddItem "TESTING"
    5.     List1.AddItem "ANOTHER ITEM"
    6.     List1.AddItem "YET ANOTHER ITEM"
    7.     RightAlignListBox List1
    8. End Sub
    9.  
    10. Public Sub RightAlignListBox(lstBox As ListBox)
    11.     Dim lTextSizePix As Long
    12.     Dim lSpaceSizePix As Long
    13.     Dim lSizeNeeded As Long
    14.     Dim i As Integer
    15.    
    16.     'MAY NEED TO CHANGE THIS VALUE TO ACCOMODATE SMALL GAPS ON THE RIGHT SIDE,
    17.     'EITHER TOO MUCH SPACE OR NOT ENOUGH SPACE. 50 WORKED WELL FOR ME
    18.     Const Variable_Difference = 50
    19.    
    20.     lSpaceSizePix = Printer.TextWidth(" ")
    21.     For i = 0 To lstBox.ListCount - 1
    22.         lTextSizePix = Printer.TextWidth(lstBox.List(i))
    23.         lSizeNeeded = ((lstBox.Width * Screen.TwipsPerPixelX) - lTextSizePix) - Variable_Difference
    24.         lSizeNeeded = lSizeNeeded / lSpaceSizePix
    25.         lstBox.List(i) = Space(lSizeNeeded) & lstBox.List(i)
    26.     Next
    27. End Sub
    28.  
    29.  
    30. Private Sub List1_DblClick()
    31.     Dim strItem As String
    32.     strItem = Trim(List1.List(List1.ListIndex))
    33.     MsgBox "Text: " & strItem & vbCrLf & "Length: " & Len(strItem)
    34. End Sub

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    Kleinma: Your code looks reasonable. I will try it and experiment a bit.

    Printer.TextWidth Function looks like the key to this task.

    Thanx !!

    If all else fails, I will try simulating a List Box using a Text Box. which might be the easiest way to go.

    I want to avoid changing to a change to a Text Box because there is some tricky logic associated with my current List Box. I am using it to simulate a LIFO Stack. The last Item is at the bottom at a fixed height on the Form, with the Top moving as items are added.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  12. #12
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Originally posted by Guv
    Kleinma: Your code looks reasonable. I will try it and experiment a bit.

    Printer.TextWidth Function looks like the key to this task.

    Thanx !!

    If all else fails, I will try simulating a List Box using a Text Box. which might be the easiest way to go.

    I want to avoid changing to a change to a Text Box because there is some tricky logic associated with my current List Box. I am using it to simulate a LIFO Stack. The last Item is at the bottom at a fixed height on the Form, with the Top moving as items are added.
    make sure you set the forms scalemode to pixel as well..

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