Imports System Imports System.Collections.Generic Module Colmize Sub Main() ' Example usage of the colmize function Dim entries As New List(Of String) From { "Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9", "Item10" } Dim maxCols As Integer = 80 Dim space As Integer = 2 Dim minWidth As Integer = 0 Dim rowWise As Boolean = False Dim distribute As Boolean = True Colmize(entries, maxCols, space, minWidth, rowWise, distribute) End Sub Sub Colmize(entries As List(Of String), maxCols As Integer, space As Integer, minWidth As Integer, rowWise As Boolean, distribute As Boolean) Dim mean As Double = 0 Dim cols As Integer Dim lines As Integer Dim width As New List(Of Integer)() Dim i As Integer = 0 ' Calculate mean length of entries For Each entry In entries mean += entry.Length Next mean /= If(entries.Count > 0, entries.Count, 1) ' Determine the number of columns For cols = (maxCols + space) * 2 \ (mean + space) To 1 Step -1 lines = (entries.Count + cols - 1) \ cols width.Clear() For j As Integer = 0 To cols - 1 width.Add(minWidth) Next If rowWise Then ' Column-wise For Each entry In entries width(i \ lines) = Math.Max(width(i \ lines), entry.Length + space) i += 1 Next Else ' Row-wise For Each entry In entries width(i Mod cols) = Math.Max(width(i Mod cols), entry.Length + space) i += 1 Next End If Dim totalWidth As Integer = 0 For Each w In width totalWidth += w Next If totalWidth <= maxCols + space Then Exit For End If Next ' Output data in columns Dim extra As Integer = If(distribute AndAlso (cols > 1), (maxCols - width.Sum()) / (cols - 1), 0) If rowWise Then ' Column-wise For i2 = 1 To lines Dim line As String = "" For j As Integer = 0 To cols - 1 If i2 + j * lines - 1 < entries.Count Then line &= Left(entries(i2 + j * lines - 1), width(j) + extra).PadRight(width(j) + extra) End If Next Console.WriteLine(line.Trim()) Next Else ' Row-wise For i2 As Integer = 0 To lines - 1 Dim line As String = "" For j As Integer = 1 To cols If j + i2 * cols - 1 < entries.Count Then line &= Left(entries(j + i2 * cols - 1), width(j - 1) + extra).PadRight(width(j - 1) + extra) End If Next Console.WriteLine(line.Trim()) Next End If End Sub ' Helper function to mimic the left string function Function Left(str As String, length As Integer) As String If str.Length <= length Then Return str Else Return str.Substring(0, length) End If End Function End Module