Results 1 to 18 of 18

Thread: [RESOLVED] Lining up text in a listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    Resolved [RESOLVED] Lining up text in a listbox

    Hi,

    I am entering items in to a listbox, it goes like this:

    Name: Qty: Location: Description: Order Code:
    Resistor 100 Drawer 4 0603 Resistor 10% 123456

    I have text boxes and dropdowns for each field and I am using the MeasureString graphics function to get the width of the text in the textbox.

    The max width in the textbox is 280 so I am running a loop adding chr(9) (tabs) each time checking the width until I reach 280, I then add this to the string and add the item to the listbox as above.

    The problem I seem to be having is that occasionally if the length is just right it will be a tab short and it doesn't line up.

    Here is my code:

    Get_Width()

    For i4 = 0 To 4 ' Max number of tabs for 280 width

    If String_Width.Width <= 228 Then ' 280 less tab width of 52 in Arial 9pt
    Temp_String = Temp_String & Chr(9)
    Get_Width()


    Else
    i4 = 4
    End If

    Next

    This should should line up 'Order Code' row perfectly but it doesn't and I can't figure out why.

    Any help would be great, thanks.

  2. #2

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    Re: Lining up text in a listbox

    Before anyone says.. I was using a while wend loop but I tried a few other ideas which required a for next loop, I have since changed it back to while wend loop. Still doesn't work of course

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Lining up text in a listbox

    The first option would be to not use a ListBox. The ListView and DataGridView controls support columns inherently so they should be used in preference. Of you really must use a ListBox then the first thing to know is that you must use a fixed-width font. To see how to fake columns in a ListBox or other text output, follow the CodeBank link in my signature and check out my thread on Fixed-Width Columns.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    Re: Lining up text in a listbox

    Thanks for the reply but that doesn't actually resolve the problem, it just avoids it.
    I will have user defined fonts so I will have to measure the witdh of a Tab for that font before measuring the text length which I'm already doing, thre sould be no issues in what I am doing as I am measuring the width and adding the appropriate amount of Tabs to line up the next lot of text.

    I tried Lucida Console which I think is fixed and the problem remains which leads me to believe that it is an issue with my code.

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    Re: Lining up text in a listbox

    Also, if I use another character in place of tabs it seems to be the same issue around the same overall string width, there is a point where there is an overlap which I need to pad out with something narrow like a space but not as I have them in my inputted text strings...

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Lining up text in a listbox

    ListBoxes are not intended to be used for data with columns. That's what the ListView and DataGridView are for. It should be no surprise that it is difficult and maybe impossible to make a ListBox do something it was never intended to do. If you use a fixed-width font then it will work if you do as I have shown in my CodeBank thread, as long as you make each "column" wide enough to accommodate the data. If you don't use a fixed-width font then, really, you deserve what you get because that's not what the ListBox is for. Avoiding the problem is good: i.e. use a control that was built to display data in the way you want it displayed and then there is no problem.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Lining up text in a listbox

    So what is the problem with using ListView/GridView? You can always give it the look and feel of a ListBox by setting its various properties, in case that is your problem.

    If you try to make the horse do the work of a cow or vice-versa, you will always face problems for sure.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Lining up text in a listbox

    I would agree with both the above members, a ListBox is not designed for columns, so although you might see it as avoiding the issue you are having, what you are trying to do with the ListBox is not very practical.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Lining up text in a listbox

    Pradeep's suggestion is your best option in my opinion...

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Lining up text in a listbox

    I would normally also use a ListView for these kind of things, however if you really want to go with a regular ListBox for some reason what you should do is to add tab stops to it to determine where the next column after a single Tab character should go. You express these in pixels. Here's an example:
    Code:
      <DllImport("user32.dll")> _
      Private Shared Function SendMessage( _
        ByVal hWnd As IntPtr, _
        ByVal wMsg As Integer, _
        ByVal wParam As IntPtr, _
        ByVal lParam As IntPtr) As Integer
      End Function
    
      Private Sub SetTabStops(listBox As ListBox, tabStops() As Integer)
        Const LB_SETTABSTOPS As Integer = &H192
        Dim pinnedValues As GCHandle
        pinnedValues = GCHandle.Alloc(tabStops, GCHandleType.Pinned)
        Dim ptr As IntPtr = pinnedValues.AddrOfPinnedObject()
        SendMessage(listBox.Handle, LB_SETTABSTOPS, New IntPtr(tabStops.Length), ptr)
        pinnedValues.Free()
        listBox.Refresh()
      End Sub
    You simply call the SetTabStops sub passing in the ListBox that you want to set the tab stops for and an array of integer values representing each tab stop. In the attached image you see two listboxes containing the same items, but I've only called the above sub for one of the listboxes. This is the code I used:
    Code:
        ListBox1.Items.Add("Product" & vbTab & "Description" & vbTab & "Price")
        ListBox1.Items.Add("BLT" & vbTab & "Sandwich with bacon, lettece and tomatoes" & vbTab & "7")
        ListBox1.Items.Add("PZVES" & vbTab & "Pizza with tomatosauce, cheese and ham" & vbTab & "12")
        ListBox1.Items.Add("BRGCH" & vbTab & "Cheese Burger" & vbTab & "9")
        ListBox2.Items.AddRange(ListBox1.Items)
        SetTabStops(ListBox2, New Integer() {70, 240})
    Since I only have 3 columns I only needed 2 tab stops (since the first column is at the beginning of each line). Now, all you have to do is to calculate where the actual tab stops should go (or do an educated estimation).

    Name:  ListBoxTabStops.png
Views: 2575
Size:  40.0 KB

  11. #11
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Lining up text in a listbox

    If the font being used was monospaced, you could just count the number of characters in the string as a width reference too for simplicity.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Lining up text in a listbox

    But the OP already said that he doesn't want to restrict the usage to monospaced fonts.

  13. #13

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    Re: Lining up text in a listbox

    Hi,

    Thanks Joacim, I actually got it working a different way but yours is actually more simple. I will try it out.

    Thanks Again

  14. #14

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    Re: Lining up text in a listbox

    Thanks again Joacim, the code works very nicely indeed!

  15. #15

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    Re: Lining up text in a listbox

    Quote Originally Posted by Joacim Andersson View Post
    But the OP already said that he doesn't want to restrict the usage to monospaced fonts.
    Hi Joacim,

    Thanks again for the settabs, it's been working really well...

    I have now come to a point where I need to print what is in the textbox but as you can imagine the tab spacing is all over the place, can I alter the settabs routine to work with the print document?

    Thanks

    Elliot

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Lining up text in a listbox

    Quote Originally Posted by ElliotHC View Post
    Hi Joacim,

    Thanks again for the settabs, it's been working really well...

    I have now come to a point where I need to print what is in the textbox but as you can imagine the tab spacing is all over the place, can I alter the settabs routine to work with the print document?

    Thanks

    Elliot
    Unlike in a ListBox, when you print you can put each piece of text wherever you want. If you want text printed in columns then you simply specify the appropriate X and Y coordinates for each field. If you had three columns then you would loop through the rows, and the columns for each row, increasing the X for fields in the same row and the Y for the rows.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    Re: Lining up text in a listbox

    Quote Originally Posted by jmcilhinney View Post
    Unlike in a ListBox, when you print you can put each piece of text wherever you want. If you want text printed in columns then you simply specify the appropriate X and Y coordinates for each field. If you had three columns then you would loop through the rows, and the columns for each row, increasing the X for fields in the same row and the Y for the rows.
    Thanks, I'd like to say that helped but I'm not exactly sure how to do that... Here is my code:

    Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim text As String = String.Join(Environment.NewLine, ListBox_Report.Items.Cast(Of String).ToArray)


    Dim tabStops As Single() = {55, 75, 170, 275, 305, 335, 365, 395, 425, 499}
    Dim myStringFormat As New StringFormat

    Print_Array = Print_Array & text


    myStringFormat.SetTabStops(0.0F, tabStops)
    e.Graphics.DrawString(Print_Array, Me.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top)



    End Sub

    for some reason it's not tabbing out correctly.

  18. #18

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    Re: [RESOLVED] Lining up text in a listbox

    Ooops... I had forgotten to add myStringFormat at the end of the DrawString

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