Hello.everyone. I met a problem today and search for a long time without any luck:(

I want to limit length in textbox. not total maxlength but maxlength perline. for example. if I choose to limit 7 char per line. when words exceeded 7 in a line textbox will start a newline.(without break the word) can anyone tell how to make it? I did actually search for a long time.. but I didn't find any useful information for my request. thank u.

For best results.
such as. when paste above text to textbox. it will become below in textbox.

For best

although in text "For best results" s is the 8th char in first line.but it;s a word I dont want to break it


Recommended Answers

All 7 Replies

Code it in TextChanged event.

String length is your friend.

Thanks for ur reply man. Actually I was about to write textbox content into a .txt file. although textbox behave as I described but when it writes to a txt file.it's actually 1line if I dont add new line in manual. that's what I want. when writes to txt file. it become what I say:) thanks.

So I have to formatted the text in textbox or when writes to a file. it produce the text into format .

I knew it has to code in textchanged event but I just dont know how to detect length per line.

why don't you store each line of the textbox into an array, so that, on TextChange, you can count how many words are in the actual index of the array, and when 7 words is reached, you pass on to the next line, and at the end of each index, you add a \n for the line break?

Hmm, Please try

Public Class SplitTest
    Public Shared Sub Main()
        Dim words As String = "This is a list of words, with: a bit of punctuation" + _
                              vbTab + "and a tab character."
        Dim split As String() = words.Split(New [Char]() {" "c, ","c, "."c, ":"c, CChar(vbTab) })

        For Each s As String In  split
            If s.Trim() <> "" Then
            End If
        Next s
    End Sub 'Main
End Class 'SplitTest
' The example displays the following output to the console:
'       This
'       is
'       a
'       list
'       of
'       words
'       with
'       a
'       bit
'       of
'       punctuation
'       and
'       a
'       tab
'       character

how ever I found this code in msdn but it's split word by a certain char. how could I modify it into split at every nth char ? or do we have a simple way to do it? read a text and split it into newline at every nth char.thnaks

have a counter to count the space char ' ' , so that every 6 spaces it adds a new line

The following code will take a string and implement breaks based on a desired width:

Public Shared Function FormatStringWithBreaks(ByVal CardMessage As String, ByVal StringWidth As Int16) As String

        Dim nNdx As Int16 = 0
        Dim BackNdx As Int16 = 0
        Dim wrkNdx As Int16 = 0
        Dim LeftOverChars As String = String.Empty
        Dim strSpace As String = " "
        Dim strArray(50) As String
        Dim intNdx As Int16 = 0
        Dim intAddAmount As Int16 = 0
        Dim bolGetOut As Boolean = False

        If CardMessage.Length > StringWidth Then
            intAddAmount = StringWidth

            For nNdx = 0 To CardMessage.Trim.Length

                If CardMessage.Substring(nNdx + intAddAmount, 1) <> " " Then
                    For BackNdx = (nNdx + intAddAmount) To 0 Step -1
                        If CardMessage.Substring(BackNdx, 1) = " " Then
                            strArray(intNdx) = CardMessage.Substring(nNdx, intAddAmount - LeftOverChars.Length)
                            intNdx += 1
                            For wrkNdx = LeftOverChars.Length To 0 Step -1
                                strArray(intNdx) &= CardMessage.Substring(BackNdx, 1)
                            nNdx = (nNdx + StringWidth) - LeftOverChars.Length
                            If (nNdx + StringWidth) > CardMessage.Length Then
                                bolGetOut = True
                                Exit For
                            End If
                            LeftOverChars = ""

                            Exit For
                            LeftOverChars &= CardMessage.Substring(BackNdx, 1)
                        End If

                    If bolGetOut = True Then
                        Exit For
                    End If
                    strArray(intNdx) &= CardMessage.Substring(nNdx, intAddAmount)
                    intNdx += 1
                    nNdx += StringWidth
                    If (nNdx + StringWidth) > CardMessage.Length Then
                        Exit For
                    End If
                End If

            strArray(intNdx) = CardMessage.Substring(nNdx + 1, CardMessage.Length - (nNdx + 1))

            strArray(intNdx) = CardMessage
        End If

        Dim strOutput As String = String.Empty

        For ndx = 0 To intNdx
            Do While strArray(ndx).Substring(0, 1) = " "
                strArray(ndx) = strArray(ndx).Substring(0, 1).Replace(" ", "") & strArray(ndx).Substring(1)
            strOutput &= strArray(ndx) & vbCrLf

        Return strOutput

    End Function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.