CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Listbox vbTa

  1. #1
    Join Date
    Aug 2000
    Posts
    265

    Listbox vbTa

    I am using the following line of code to print two columns of data in a listbox.

    List1.AddItem myfile & vbTab & vbTab & vbTab & oFile.DateCreated

    Most of the items print fine in two separate columns, but some of the second column items don't print at the same tab stop. For instance, they may be indented a few spaces. It makes the output for the second column look zigzagged.

    In debug, I see no reason for this to occur. Any ideas?


  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Listbox vbTa

    When you populate the Listbox use the Ltrim function to ensure there are no leading blanks in your source data.

    John G

  3. #3
    Join Date
    Aug 2000
    Posts
    265

    Re: Listbox vbTa

    John,

    List1.AddItem RTrim(myfile) & vbTab & vbTab & vbTab & LTrim(oFile.DateCreated)

    I tried rtrim, ltrim, trim with no success.

    Also, I changed the font to be a truetype font thinking that might affect the output. Still no luck. Only 4 or 5 items in the second column are out of line. It is not consistent with any other fields of data.


  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Listbox vbTa

    A TrueType font has variable width spacing. Try a non-Proportional font such as the Courier font. All of its characters are the same width.
    By the way. Would it not be better to use a ListView control instead of a ListBox control for multiple columns?

    John G

  5. #5
    Join Date
    Aug 2000
    Posts
    265

    Re: Listbox vbTa

    Courier font did not correct the problem.

    I've never used the listview control. I am trying to research it now in help to see if I can figure out how to use it.


  6. #6
    Join Date
    Mar 2001
    Posts
    11

    Re: Listbox vbTa

    Yes ListView is the best control to use here.
    [vbcode]
    Dim FirstStorecol As ListItem
    Dim NextStoreCol As ListSubItem
    Set FirstStorecol = lvSelStores.ListItems.Add(, , "TEST")
    Set NextStoreCol = FirstStorecol.ListSubItems.Add(, , "TESTSUB")

    This will do it for u




  7. #7
    Join Date
    Aug 2000
    Posts
    265

    Re: Listbox vbTa

    I'm anxious to learn about this control. I modified my code as such:
    Set FirstStorecol = ListView1.ListItems.Add(, , myfile)
    Set NextStoreCol = FirstStorecol.ListSubItems.Add(, , oFile.DateCreated)

    The output is very strange. All of the data wraps so it is very difficult to read, and there are many, many columns. The subitem data doesn't appear at all.


  8. #8
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Listbox vbTa

    Here is a quick and dirty example of a Listview. It has 3 columns. The First column is hidden.
    Study the Form_Load event. It is a little more complex to use but is very flexible and a lot more capabilities than a standard ListBox.
    Start a new project, add a Listview. Make it about 5 inches wide. Paste this code into the form. The sample includes a method to sort the rows by clicking on the Row header.

    option Explicit

    public Function GetRandomNumber()
    Randomize
    GetRandomNumber = Int((99999 - 1 + 1) * Rnd + 1)
    End Function

    private Sub Form_Load()
    Dim NX as ListItem, i as Integer
    Dim Temp as string
    lv.ListItems.Clear
    lv.ColumnHeaders.Clear
    lv.ColumnHeaders.Add , "Hidden", "Hidden", 0, lvwColumnLeft
    lv.ColumnHeaders.Add , "string", "string", 3000, lvwColumnRight
    lv.ColumnHeaders.Add , "Number", "Number", 2000, lvwColumnRight
    lv.View = lvwReport
    for i = 0 to 100
    Temp = string(25, " ")
    RSet Temp = Format(GetRandomNumber, "###,###,###.#0")
    set NX = lv.ListItems.Add(, "A" & Chr(i), i)
    NX.SubItems(1) = GetRandomNumber
    NX.SubItems(2) = Temp
    next i

    End Sub

    private Sub lv_ColumnClick(byval ColumnHeader as MSComctlLib.ColumnHeader)
    lv.SortKey = ColumnHeader.Index - 1
    If lv.SortOrder = 0 then
    lv.SortOrder = 1
    else
    lv.SortOrder = 0
    End If
    ' set Sorted to true to sort the list.
    lv.Sorted = true ' force list sort

    End Sub




    John G

  9. #9
    Join Date
    Aug 2000
    Posts
    265

    Re: Listbox vbTa

    It works (with modifications, of course).

    Thanks so much!


  10. #10
    Join Date
    Aug 2000
    Posts
    265

    Re: Listbox vbTa

    One last question. How can I print the listview's contents?


  11. #11
    Join Date
    Jan 2000
    Location
    Saskatchewan, Canada
    Posts
    595

    Re: Listbox vbTa

    Lets say that the tab postions are set at 5,10,15,20 etc.
    Lets say that your first variable myfile has a length of 9. vbtab will take it to position 10. Lets say in your second variable of myfile, it has a length of 11. The vbtab will then take it to position 15. That is why it is not lining up. You will have to format these string variables to have the same length.
    Something like myfile = myfile & space(15-len(myfile)). myfile will now have a length of 15 regardless of how many characters it originally had.

    David Paulson

  12. #12
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Listbox vbTa

    There is no one method to print a Listview. Printing is pretty much left up to you. I know some controls have a .Print method but the Listview does not.

    John G

  13. #13
    Join Date
    Aug 2000
    Posts
    265

    Re: Listbox vbTa

    Thanks for the info. It works great now.


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

Featured