Reputation: 13
lstPrint.Items.Add(String.Format("{0,-20} {1,5}", "Denomination", "Count"))
For x As Integer = 0 To 6
lstPrint.Items.Add(String.Format("{0,-20} {1,13:S}", ouputArray(x), "Count"))
Next
For the sake of making things easier, Dim outputArray As String() = {"1$", "2$", "5$", "10$", "20$", "50$", "100$"}
, and I swapped the second array and made it just say Count.
typically I would simply use Convert.ToChar(Keys.Tab)
to make all my columns line up, but I'm trying to get better with string formatting. How would I go about compensating for the difference in characters?
Upvotes: 0
Views: 190
Reputation: 15091
I set the font for the list box to a fixed width font. Next I got the length of the longest string in the array. I used .PadLeft to make all the strings the same length.
Private Sub OPCode()
ListBox1.Font = New Font("Consolas", 12)
Dim outputArray As String() = {"1$", "2$", "5$", "10$", "20$", "50$", "100$"}
Dim longest As Integer = outputArray.OrderByDescending(Function(s) s.Length).FirstOrDefault().Length
For Each s In outputArray
ListBox1.Items.Add(s.PadLeft(longest) & " Count")
Next
End Sub
Upvotes: 1
Reputation: 54417
You need to use a fixed-width font if you expect that sort of formatting to produce aligned text. Spaces are much narrower than other characters in variable-width fonts.
Otherwise, how about using a control that actually has columns instead of a ListBox
, e.g. ListView
or DataGridView
? Using the best tool for the job is always a good idea.
Upvotes: 4