dcsimg
Results 1 to 6 of 6

Thread: [RESOLVED] String.Format doesnt seem to be working

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    7

    Resolved [RESOLVED] String.Format doesnt seem to be working

    I am trying to use String.format() to set up columns in a listbox that I am using for output. I first set up the format string into a variable like

    fmtstr = "{0,7} {1,6} {2,5} {3,12} {4,3} {5,3} {6,3} {7,3} {8,3} {9,2}"

    then when ever I out put to the listbox all I have to do is

    listbox1.items.add(String.format(fmtstr, var1, var2, var3,...var10"

    I have a headings output that goes in first when the form loads that uses the same format string, but when I go to use it for user generated output the data doesn't line up with the headings.

    I know this is probably a basic question but I have been out of the programming field for a few years now and I'm a bit rusty. Any help or suggestions with this is greatly appreciated.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,214

    Re: String.Format doesnt seem to be working

    Welcome to VBForums

    As the ListBox doesn't actually have columns, the closest you can do is put a varying amount of spaces in between each 'column' (how many spaces depends on the width of the data), and hope that the characters don't have different widths... but for most fonts, characters do have different widths, so you would be extremely lucky to have multiple rows of data line up.

    With a ListBox the best you can do is use a fixed-width font (perhaps Courier), but that will look odd.

    A better idea would be to use a control that does have multiple columns, such as a ListView or a DataGridView.

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: String.Format doesnt seem to be working

    "Fixed-width font" is probably the only solution for ListBox, as mentioned by si_the_geek.

    Most fonts are "variable width". That means "M" is wider than "i". But because of that, it's very rare that spaces and/or tabs can be used to align text. Some fonts are "fixed width", and use the same amount of space for all characters. They tend to look "worse" to the eye, but line up properly.

    If you want columns, si_the_geek is spot on: ListView and DataGridView are your choices in WinForms.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    7

    Re: String.Format doesnt seem to be working

    I have the listbox set to use a fixed width font. Its not like the data is off a little here and there because if the font, its way off. Here is a picture with the headings and sample data
    Name:  Capture.jpg
Views: 67
Size:  5.1 KB

    I will give either a list view or grid view a shot. Never used either of those before.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,479

    Re: String.Format doesnt seem to be working

    The reason why your data is off is because you do not account for the fact that your field "1234567" is shorter than your column header "Work Order #". If you wanted to continue to use the ListBox, then whenever you add a new item, you'll need to keep track of the longest length item in that field (including column headers) so that once you're finished adding all of the items you can iterate through all of the items and check if the field's length needs a space appended to it.

    I have no problem helping you load your data into the appropriate control, but first how are you getting the data? Is it stored in a file (*.txt, *.mdb, etc.), is a user inputting the data during run-time, or is it something else?

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    7

    Re: String.Format doesnt seem to be working

    Quote Originally Posted by dday9 View Post
    I have no problem helping you load your data into the appropriate control, but first how are you getting the data? Is it stored in a file (*.txt, *.mdb, etc.), is a user inputting the data during run-time, or is it something else?
    The first four fields are user input at runtime, everything else is calculated using that input.

    Here is what the max should be for each field
    Work order#- 7 digits
    Part# - 6 digits
    Qty per Skid - max of 4 digits
    Total Good - 6 digits
    3 of skids, top trays, and btm trays - max of 3 digits
    tier sheets - max of 4 digits
    Machine# 2

    I did not take into consideration the length of the Heading text.

    UPDATE:
    After taking into consideration the length of my headings and adjusting my format string, the listbox output is working.

    Name:  Capture.jpg
Views: 60
Size:  4.8 KB

    Thank you everyone for the help. And a big thank you to Dday9 for pointing that out!
    Last edited by dschaldach; Jan 26th, 2018 at 10:50 AM. Reason: update

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width