Help with splitting text into lines.

DouglasBell

Active member
Joined
Aug 16, 2011
Messages
38
Programming Experience
Beginner
Hi All

I have a database application that basically recreates a spreadsheet form for data entry.

I have used a Table Layout Panel so that the cells, using Textbox Controls and the Column / Row headers using label controls can be dynamically created based on what selection criteria is used. It all works fine apart from my label controls, as these are a specific size if the label string from the database is too large then I lose of the heading text.

What I would like to do is have my text go onto multiple lines in the label. Say I make my label width 100 ad that can support a maximum of 20 characters, then I want the text to go onto the second line if the characters are too long.

For example

"The quick brown fox jumped over the fence" would look like this on the label.

The quick brown fox
jumped over the
fence

Hope you can help

Regards

Douglas Bell
 
Here is one solution using an array of words

It moves to the next line after the end of a word, as long as the line has less than 20 characters:

VB.NET:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim max As Integer = 20
        Dim n, u, w As Integer
        Dim oneword, myline As Integer
        Dim sentence, word As String
        Dim words() As String
        sentence = "Mary had a little lamb, its fleece was white as snow."
        'sentence = InputBox("Enter your string...")    'or get string from another source
        n = sentence.Length
        u = n \ max
        Dim linewords(u) As String
        sentence = sentence.Trim()      'remove leading and trailing spaces
        Do                      'remove duplicate spaces
            sentence = sentence.Replace("  ", " ")
        Loop Until sentence.IndexOf("  ") = -1

        words = sentence.Split(" "c)    'separate sentence into an array of words

        w = 0
        For Each word In words
            oneword = (word & " ").Length
            myline += oneword
            If myline < max Then
                linewords(w) &= word & " "
            Else
                w += 1
                linewords(w) = word & " "
                myline = word.Length
            End If

        Next word

        Label1.Text = ""
        For x As Integer = 0 To u
            Label1.Text &= linewords(x) & vbCrLf
        Next x
    End Sub
 
If you set Labels AutoSize property to False and give it a fixed size (to allow multiple lines) it will wrap the text to multiple lines automatically.
 
If you would need to "split" text that could be achieved very easy with Regex class:
        Dim text = "Mary had a little lamb, its fleece was white as snow."
        text = Regex.Replace(text, ".{1,20}( |$)", Function(m) m.Value & vbNewLine)
 
Hi All

Sorry first chance of me getting to look at this again. Thanks for the replys

@Solitare. I get an array error when using this its says "Index was outside bounds of the array" @ linewords(w) = word & " "

@JohnH I am creating the labels automatically so using Direct Cast to change label properties, I have specified the label size and set AutoSize to false but it just doesnt seem to wrap the text.
Tried your code above but I get an error on "Function(m)" saying expression expected.

Regards

Douglas Bell
 
Tried your code above but I get an error on "Function(m)" saying expression expected.
You're probably using an old VB version, instead of the lambda expression you can use AddressOf operator and a full regular function. For example:
Public Function Replacer(m As Match) As String
    Return m.Value & vbNewLine
End Function

and call:
text = Regex.Replace(text, ".{1,20}( |$)", AddressOf Replacer)
 
@JohnH

Perfect, exactly what I was needing, I also managed to use this with my Label On Paint function that changes the direction of the text.

Thanks for the help.

Regards

Douglas Bell
 
Back
Top