Multi line textbox and the number of lines?

Hi,
I need to read the lines from a multi line textbox one by one and store them in an array. 
This multi line textbox shows the text in multi lines on  screen. But the problem is : the entire text returns as a one row when I programmatically reads the value from text box. 
And also “textbox.Lines().Length()” returns 1. 

Any thing I missed  Or any way to get around of this  


Answer this question

Multi line textbox and the number of lines?

  • Mukesh Patel

    The problem is the difference between "hard" returns and "soft" returns.  When a user enters data into a multiline textbox, the text may *appear* to be on different lines, but if they never press return, then the line*space
    space*is a "soft" return.  What you want is to place the lines into an array based on soft or hard returns.  Is that correct

    I *think* it's doable, but it'll require the API and some complicated code...  I can whip some up for you if you like, but it'll take a little while.

  • Jon Yong

    Erymuzuan,
    Thanks for the reply. 

    >>if the new line if created as a result of wrap property of the textbox<<

    if we could find how wrap property indicates the end of line, then simply could read the lines and store them in the array. This is something that microsoft experts should know. hope somebody from there help us on this.
    Thanks once again.

  • keithrull

    it should read

     Private Declare Function SendMessageINT Lib "user32.dll" _

            Alias "SendMessageA" (ByVal hWnd As IntPtr, _

            ByVal wMsg As Integer, ByVal wParam As Integer, _

            ByVal lParam As IntPtr) As Integer




  • aspnovice

    This works perfect. Thank you guys. 
  • Cunk

    JacobMVP,
    Thanks for your reply. Yes, i want to read the data from the textbox on screen and store them on an array exactly the way it looks on screen. Means line 1 from the textbox goes to the first element of array( array[0] ) and line 2 goes to second element of the array ( array[1] ) and so on.  
    In your note i underestood that i need to convert "soft returns" to the "hard returns". 
    Any help would be appreciated. Thanks.

  • Paully

    Jacob,

    I merge the code you provided to my class. received an error in runtime, says:

    Unable to load DLL (user3)


    on the following statement:

    >>Dim LineCount As Integer = SendMessageINT(TextBox1.Handle, EM_GETLINECOUNT, 0, IntPtr.Zero)<<
     



  • longxe

    to get the line count you could investigate this API <a href="http://msdn.microsoft.com/library/default.asp url=/library/en-us/shellcc/platform/commctls/editcontrols/editcontrolreference/editcontrolmessages/em_getlinecount.asp">EM_GETLINECOUNT Message </a> ,
    but i have no idea at all how to change the soft return to "hard" one,

    in order to split the string to a string array you'll need a "delimiter" in case of hard return ("\r\n") that would be easier (using String.Split method) but if the new line if created as a result of wrap property of the textbox.. .. i really like to find out how



  • MC

    Okay, got a solution for you that will retrieve each line in a multiline textbox accounting for soft and hard returns.

    First, you need the following declarations:

        Private Const EM_GETLINECOUNT As Integer = &HBA
        Private Const EM_GETLINE As Integer = &HC4
        Private Const EM_LINELENGTH As Integer = &HC1
        Private Const EM_LINEINDEX As Integer = &HBB

        Private Declare Function SendMessageINT Lib "user32" _
            Alias "SendMessageA" (ByVal hWnd As IntPtr, _
            ByVal wMsg As Integer, ByVal wParam As Integer, _
            ByVal lParam As IntPtr) As Integer


    Now, I placed this code in a button click event, but you can place it anywhere you want:

            Dim LineCount As Integer = SendMessageINT(TextBox1.Handle, EM_GETLINECOUNT, 0, IntPtr.Zero)
            Dim counter As Integer = 0
            Dim LineIndex As Integer = 0
            Dim lineLength As Integer
            Dim curLine As String = ""
            Dim stringPTR As IntPtr

            For counter = 0 To LineCount - 1
                'Get Index for line we want to retrieve
                LineIndex = SendMessageINT(TextBox1.Handle, EM_LINEINDEX, counter, IntPtr.Zero)
                'GetLineLength
                lineLength = SendMessageINT(TextBox1.Handle, EM_LINELENGTH, LineIndex, IntPtr.Zero)
                'Create the buffer
                curLine = New String("0"c, lineLength + 2)
                Mid(curLine, 1, 1) = Chr(lineLength And &HFF)
                Mid(curLine, 2, 1) = Chr(lineLength \ &H100)
                'Get the pointer for a buffer
                stringPTR = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(curLine)
                'Fill the pointer with the current line
                SendMessageINT(TextBox1.Handle, EM_GETLINE, counter, stringPTR)
                'Read the line
                curLine = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(stringPTR)
                curLine = curLine.Substring(0, lineLength)
                MessageBox.Show(curLine)
                'clear out the space
                System.Runtime.InteropServices.Marshal.FreeHGlobal(stringPTR)
                stringPTR = IntPtr.Zero
            Next


    This will display each line in your multiline textbox.  Let me know if you have questions.

  • Multi line textbox and the number of lines?