Formatting text

vb.net , vista , macbootcamp Dublin, Ireland
  • 10 years ago
    Hi all I need help with the following task: User posts text into textbox/grid etc Button is pushed. The text is formatted as if it were in a spreadsheet. ie Only x words per row Each cell is of equal length and width. To put it another way; The first letter of the second word of the first row. should be directly above the first letter of the second word of the second row. many thanks
  • 10 years ago
    Shared Function CheersomeColumnize(ByVal text$, ByVal padding_left As Boolean, Optional ByVal glue$ = " ") As String 'fizzle spaces - it means spacing is not really user scalable now While InStrRev(text, " ") > 0 text = text.Replace(" ", " ") End While Dim lines() As String = Strings.Split(text, vbCrLf) Dim words$() = Nothing, word$ = Nothing Dim maximals As New Generic.Dictionary(Of Integer, Integer), wix, tln As Integer Dim U% = lines.GetUpperBound(0) : Dim parts As New Generic.List(Of String()) 'create index (dict) of longest words, known for each column, keeping word arrays in 'parts' For Each line In lines If line <> Nothing AndAlso line.Length > 1 Then words = line.Split(CChar(" ")) : parts.Add(words) For wix = 0 To UBound(words, 1) tln = Strings.Len(words(wix)) If maximals.ContainsKey(wix) Then If tln > maximals(wix) Then maximals.Item(wix) = tln : Else : maximals(wix) = tln : End If Next Else : parts.Add(New String() {line}) End If Next 'pad all words from 'parts' according to max index Dim output(U) As String, noop As Boolean = CBool(maximals.Count = 0) For i As Integer = 0 To U words = parts(i) If Not noop Then For j As Integer = 0 To UBound(words) word = words(j) tln = Strings.Len(word) If tln < maximals(j) Then If padding_left Then : words(j) = Strings.LSet(word, maximals(j)) : Else : words(j) = Strings.RSet(word, maximals(j)) : End If End If Next End If output(i) = Strings.Join(words, glue) Next Return Strings.Join(output, vbCrLf) End Function
  • 10 years ago
    try that, but if you want to do it interactive (means user will edit), I would be curious if you can work it out for padding_right :)

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - Rick Osborne