Imports System.IO
Imports System.Text
Public Class TestForm
Private Sub Test() Handles Button1.Click
Dim lbs As ListBox() = {ListBox1, ListBox2, ListBox3, ListBox4}
Dim spacing As Integer = 6 ' The horizontal spacing between columns.
Dim filaPath As String = Path.Combine(Application.StartupPath, "Test.txt")
Dim item1Parts As IEnumerable(Of String) = {}
Dim item2Parts As IEnumerable(Of String) = {}
Dim item3Parts As IEnumerable(Of String) = {}
Dim item4Parts As IEnumerable(Of String) = {}
Dim maxItemCount As Integer
Dim maxItemPartCount As Integer
Dim sb As New StringBuilder
' Get the max item count in listboxes.
maxItemCount = (From lb As ListBox In lbs
Order By lb.Items.Count Descending
Select lb.Items.Count).First
' Write column names.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
lbs(0).Name, lbs(1).Name,
lbs(2).Name, lbs(3).Name))
' Write column separator.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
New String("*"c, lbs(0).Name.Length),
New String("*"c, lbs(1).Name.Length),
New String("*"c, lbs(2).Name.Length),
New String("*"c, lbs(3).Name.Length)))
' Iterate listbox items.
For index As Integer = 0 To (maxItemCount - 1)
' If item at index exist...
If lbs(0).Items.Count > index Then
item1Parts = SplitByLength(lbs(0).Items(index).ToString, lbs(0).Name.Length)
End If
If lbs(1).Items.Count > index Then
item2Parts = SplitByLength(lbs(1).Items(index).ToString, lbs(1).Name.Length)
End If
If lbs(2).Items.Count > index Then
item3Parts = SplitByLength(lbs(2).Items(index).ToString, lbs(2).Name.Length)
End If
If lbs(3).Items.Count > index Then
item4Parts = SplitByLength(lbs(3).Items(index).ToString, lbs(3).Name.Length)
End If
If (item1Parts.Count > 1) OrElse
(item2Parts.Count > 1) OrElse
(item3Parts.Count > 1) OrElse
(item4Parts.Count > 1) Then
' Get the max item count in itemParts.
maxItemPartCount = (From col As IEnumerable(Of String)
In {item1Parts, item2Parts, item3Parts, item4Parts}
Order By col.Count Descending
Select col.Count).First
For x As Integer = 0 To (maxItemPartCount - 1)
' Write multiple items rows.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
If(item1Parts.Count <= x, String.Empty,
item1Parts(x) & New String(" "c, lbs(0).Name.Length - item1Parts(x).Length)),
If(item2Parts.Count <= x, String.Empty,
item2Parts(x) & New String(" "c, lbs(1).Name.Length - item2Parts(x).Length)),
If(item3Parts.Count <= x, String.Empty,
item3Parts(x) & New String(" "c, lbs(2).Name.Length - item3Parts(x).Length)),
If(item4Parts.Count <= x, String.Empty,
item4Parts(x) & New String(" "c, lbs(3).Name.Length - item4Parts(x).Length))))
Next
Else
' Write simgle items row.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
If(lbs(0).Items.Count <= index,
String.Empty & New String(" "c, lbs(0).Name.Length),
item1Parts.First & New String(" "c, lbs(0).Name.Length - item1Parts.First.Length)),
If(lbs(1).Items.Count <= index,
String.Empty & New String(" "c, lbs(1).Name.Length),
item2Parts.First & New String(" "c, lbs(1).Name.Length - item2Parts.First.Length)),
If(lbs(2).Items.Count <= index,
String.Empty & New String(" "c, lbs(2).Name.Length),
item3Parts.First & New String(" "c, lbs(2).Name.Length - item3Parts.First.Length)),
If(lbs(3).Items.Count <= index,
String.Empty & New String(" "c, lbs(3).Name.Length),
item4Parts.First & New String(" "c, lbs(3).Name.Length - item4Parts.First.Length))))
End If
' Write horizontal grid.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
New String("-"c, lbs(0).Name.Length),
New String("-"c, lbs(1).Name.Length),
New String("-"c, lbs(2).Name.Length),
New String("-"c, lbs(3).Name.Length)))
Next index
File.
WriteAllText(filaPath, sb.
ToString, Encoding.
Default) sb.Clear()
Process.Start(filaPath)
End Sub
' Split By Length
' By Elektro
'
''' <summary>
''' Splits a string by the specified character length.
''' </summary>
''' <param name="str">The string to split.</param>
''' <param name="length">The character length.</param>
''' <returns>IEnumerable(Of System.String).</returns>
Private Function SplitByLength(ByVal str As String,
ByVal length As Integer) As IEnumerable(Of String)
Dim stringParts As New List(Of String)
If str.Length > length Then
Do Until str.Length <= length
stringParts.Add(str.Substring(0, length))
str = str.Remove(0, length)
Loop
' Add the last missing part (if any).
If Not String.IsNullOrEmpty(str) Then
stringParts.Add(str)
End If
Else
stringParts.Add(str)
End If
Return stringParts
End Function
End Class