The Artima Developer Community
Sponsored Link

.NET Buzz Forum
A word wrapping solution using VB.Net

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Raymond Lewallen

Posts: 312
Nickname: rlewallen
Registered: Apr, 2005

Raymond Lewallen is a .Net developer and Sql Server DBA
A word wrapping solution using VB.Net Posted: Jan 17, 2006 9:19 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Raymond Lewallen.
Original Post: A word wrapping solution using VB.Net
Feed Title: Raymond Lewallen
Feed URL: /error.htm?aspxerrorpath=/blogs/raymond.lewallen/rss.aspx
Feed Description: Patterns and Practices, OOP, .Net and Sql
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Raymond Lewallen
Latest Posts From Raymond Lewallen

Advertisement

While helping a friend with some code, I ran into a situation where I needed to do something to word wrap lines that were going to print.  The lines were actually data coming from a database where the max length of the field is over 300 characters.  When making this data show up in a multiline textbox, it isn’t a big deal because we can word wrap it easily enough.  The problem comes when you want to print this data.  I needed to take the data stored in the textboxes and write it to a text file for printing at a later time.  That was all fine, except when I printed, naturally, the lines would run off the side of the page.

So here is what I came up with.  It was very difficult looking for solutions to handle word wrapping out on google.  The solutions I did find seems overly complex for the problem that needed to be solved.  If you have written a better solution, please let me know.  This is the best I came up with in the time constraint I had to write it in (2 hours).

Keep in mind, this word wrapping is rather specific, because it is setup for a basic print layout in Portrait mode, 1” margins and printing 10pt Times New Roman font, which is where the 86 characters per line comes from.

My next update to this routine will be to handle changing printer settings and adjusting for those for proper word wrapping.

Word wrapping

    Private Sub SetupForPrint(ByVal currentText As TextBox, ByVal filename As String)

        Dim currentStreamWriter As System.IO.StreamWriter = New System.IO.StreamWriter(filename)

 

        ' This is the max length we want any line to be.  This is based on printing using

        '    the font fact Times New Roman at a font size of 10.

        Dim maxLengthOfALine As Integer = 86

 

        ' This is our starting position within the text body wherewe start a new line

        Dim startingPosition As Integer

 

        ' This is the ending position within the text body where we end a new line

        Dim endingPosition As Integer

 

        ' This is used for the substring for the length of the line we need to pull.

        '    This has to be used because you can't always break apart strings for

        '    word wrapping at exactly 86 characters.  You have to account for not

        '    breaking words in half, so you have to break apart at spaces.  You'll

        '    see this below

        Dim lineLength As Integer = maxLengthOfALine

 

        ' This is the line that we will be writing to the file.

        Dim line As String

 

 

        ' Start looping through the text of the textbox until we reach the end.

        While startingPosition < currentText.Text.Length

            ' This locates the ending position of a line identified by a CRLF.

            endingPosition = currentText.Text.IndexOf(vbCrLf, startingPosition)

 

            ' This tells us that the complete line, from start to CRLF, is less than

            '    86 characters, so no word wrapping needs to be handled.

            If endingPosition - startingPosition < maxLengthOfALine Then

 

                ' This tells us that we have reached the end of the file, so get

                '    the line and get out of the loop.

                If endingPosition = -1 Then

                    line = currentText.Text.Substring(startingPosition, currentText.Text.Length - startingPosition)

                    currentStreamWriter.Write(line)

                    Exit While

                Else

                    ' We are not at the end of the file, but we have a suitable line

                    '    to write that doesn't need word wrapping.  Get the line and

                    '    set the start position of the next line to the end position of the

                    '    current line + 1.

                    line = currentText.Text.Substring(startingPosition, endingPosition - startingPosition)

                    startingPosition = endingPosition + 1

                    currentStreamWriter.Write(line)

                End If

 

            Else

                ' THIS IS WHERE WORD WRAPPING IS HANDLED

                ' This continues to word wrap for every 86 characters in the line

                While lineLength + startingPosition <= endingPosition

                    ' This backtracks in the line in order to find a suitable place to word wrap,

                    ' in this case, a SPACE

                    While currentText.Text.Substring(startingPosition + lineLength - 1, 1) <> Chr(32)

                        lineLength -= 1

                    End While

 

                    ' Get the line.

                    line = currentText.Text.Substring(startingPosition, lineLength)

 

                    ' Write the line.

                    currentStreamWriter.Write(line & vbCrLf)

 

                    ' If we had to backtrack, we can't add 1 to the count.  This

                    ' prevents us from cutting off the first letter of the next word.

                    If lineLength < maxLengthOfALine Then

                        startingPosition += lineLength

                    Else

                        startingPosition += lineLength + 1

                    End If

 

                    ' Reset the lineLength back to the default.

                    lineLength = maxLengthOfALine

 

                End While

 

                ' This is the end of the lines we had to word wrap.  This writes

                ' the last line that got word wrapped to the file.

                line = currentText.Text.Substring(startingPosition, endingPosition - startingPosition)

                currentStreamWriter.Write(line)

 

                ' Set the start position of the next line to the end position of the

                '    current line + 1.

                startingPosition = endingPosition + 1

            End If

        End While

 

        currentStreamWriter.Close()

    End Sub

Read: A word wrapping solution using VB.Net

Topic: Better Living Through Software - Let's Hack a Gibson, Dude! Previous Topic   Next Topic Topic: Service Component Architecture - bleh!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use