-
Nov 24th, 2015, 02:33 PM
#1
Thread Starter
Junior Member
How to column aline string data
How do I format data into a List box so that all the columns line up? For example:
The first column contains strings such as: 1, 200, 3000.
The second column contains strings such as: .3, .23, 1.2
I WANT the rows in my list box to look like the following, except without the underscores. I essentially want all the decimal points, whether shown or not, to line up:
___1 _.3
_200 _.23
3000 1.2
Instead, what I'm getting in my list box is the following:
1 .3
200 .23
3000 1.2
Formatting values such as "###.00" preserves the TRAILING zero's place. But how do I preserve the leading blanks so the columns of data line up?
Thanks
Last edited by jerich1000; Nov 24th, 2015 at 03:33 PM.
-
Nov 24th, 2015, 02:35 PM
#2
Thread Starter
Junior Member
Re: How to column aline string data
I think I put this in the wrong forum. I'm programming in Visual Basic 2010 Express. Which forum prevails for this topic?
-
Nov 24th, 2015, 04:58 PM
#3
Re: How to column aline string data
The listbox cannot really do right justified digits by column aligned text.
The best you can do with it is to use a fixed width font. (Where a period character is the same width as a 1 or a 0)
You can usually spot a fixed width font because it looks ugly. FixedSys is one.
The column property in the listbox is a misnomer. It really means use vertical wrapping to the right.
To get right justified text you need to use something like the DataGridView control. Text Alignment is a column property.
Even so you will have to pad characters to the right with zeros.
Last edited by Gruff; Nov 24th, 2015 at 05:04 PM.
Burn the land and boil the sea
You can't take the sky from me
~T
-
Nov 24th, 2015, 09:40 PM
#4
Re: How to column aline string data
Originally Posted by jerich1000
I think I put this in the wrong forum. I'm programming in Visual Basic 2010 Express. Which forum prevails for this topic?
Contrary to misleading marketing attempts by the evil forces that hold Microsoft hostage today, there was no Visual Basic ever produced beyond VB 6.0 in 1998.
Everything else is some version of The Great Pretender, VB.Net. So your question belongs in the question forum for that family of non-VB languages that have assumed the Visual Basic name in order to cause mass confusion.
-
Nov 24th, 2015, 10:03 PM
#5
Re: How to column aline string data
Originally Posted by dilettante
Contrary to misleading marketing attempts by the evil forces that hold Microsoft hostage today, there was no Visual Basic ever produced beyond VB 6.0 in 1998.
Everything else is some version of The Great Pretender, VB.Net. So your question belongs in the question forum for that family of non-VB languages that have assumed the Visual Basic name in order to cause mass confusion.
Of course, there WAS a Visual Basic produced beyond VB6, because the name is given to whatever MS decides to give it to. Whether you are bitter about what they gave the name to is irrelevant to the fact that there exists such a thing.
To the OP: Some people are STILL seriously bitter about MS discontinuing the earlier VB. They tend to deny the existence of VB.NET, as you can see. There are some entertaining threads on the subject around here. However, the place this thread should be is in the .NET forum, as that is what you are using.
The Listbox doesn't format well for multiple columns. The DataGridView will do it, but the ListView (in Detail mode) is another alternative. In general, the DGV is easier to work with and more versatile than using the ListView in this fashion, and is probably what you want to use, but the ListView has a few display alternatives that are different from the DGV....slightly.
My usual boring signature: Nothing
-
Nov 25th, 2015, 03:38 AM
#6
Re: How to column aline string data
Welcome to VBForums
Thread moved from the 'General Developer' forum to the 'VB.Net' (VB2002 and later) forum
-
Nov 25th, 2015, 08:31 AM
#7
Re: How to column aline string data
Originally Posted by dilettante
Contrary to misleading marketing attempts by the evil forces that hold Microsoft hostage today, there was no Visual Basic ever produced beyond VB 6.0 in 1998.
Everything else is some version of The Great Pretender, VB.Net. So your question belongs in the question forum for that family of non-VB languages that have assumed the Visual Basic name in order to cause mass confusion.
I need to start clipping these and storing links somewhere for the monthly "Why is there controversy around VB .NET" so people believe me about the frothing-mouth people.
OP: Ignore people like dilettante. They are fiercely loyal to VB6 to the point that they do everything they can to turn people away from VB. Which, honestly, is probably for the better. The market sees VB like it sees the FORTRAN and ALGOL that inspires it: C# and a handful of other languages are much more marketable.
And if you use VB, you have to deal with people like ^ that in every post you make, and sometimes work with them. Sound fun? I didn't think so.
Last edited by Sitten Spynne; Nov 25th, 2015 at 08:35 AM.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Nov 25th, 2015, 10:07 AM
#8
Re: How to column aline string data
Dilettante is mostly pretty good with advice, and shouldn't be ignored, there just seem to be days when he's either off, or on, his meds when it comes to that subject.
My usual boring signature: Nothing
-
Nov 25th, 2015, 12:21 PM
#9
Re: How to column aline string data
It's useless and teaches people silly ideas like "You should ignore VB .NET and use VB6 instead", which is terrible advice for anything but very casual hobbyists at this point.
jerich1000: Gruff's response was factual and correct. The ListBox doesn't really have any support for columnar alignment of text, though I disagree that all fixed-width typefaces are ugly. You can consider using a ListView, which has columns if its View property is set to Details. Or you can use the DataGridView, which is one of the most complicated but flexible controls available. Or you can consider using a fixed-width font. FixedSys is one of them, but Consolas is a newer one you're likely to have that looks pretty nice.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Nov 25th, 2015, 12:53 PM
#10
Re: How to column aline string data
Set the listbox font to a fixed width type font, like "Courier New"
then you can do something like this,
Code:
Dim itms() As String = {"1.2", "222", "333.45", "444.56"}
For i = 0 To itms.Count - 1
Dim itm As Decimal = CDec(itms(i))
itms(i) = itm.ToString("n2")
Me.ListBox1.Items.Add(Space(7 - itms(i).Length) & itms(i))
Next
Make sure that Space(someNumber - itms(i).Length), where someNumber has a value greater than or equal to the Maximum length of itms(i).
-
Nov 25th, 2015, 02:48 PM
#11
Re: How to column aline string data
Or instead of calculating a number of spaces on the left use the .NET string padleft method.
itms(i).Padleft(10," ")
BTW
Perhaps ugly was to harsh a word. For numbers Fixed width fonts are fine.
For plain text though I submit that it looks strange when place side by side with
non fixed width (normal) fonts. The majority being non.
Also they take up a lot more width to display the same information than a non fixed width font.
Last edited by Gruff; Nov 25th, 2015 at 03:03 PM.
Burn the land and boil the sea
You can't take the sky from me
~T
-
Nov 30th, 2015, 09:22 AM
#12
Thread Starter
Junior Member
Re: How to column aline string data
Thanks for all your help. Dilettante gets the prize for being most creative. I'm going to do something similar to what wes4dbt suggested. When I get more powerful I may progress to DataGridView.
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
|