Results 1 to 7 of 7

Thread: Setting tab stops in listbox with VB5

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    I'm trying to use the example in Setting Tab Stops in a ListBox and the results are hosed.

    I am running VB5.0 SP3 at home. Should I be able to use the above example?

    I am rewriting the old DOS Basic doorprize drawing program for our PC Users Group. In the form that displays the "lucky numbers", I am adding the names & numbers to a list box. To make it easier to read (there are a lot of senior citizens in the club), I want to format the text.

    The following code is my routine that loads the ListBox. I must point out that, as the membership file was loaded, the lengths of the longest first & last names were saved. The lengths are used to determine the tab stops.
    Code:
    Private Sub Form_Load()
      ' Load the list box
      Dim lTabs(2)   As Long
      
      ' set my size
      Me.Height = 6600
      Me.Width = 10155
      
      lstReviewV.Clear
      
      lTabs(0) = 0
      lTabs(1) = giFNLen + 2             ' giFNLen was set to 7
      lTabs(2) = lTabs(1) + giLNLen + 2  ' giLNLen was set to 9
      DoTabs lstReviewV, lTabs           ' set the tabs
      For cntl = 1 To giRecCount
          ' load the listbox
          lstReviewV.AddItem gsFName(cntl) & vbTab & _
                             gsLName(cntl) & vbTab & _
                             "has lucky #" & Format(giNumber(cntl, 2), "000")
      Next cntl
    
    End Sub
    The results look like the following. The group is small, so I have included the entire output in the hopes that some pattern may be seen. The first & last names have been replaced for privacy. Note: I have also provided a rule above the output as an aid for anyone looking at this.
    Code:
    1234567890123456789012345678901234567890
    FFFFFF  LLLLLLLL        has lucky #021
    FFFFFF  LLLL    has lucky #015
    FFFFFF  LLLLLL  has lucky #020
    FFFFF   LLLLLLL has lucky #008
    FFFF LLLLLLL    has lucky #018
    FFFFF   LLLLLLL has lucky #014
    FFF  LLLLLL     has lucky #035
    FFFFFF  LLLLLLL has lucky #028
    FFFF LLLLLLL    has lucky #019
    FFFFF   LLLLLLL has lucky #034
    FFFF LLLLLLL    has lucky #031
    FFFFF   LLLLLLLLL       has lucky #027
    FFFFF   LLLLLLLLL       has lucky #038
    FFFF LLLLLLLL   has lucky #002
    FFFFFF  LLLLLLL has lucky #036
    FFFFFF  LLLLLLL has lucky #024
    FFFFFF  LLLLLLL has lucky #037
    FFFF LLLLLL     has lucky #023
    FFF  LLLLLL     has lucky #029
    FFFFFFF LLLLL   has lucky #013
    FFFFF   LLLLLL  has lucky #006
    FFFFF   LLLLLL  has lucky #041
    FFF  LLLLLL     has lucky #042
    FFFF LLLLLL     has lucky #032
    FFFFF   LLLLLLL has lucky #040
    FFFFF   LLLLLLL has lucky #012
    FFFFFF  LLLLLL  has lucky #026
    FFFF LLLLL      has lucky #016
    FFFFF   LLLLLL  has lucky #033
    FFFFF   LLLLLL  has lucky #025
    FFF  LLLLLL     has lucky #030
    FFFF LLLLLLLLL  has lucky #004
    FFF  LLLLL      has lucky #022
    FFFFF   LLLLL   has lucky #001
    FFFFFF  LLLLLL  has lucky #010
    FFFF LLLLL      has lucky #005
    FFFF LLLLLLL    has lucky #011
    FFFF LLLLLLLL   has lucky #007
    FFFFF   LLLLL   has lucky #039
    FFF  LLLLL      has lucky #017
    FFFFFF  LLLLLL  has lucky #009
    FFFF LLLLLL     has lucky #003

  2. #2
    Lively Member Surgeon's Avatar
    Join Date
    Oct 2000
    Posts
    121
    Set the font of your listbox to Courier New. This font has the property that each char of it has the same screen width.
    I mean that "i" occupies the same screen width as "w", no matter of the screen resolution. Of course, that only solves your problem at its surface. My opinion is that you definetely shouldn't use a listbox if you want to display multicolumn text such as you discribed to us. Maybe you should give the DataGrid a try, or a similar control.

    Surgeon

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    I am using Courier New. It's one of the few fixed pitch fonts that are available and I tend to favor it when alignment is important.

    I'm relatively new to VB and am trying different things to see how well they work. At work, I program in C and string manipulation in C is a piece of cake compared to VB.

    Guess I'll take a look at DataGrids...something new to play with. ~¿ô

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    I just found something interesting!

    I had a little time to play around and tried using the code exactly as it appears in the above mentioned tip. I added the extra line to display a rule.

    The last name lined up at byte 25 (not 100) as I expected. The title was off the edge of the control.
    Code:
    Private Sub Form_Load()
      Dim Tabs(2) As Long
      
      Tabs(0) = 0
      Tabs(1) = 100
      Tabs(2) = 200
      DoTabs List1, Tabs
      List1.AddItem "01234567890123456789012345678901234567890"
      List1.AddItem "John" & vbTab & "Percival" & vbTab & "Content Editor"
      List1.AddItem "James" & vbTab & "Limm" & vbTab & "Senior Editor"
    
    End Sub
    
    Output was:
    01234567890123456789012345678901234567890
    John                     Percival 
    James                    Limm
    I halved the values for Tabs(1) and Tabs(2) and reran.
    Code:
    Tabs(0) = 0
    Tabs(1) = 50
    Tabs(2) = 100
    
    Output was:
    01234567890123456789012345678901234567890
    John        Percival     Content Editor
    James       Limm         Senior Editor
    Note: the left side of the last names actually started in the middle of the 2.

    My expectations were that text would start at positions 100 & 200, and later at 50 & 100. It appears that the actual starting position is 1/4 of the tab value.

    Can anyone shed some light on this?

  5. #5
    Guest

    i prefer to use a different approach to tabs in a listbox:

    i will assume a safe length of the string and adjust the
    spacing according to this length. a fixed font has to
    be used with this, i use courier new.


    Code:
    Private Sub Form_Load()
        List1.Clear
        List1.AddItem "John" & Space$(15 - Len("John")) & _
        "Percival" & Space$(15 - Len("Percival")) & "Content Editor"
        
        List1.AddItem "James" & Space$(15 - Len("James")) & _
        "Limm" & Space$(15 - Len("Limm")) & "Senior Editor"
    End Sub



  6. #6
    Addicted Member
    Join Date
    Sep 2000
    Posts
    230

    Thumbs down I've had this problem

    I struggled with this problem before and wound up using a simple grid control. The above code only works if the items are around the same length. For example



    Code:
    Private Sub Form_Load()
    
    List1.Clear
        List1.AddItem "John" & Space$(15 - Len("John")) & _
        "Percival" & Space$(15 - Len("Percival")) & "Content Editor"
        
        List1.AddItem "Alexandra" & Space$(15 - Len("James")) & _
        "Limm" & Space$(15 - Len("Limm")) & "Senior Editor"
    
    End Sub
    shifts the tabstops.



    I found myself using the listbox only because I did not know how to use the grid controls, but found out they are much better for displaying tabular data and not difficult to use.

    Shawn Hull
    VB6, SP3 (Professional Edition)

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    I like to dig into things to see how they work. Using the odd discovery that I posted earlier, I multiplied my tab values by 4 and the result was just what I wanted.
    Code:
      lTabs(0) = 0
      lTabs(1) = (giFNLen + 1) * 4           ' giFNLen = 7
      lTabs(2) = (giFNLen + giLNLen + 2) * 4 ' giLNLen = 9
    I still don't have a good explanation for what is happening. I found some info in the feedback replies to the tip which states that the tabs are measured in pixels.

    This doesn't seem to correspond with my values and their results. My lTabs(1) is set to 32 and lTabs(2) is set to 72. I'm using Courier New at 20 pt. At that pitch, I would think that a 7 letter first name would be more than 32 pixels wide.

    I took a look at the DataGrid control. I doesn't provide the presentation that I am looking for. Since I have figured out how to manipulate the tabs, I'm going to leave things alone.

    Thanks for all the suggestions.

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