Results 1 to 9 of 9

Thread: [RESOLVED] Count the number of lines in a textbox?

  1. #1

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Resolved [RESOLVED] Count the number of lines in a textbox?

    Hello, does anybody know how to count the number of lines that are in a multi-line textbox? Thank you.

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Count the number of lines in a textbox?

    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Count the number of lines in a textbox?

    Thanks for the link. I used the following API call. However this does not work with a TextBox you must use a Rich Edit Control for this to return the correct results.

    Code:
    Private Declare Function SendMessageAsLong Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Const EM_GETLINECOUNT = 186
    Code:
    MsgBox SendMessageAsLong(MyRichEditBox.hWnd, EM_GETLINECOUNT, 0, 0)

  4. #4
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: [RESOLVED] Count the number of lines in a textbox?

    You could also use
    Code:
        Dim Lns() As String
        Lns() = Split(Text1.Text, vbNewLine)
        MsgBox "There are " & UBound(Lns()) + 1 & " lines in textbox 1"
    But for a lot of lines it might be a bit slow.

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: [RESOLVED] Count the number of lines in a textbox?

    EM_GETLINECOUNT works fine with a standard TextBox

  6. #6

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: [RESOLVED] Count the number of lines in a textbox?

    It doesn't work with a textbox for me. I have some text that is 22 lines long, in a textbox it returns 7 using EM_GETLINECOUNT, in a rich edit box it returns the correct 22.

  7. #7
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: [RESOLVED] Count the number of lines in a textbox?

    works fine for me

    Attached Files Attached Files

  8. #8

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: [RESOLVED] Count the number of lines in a textbox?

    I think it may have to do with how each control does word-wrapping. Try adding a Rich Edit box to your form and increase the amount of characters in your Text2 like this and the results will not be the same between the two controls:

    Code:
    Text2.Text = String(22 * 22, "A")
        
    RichTextBox1.Text = String(22 * 22, "A")

  9. #9
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: [RESOLVED] Count the number of lines in a textbox?

    still not getting any problems (see attached) - you might get 1 or 2 lines difference do to character spacing (assuming the controls are the same width) - but EM_GETLINECOUNT still returns the correct number of lines.

    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width