Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Wrapping

Status
Not open for further replies.

sillysod

Technical User
Jan 6, 2004
300
GB
Hi Guys,

This is driving me daft, I need to restrict the length of each line entered onto a multi line textbox to only be X chars, however the number of lines is not a concern.

Now i have found this code on internet which needed a little tweaking but as it stands now ...

Code:
Public Function Wrap(ByVal text As String, ByVal maxLength As Integer) As String
  
        text = text.Replace(vbCrLf, " ")



        Dim Words() As String = text.Split(" ")
        Dim currentLineLength As Integer = 0
        Dim Lines As ArrayList = New ArrayList((text.Length / maxLength))
        Dim currentLine As String = ""
        Dim InTag As Boolean = False
        For Each currentWord As String In Words


            If (currentLineLength + (currentWord.Length + 1)) < maxLength Then
                currentLine = currentLine & " " & currentWord.Trim(" ")
                currentLineLength = currentLineLength + (currentWord.Length + 1)
            Else
                Lines.Add(currentLine)
                currentLine = currentWord
                currentLineLength = currentWord.Length

            End If


        Next
        If (currentLine <> "") Then
            Lines.Add(currentLine)
        End If
        Dim textLinesStr() As String = New String((Lines.Count) - 1) {}
        Lines.CopyTo(textLinesStr, 0)


        Dim strReturn As String = ""

        For i As Integer = 0 To textLinesStr.Count - 1
            textLinesStr(i).Trim(vbCrLf)
            textLinesStr(i).Trim(" "c)

            strReturn = strReturn & textLinesStr(i) & ControlChars.NewLine

        Next

        Return strReturn.Trim(" "c)


    End Function


This code works great provided that the string does not already contain line breaks eg

if I enter the following, assuming i want the maximum length of each line to be 10 chars

Code:
This Line is too long
I am Line2
Hello3
xyz

the result is

Code:
This Line is too long I am
Line2 Hello3 xyz

However what I actually want is for it too take into account the line breaks I have entered already in the string and then add new ones where appropriate, i.e.

Code:
This Line
is too long
I am Line2
Hello3
xyz

Obviously this is because the very first line of the code strips out line breaks/ carriage returns, if I remove that line it puts in extra line breaks so the text gets further and further apart !

I've already tried a few ways of modifying the code but am out of ideas

Does anyone have any suggestions?

Cheers,
Daniel
 

Acctually, your text would look like:
Code:
This Line
is too 
[blue]long[/blue]
I am Line2
Hello3
xyz
I did play with this idea and the way I tried to do it was: for every line of text I found the 10th position, checked if it was a space. If it was - that's where I placed vbCrLf If not, I went backwords and found the last space and that's where the vbCrLf went.

But there was always a problem:
Let's say you do it and it looks right. Then the User wants to add a word in the middle of a paragraph you already formated this way, and all goes to hell because now all text is messed up.

Have fun.

---- Andy
 
Yea sorry your right, I cant't count.

I have had lots of fun already :) Googled my fingers to the bone on this one.
 
What happens if you don't remove the line breaks that are already there?

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Well I have just realised what is happening when i dont remove the vbcrlf.

It doesnt reset the currentlinelength so everytime i run the event the text gets longer as linebreaks keep getting added every x chars.

So i need to somehow check for a linebreak and if one is found reset the count.

I'll have a play and report back.
 
Well i tried amended the code as follows
Code:
        Dim str As String = vbCrLf

        Dim Words() As String = text.Split(" "c, vbCrLf)
        Dim currentLineLength As Integer = 0
        Dim Lines As ArrayList = New ArrayList((text.Length / maxLength))
        Dim currentLine As String = ""
        Dim InTag As Boolean = False

        For Each currentWord As String In Words

            If currentWord = vbCrLf Then
                Lines.Add(ControlChars.CrLf)
                currentLine = ""
                currentLineLength = 0
            Else



                If (currentLineLength + (currentWord.Length + 1)) < maxLength Then
                    currentLine = currentLine & " " & currentWord.Trim(" ")
                    currentLineLength = currentLineLength + (currentWord.Length + 1)
                Else
                    Lines.Add(currentLine)
                    currentLine = currentWord
                    currentLineLength = currentWord.Length

                End If


            End If
        Next



        If (currentLine <> "") Then
            Lines.Add(currentLine)
        End If

        Dim textLinesStr() As String = New String((Lines.Count) - 1) {}
        Lines.CopyTo(textLinesStr, 0)


        Dim strReturn As String = ""

        For i As Integer = 0 To textLinesStr.Count - 1
            textLinesStr(i).Trim(vbCrLf)
            textLinesStr(i).Trim(" "c)

            strReturn = strReturn & textLinesStr(i) & vbCrLf


        Next

        Return strReturn.Trim(" "c)

I was expecting to be able to check whether the word is vbcrlf and if so add a new line, however

If currentWord = vbCrLf Then

never returns true ???

so if i enter

Code:
Test
Test

TEST TEST

i now get

Code:
Test Test  TEST TEST 

does anyone have any ideas?


 
Yes, that isn't going to work.

This
Code:
Test Test  TEST TEST 
is actually this
Code:
Test vbLfTest vbLf vbLfTEST TEST vbLf

When you hit Enter while in a text box doesn't read it as vbCrLf but vbLf. When adding to the text of a text box you have to add it as vbCrLf.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top