Word Wrap Function In VB.NET
- Public Function WrapText(ByVal Text As String, ByVal LineLength As Integer) As List(Of String)
- Dim ReturnValue As New List(Of String)
- ' Remove leading and trailing spaces
- If Words.Length = 1 And Words(0).Length > LineLength Then
- ' Text is just one big word that is longer than one line
- ' Split it mercilessly
- Text = Text.PadRight(lines * LineLength)
- For i = 0 To lines - 1
- Dim SliceStart As Integer = i * LineLength
- ReturnValue.Add(Text.Substring(SliceStart,
- LineLength))
- Next
- Else
- Dim CurrentLine As New System.Text.StringBuilder
- For Each Word As String In Words
- ' will this word fit on the current line?
- If CurrentLine.Length + Word.Length <
- LineLength Then
- CurrentLine.Append(Word & " ")
- Else
- ' is the word too long for one line
- If Word.Length > LineLength Then
- ' hack off the first piece, fill out the current line and start a new line
- Dim Slice As String =
- Word.Substring(0, LineLength - CurrentLine.Length)
- CurrentLine.Append(Slice)
- ReturnValue.Add(CurrentLine.ToString)
- CurrentLine = New System.Text.StringBuilder()
- ' Remove the first slice from the word
- Word = Word.Substring(Slice.Length,
- Word.Length - Slice.Length)
- ' How many more lines do we need for this word?
- Dim RemainingSlices As Integer =
- For LineNumber = 1 To RemainingSlices
- If LineNumber = RemainingSlices Then
- 'this is the last slice
- CurrentLine.Append(Word & " ")
- Else
- ' this is not the last slice
- ' hack off a slice that is one line long, add that slice
- ' to the output as a line and start a new line
- Slice = Word.Substring(0,
- LineLength)
- CurrentLine.Append(Slice)
- ReturnValue.Add(CurrentLine.ToString)
- CurrentLine = New System.Text.StringBuilder()
- ' Remove the slice from the
- Word = Word.Substring(Slice.Length, Word.Length - Slice.Length)
- End If
- Next
- Else
- ' finish the current line and start a new one with the wrapped word
- ReturnValue.Add(CurrentLine.ToString)
- CurrentLine = New System.Text.StringBuilder(Word & " ")
- End If
- End If
- Next
- ' Write the last line to the output
- If CurrentLine.Length > 0 Then
- ReturnValue.Add(CurrentLine.ToString)
- End If
- End If
- Return ReturnValue
- End Function
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- WrapText(TextBox1.Text, 30)
- End Sub
Output:
 For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]
Visit and like my page on Facebook at: Bermz ISware Solutions
Subscribe at my YouTube Channel at: SerBermz
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
 
              