Trying to locate Carraige Returns in textbox RRS feed

  • Question

  • I have a text box that is populated by SQL datasource. The text type in the DB is a nvarchar and it stores carriage returns for a large block of text.

    I would like to count the carriage returns in the box to help me decide the height I should set for the text box, among other reasons. In the end I hope to deploy this to a Gridview and add a unique text box for each row of text in the block returned from the DB.

    I have tried searching the block, for CR, LF, Char(13), Chr(13), Char(10), Environment.NewLine, etc, etc, the function works with actual text characters, like the letter "a" or a string like "becuase" but it will not locate the carriage returns.

    Sample Text block:


    	[%iScreen%]   = getnum("iScreen")
    	[%iButton%]   = getnum("iButton")
    	[%iProg%]     = getnum("iProg")
    	[%itest1%]    = getnum("itest1")
    	[%iSelectMY%] = getnum("iSelectMY")
    	[%iDSAPNO%]   = getnum("iDSAPNO")
    	[%iVehicle%]  = getnum("iVehicle")
    	[%iIESC%]     = getnum("iIESC")
    	[%iEngine%]   = getnum("iEngine")
    	[%iDSA%]      = getnum("iDSA")
    	[%iEXY%]      = getnum("iEXY")
    	[%iACCPNO%]   = getnum("iACCPNO")
    	[%sPart%]     = 

    Here is the function and call...




    Dim count As Integer = StringCountOccurrences(myTextString, myCR)




        Function StringCountOccurrences(ByVal strText As String, _
        ByVal strFind As String) As Long
            Dim lngCompare As Integer = 1
            Dim lngPos As Long
            Dim lngTemp As Long
            Dim lngCount As Long
            If Len(strText) = 0 Then Exit Function
            If Len(strFind) = 0 Then Exit Function
            lngPos = 1
                lngPos = InStr(lngPos, strText, strFind, lngCompare)
                lngTemp = lngPos
                If lngPos > 0 Then
                    lngCount = lngCount + 1
                    lngPos = lngPos + Len(strFind)
                End If
            Loop Until lngPos = 0
            StringCountOccurrences = lngCount
        End Function


    Any help would be greatly appreciated. All suggestions will be tested.



    • Edited by march111 Thursday, September 22, 2011 6:39 PM
    Thursday, September 22, 2011 6:35 PM


  • Here is the solution....

            Dim lines() As String = Split(strText, vbCrLf)
            Dim num As Integer = lines.Length


    • Marked as answer by march111 Thursday, September 22, 2011 7:55 PM
    Thursday, September 22, 2011 7:55 PM