Announcement

Collapse
No announcement yet.

Listbox formatting

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Listbox formatting

    I'm making a listbox and am having some troubles here. Suppose I have 2 or more types of info in the box. First column could be "Randolph" or "Bob". Second column could be his age. I'd add name$ and age$ together with a couple of spaces inbetween.
    When using a font like Courier New with a constant width, "Bob" can be "Bob " and the ages would line up perfectly. However, I really want to use the New Times Roman font, which has the different width letters. Right now, I'm writing an algorithm to calculate how many spaces to add to the end of the first string to make the next column line up. Is there an easier way?

    Thanks from a newbie! Todd
    Todd Wasson
    http://PerformanceSimulations.Com
    PowerBasic Racing Simulator (October 2007 clip - 15.1MB wmv file) http:http://www.performancesimulations.co...m-GenIV-12.wmv

  • #2
    Todd,

    Give your listbox the %LBS_USETABSTOPS style and use a tab, CHR$(9) in between name and age.
    Good luck,
    Egbert

    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
    http://zijlema.basicguru.eu
    *** Opinions expressed here are not necessarily untrue ***

    Comment


    • #3
      Further to what Egbert said, use the %LB_SETTABSTOPS message to set the width of each "column" in the list box. This message requires the width of the columns to be specified in Dialog Box Units. To specify the widths in pixels, you have to change the mapping mode of the list box with the SetMapMode() function and the %MM_TEXT parameter.

      You can also have right-aligned instead of left-aligned columns by specifying the column width for the %LB_SETTABSTOPS message as a negative value (Windows 95).
      If you try to make something idiot-proof, someone will invent a better idiot.

      Comment


      • #4
        Egbert,
        Thanks for the fast response time. Got the reply before I got off line. That's what it's all about!
        Ok. It didn't work exactly as planned. The strings I'm using aren't names of people, they're longer, so it didn't work right. I think I need to make the tabstops further apart. I send a message to the Listbox like this:
        CONTROL SEND hDlgMain, %IDLISTBOX_MAIN, %LB_SETTABSTOPS ,wParam,lParam

        Win32api.hlp says:

        LB_SETTABSTOPS
        wParam = (WPARAM) cTabs; // number of TAB stops
        lParam = (LPARAM) (LPINT) lpnTabs; // address of TAB-STOP ARRAY

        Supposedly, I can put 1 for the wParam and the tabstop width in lParam. So I tried this:
        CONTROL SEND hDlgMain, %IDLISTBOX_MAIN, %LB_SETTABSTOPS,1,40

        and of course, this didn't work because I'm clueless when it comes to this Windows programming stuff. Help!! How do I set the tabstop width? (Just need it for one tabstop, the rest of the columns are easy to format from there.)

        Thanks again,
        Todd



        [This message has been edited by Todd Wasson (edited January 06, 2000).]
        Todd Wasson
        http://PerformanceSimulations.Com
        PowerBasic Racing Simulator (October 2007 clip - 15.1MB wmv file) http:http://www.performancesimulations.co...m-GenIV-12.wmv

        Comment


        • #5
          Todd,

          I've had the same problem using filenames in the first column. If a filename counts less than 8 characters the right column does not align properly when using 1 tabchar. The solution was very easy in this case: I forced my program to use 8.3 filenames, so there was a maximum length of 12 chars.

          Code:
          IF LEN(text$) < 9 THEN
            text$ = text$ + STRING$(2, 9)             ' 2 tabs
          ELSE
            text$ = text$ + CHR$(9)                   ' 1 tab
          END IF
          Maybe you can try a sort of a formula like this:
          text$ = text$ + STRING$(LEN(text$) \ 8 + 1, 9)

          Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
          http://zijlema.basicguru.eu
          *** Opinions expressed here are not necessarily untrue ***

          Comment


          • #6
            You can't send a value directly to lParam. You must send a pointer
            to a variable, using VARPTR(Value&).

            First of all, be sure to create the listbox with %LBS_USETABSTOPS.
            The following code will set the first tabstop to -50 dialog units,
            the negative value making it right aligned. The second tabstop is
            positive, so it becomes left aligned (default) right next to the
            first one. The third one is right aligned again, just as an example.
            Useful, in case you want to list a filename in the first column, the
            path in the second, length in third, etc..

            Code:
              LOCAL tbs() AS LONG: REDIM tbs(2) AS LONG
              tbs(0) = -50: tbs(1) = 54: tbs(2) = -180  ' whatever..
              CALL SendMessage(lsthWnd, %LB_SETTABSTOPS, 3, VARPTR(tbs(0)))
            When you fill the list, be sure to start with $TAB (CHR$(9) if you want to
            make the first item right aligned. Change the values and number of stops
            to your own liking..

            Comment


            • #7
              Fantastic! Thanks for the help Egbert, Matt, and Borje. In case anyone of my level is following, this is what I ended up doing:
              Egbert's suggestion for the LEN statement to decide how many tabs to use worked very well. I tried a variation of the formula he showed and it worked perfectly. After Matt and Borje described setting tab widths, I decided instead to set only one tabstop in the listbox and skip the formula:

              LOCAL tbs() AS LONG: REDIM tbs(2) AS LONG 'Could be tbs(0) or (1)?
              tbs(0) = 110
              CONTROL SEND hDlgMain, %IDLISTBOX_MAIN, %LB_SETTABSTOPS,1,VARPTR(tbs(0))

              Text1$=LEFT$(Description$,24) 'Trim if string is too long to fit column.
              Text1$=Text1$+CHR$(9) 'Add the tab to the end of the string.

              Voila, perfect! Thank you for the help!
              Todd




              [This message has been edited by Todd Wasson (edited January 07, 2000).]
              Todd Wasson
              http://PerformanceSimulations.Com
              PowerBasic Racing Simulator (October 2007 clip - 15.1MB wmv file) http:http://www.performancesimulations.co...m-GenIV-12.wmv

              Comment


              • #8
                .. REDIM tbs(2) AS LONG 'Could be tbs(0) or (1)?

                Can be zero. Many programmers do the same mistake and waiste space on
                one too many, but since arrays are zero based by default, it means you
                DIM item number zero = one item..

                Comment


                • #9
                  Thanks Borje. I will remember that for the future!
                  Todd
                  Todd Wasson
                  http://PerformanceSimulations.Com
                  PowerBasic Racing Simulator (October 2007 clip - 15.1MB wmv file) http:http://www.performancesimulations.co...m-GenIV-12.wmv

                  Comment

                  Working...
                  X