Number of lines in a multiline textbox

Posted on 2006-04-25
Last Modified: 2013-12-25
How can I get the number of lines typed in a multiline textbox control?
Question by:nashuald
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    LVL 17

    Accepted Solution

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long

    Private Sub Command1_Click()

      MsgBox SendMessage(RichTextBox1.hWnd, EM_GETLINECOUNT, 0, 0)

    End Sub
    LVL 19

    Expert Comment

    Or if you want a very un-orthodox method:

    dim strString as string
    dim numLines as integer
    dim myArray() as string
    strString = text1.text
    myArray() = Split(strString,vbcrlf)
    numLines = myArray.UBound

    msgbox "The text box has : " & numLines & " Lines"

    I do not recommend using this method in any commerical application.

    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    Brian: the split method only returns the number of hard breaks (via the crlf characters), but not those lines that are wrapped by the length of the line...
    LVL 19

    Expert Comment

    angelIII: You are very right.

    I was just providing it as an example. Nothing more. I would never recommmend anything like that in a serious application.


    Author Comment

    Thank's everybody!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now