Click to See Complete Forum and Search --> : Listbox vbTa


comart
April 20th, 2001, 11:54 AM
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?

John G Duffy
April 20th, 2001, 12:57 PM
When you populate the Listbox use the Ltrim function to ensure there are no leading blanks in your source data.

John G

comart
April 20th, 2001, 01:02 PM
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.

John G Duffy
April 20th, 2001, 01:17 PM
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

comart
April 20th, 2001, 01:27 PM
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.

thomas_dipu
April 20th, 2001, 01:55 PM
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

comart
April 20th, 2001, 02:42 PM
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.

John G Duffy
April 20th, 2001, 04:08 PM
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

comart
April 20th, 2001, 04:31 PM
It works (with modifications, of course).

Thanks so much!

comart
April 20th, 2001, 04:38 PM
One last question. How can I print the listview's contents?

d.paulson
April 21st, 2001, 08:24 AM
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

John G Duffy
April 21st, 2001, 03:04 PM
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

comart
April 23rd, 2001, 08:11 AM
Thanks for the info. It works great now.