|
-
May 15th, 2013, 06:18 PM
#1
Thread Starter
New Member
[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.
-
May 15th, 2013, 06:23 PM
#2
Thread Starter
New Member
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
-
May 15th, 2013, 07:13 PM
#3
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.
-
May 16th, 2013, 03:16 AM
#4
Thread Starter
New Member
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.
-
May 16th, 2013, 03:20 AM
#5
Thread Starter
New Member
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...
-
May 16th, 2013, 04:15 AM
#6
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.
-
May 16th, 2013, 05:31 AM
#7
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.
-
May 16th, 2013, 03:29 PM
#8
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
May 16th, 2013, 04:22 PM
#9
Re: Lining up text in a listbox
Pradeep's suggestion is your best option in my opinion...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 16th, 2013, 04:53 PM
#10
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).
-
May 16th, 2013, 05:26 PM
#11
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
May 16th, 2013, 05:50 PM
#12
Re: Lining up text in a listbox
But the OP already said that he doesn't want to restrict the usage to monospaced fonts.
-
May 17th, 2013, 03:34 AM
#13
Thread Starter
New Member
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
-
May 18th, 2013, 09:22 AM
#14
Thread Starter
New Member
Re: Lining up text in a listbox
Thanks again Joacim, the code works very nicely indeed!
-
Aug 12th, 2013, 05:46 AM
#15
Thread Starter
New Member
Re: Lining up text in a listbox
 Originally Posted by Joacim Andersson
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
-
Aug 12th, 2013, 05:54 AM
#16
Re: Lining up text in a listbox
 Originally Posted by ElliotHC
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.
-
Aug 12th, 2013, 06:04 AM
#17
Thread Starter
New Member
Re: Lining up text in a listbox
 Originally Posted by jmcilhinney
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.
-
Aug 12th, 2013, 07:19 AM
#18
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|