Results 1 to 14 of 14

Thread: [RESOLVED] List Box Formatting

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Resolved [RESOLVED] List Box Formatting

    Well, I need help again. I am trying to correctly align the columns in this list box and it isnt working. If the name of the employee is longer, it pushes everything sideways.



    My code is :
    VB Code:
    1. Dim line, fields()
    2.         Dim employee As String, workhours, payrate, grosspay As Single, totworkhours, totpayrate, totgrosspay As Double
    3.         Dim fmtstr As String = "{0,-10}{1,23}{2,30}{3,23}"
    4.         Dim total As String = "Total"
    5.         Dim sr As IO.StreamReader = IO.File.OpenText("C:\windows\temp\payroll.txt")
    6.         Do While sr.Peek <> -1
    7.             line = sr.ReadLine
    8.             fields = line.split(","c)
    9.             employee = fields(0)
    10.             workhours = fields(1)
    11.             totworkhours += workhours
    12.             payrate = fields(2)
    13.             totpayrate += payrate
    14.             grosspay = workhours * payrate
    15.             totgrosspay += grosspay
    16.             lstsummarize.Items.Add(String.Format(fmtstr, employee, workhours, FormatCurrency(payrate), FormatCurrency(grosspay)))
    17.         Loop
    18.         lstsummarize.Items.Add(String.Format(fmtstr, total, totworkhours, FormatCurrency(totpayrate), FormatCurrency(totgrosspay)))
    19.         sr.Close()
    take from payroll.txt looks like this:

    Ricky Carmichael,20,500
    Roger Decoster,50,1000
    James Stewart,40,2000
    Broc Hepler,30,6000


    I want it to align all correctly. Please help fast
    Last edited by alain250sxf; Apr 12th, 2006 at 09:32 PM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,436

    Re: List Box Formatting

    Don't use a ListBox for tabular data. It is designed for a single list. Use a ListView or perhaps DataGrid or DataGridView to display tabular data. There are modifed ListBoxes here and here that do support columns but frankly I don't see the point.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Re: List Box Formatting

    It worked before, without using all that stuff. Im getting frustrated.

    This shows it correctly:
    VB Code:
    1. Private Sub btnreadin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnreadin.Click
    2.         Dim sr As IO.StreamReader = IO.File.OpenText("C:\windows\temp\Poopoo2.txt")
    3.         'Open a text file and put the data in the list box
    4.         Dim line, fields(), descrip As String
    5.         Dim price3, cost3, grossmargin, numrecords As Single
    6.         Dim totprice, totcost, totgross
    7.         Dim fmtstr As String = "{0,-20}{1,10}{2,10}{3,10}"
    8.         lstclear.Items.Clear()
    9.         Do While sr.Peek <> -1 'process until there are no more records
    10.             'If 3 records are processed, the program goes through this loop 3 times
    11.             line = sr.ReadLine 'get one line from the file
    12.             fields = line.Split(","c) 'split it into 3 fields based on the ","
    13.             descrip = fields(0) 'make custname the first field
    14.             price3 = fields(1)
    15.  
    16.             cost3 = fields(2)
    17.             'Calculate Amt over limit and display it as 4th column
    18.             grossmargin = price3 - cost3
    19.             lstclear.Items.Add(String.Format(fmtstr, descrip, FormatCurrency(price3), FormatCurrency(cost3), FormatCurrency(grossmargin)))
    20.             'calculate 3 totals
    21.             numrecords += 1
    22.             totprice += price3
    23.             totcost += cost3
    24.             totgross += grossmargin
    25.         Loop
    26.         sr.Close()
    27.         lstclear.Items.Add("")
    28.         lstclear.Items.Add(String.Format(fmtstr, "Average", FormatCurrency(totprice / numrecords), FormatCurrency(totcost / numrecords), FormatCurrency(totgross / numrecords)))
    29.     End Sub

    But mine doesnt.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,436

    Re: List Box Formatting

    The problem with padding strings and so forth is that as soon as one is longer than you expect it throws the whole thing out. Why try to force a nail in with a screwdriver. Use a hammer instead. The ListView is designed to support columns. The right tool for the job.

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Re: List Box Formatting

    I cant seem to find what the code will actually be for the listview. I see what you mean and it makes much more sense. Can you help me?

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,436

    Re: List Box Formatting

    Instead of this:
    VB Code:
    1. lstclear.Items.Add(String.Format(fmtstr, descrip, FormatCurrency(price3), FormatCurrency(cost3), FormatCurrency(grossmargin)))
    you would use something like this:
    VB Code:
    1. myListView.Items.Add(New ListViewItem(descrip, price3.ToString("c"), cost3.ToString("c"), grossmargin.ToString("c")))
    This assumes that you set the View property of the ListView to Details and already added the appropriate columns, which you would normally do in the designer. Note that I have also replaced the FormatCurrency Runtime function with the System-based equivalent.

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383

    Re: List Box Formatting

    You could pad the strings using the PadRight method, and it should allow you to have things line up in a listbox.

    Using code like
    VB Code:
    1. employee = fields(0).ToString.PadRight(50)
    2.             workhours = fields(1).ToString.PadRight(10)
    where the number on the right is how many total spaces will be allocated including the string itself. So if your string is 5 characters, using 50 will give you your string and 45 spaces. If your string is 10 characters, it returns your string with 40 spaces, etc...

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Re: List Box Formatting

    I have no idea how to implement what you just wrote.

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Re: List Box Formatting

    Quote Originally Posted by kleinma
    You could pad the strings using the PadRight method, and it should allow you to have things line up in a listbox.

    Using code like
    VB Code:
    1. employee = fields(0).ToString.PadRight(50)
    2.             workhours = fields(1).ToString.PadRight(10)
    where the number on the right is how many total spaces will be allocated including the string itself. So if your string is 5 characters, using 50 will give you your string and 45 spaces. If your string is 10 characters, it returns your string with 40 spaces, etc...
    Thank you, that worked perfectly.

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383

    Re: [RESOLVED] List Box Formatting

    glad to hear it... Sorry I couldn't respond sooner when you were unsure about the code I posted, but its good to see you were able to figure it out and get it working.

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,436

    Re: [RESOLVED] List Box Formatting

    As I said earlier, the limitation of padding strings is that as soon as one is longer than you expect things are thrown out of wack. Plus, if you aren't using a fixed width font then you're also stuffed. Here's the code:
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Me.ListBox1.Items.Add("wwww".PadRight(10) & "www")
    3.         Me.ListBox1.Items.Add("iiii".PadRight(10) & "iii")
    4.     End Sub
    and the result is shown in the attached screenshot. You can use tabs instead of spaces:
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Me.ListBox1.Items.Add("wwww" & ControlChars.Tab & "www")
    3.         Me.ListBox1.Items.Add("iiii" & ControlChars.Tab & "iii")
    4.     End Sub
    This will overcome the font width issue but if a string in one row is much longer than a string in another row then it will also throw things out. I just don't see why you would keep on using the screwdriver when there is a hammer sitting right next to you, but to each there own.
    Attached Images Attached Images  
    Last edited by jmcilhinney; Apr 13th, 2006 at 06:29 PM.

  12. #12
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383

    Re: [RESOLVED] List Box Formatting

    I do agree that for most situations dealing with column data, a listview is the most flexible option...

    but John you know as well as me that sometimes people just want an answer to the question at hand, rather than a different approach (even if said different approach is in most eyes a better method of doing things)

    I could tell from his original post image, that he was already using a fixed width font (looks like courier new) so I didn't think that was going to be an issue.

    As for the issue of padding a string that is bigger than the allocated padding space, that is something that needs to be coded for, however perhaps the data he is populating will always be in the bounds he has set for padding.

    I just didn't want you to think I was knocking your suggestion.. so for the record, I agree that the listview would be what I would personally use if I was coding such a project.

  13. #13
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,436

    Re: [RESOLVED] List Box Formatting

    I didn't think that you were specifically knocking my suggestion. Perhaps I do sometimes push what I consider the "proper" way too hard. Also, I didn't see the image in the first post because I have Firefox set to not show images from other servers. I think a lot of people don't realise that you can attach an image to your post, otherwise why would they go to the trouble of uploading an image to another server and linking to it? Attached images will only be displayed in all their glory if there is only one (otherwise a link is shown) but most people don't post more than one anyway.

  14. #14
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383

    Re: [RESOLVED] List Box Formatting

    I can't argue with you there... uploading directly to the forum is much easier that bothering with linking from somewhere else

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