Results 1 to 2 of 2

Thread: how do I count how many spaces across in one line???

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Leeds, UK
    Posts
    287

    Post

    Hi,

    I need to count how many spaces there are in the current line i'm in.

    Richard

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,176

    Post

    You can use the SendMessage API to get the Current Line from a Textbox/RTFBox then you can count the spaces in 2 ways..

    If you have VB6..
    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const EM_GETLINE = &HC4
    Private Const EM_LINEFROMCHAR = &HC9
    Private Const EM_LINELENGTH = &HC1
    
    Private Sub Command1_Click()
        Dim iLen As Long
        Dim iLine As Long
        Dim sLine As String
        
        iLine = SendMessage(Text1.hwnd, EM_LINEFROMCHAR, -1, 0&)
        iLen = SendMessage(Text1.hwnd, EM_LINELENGTH, Text1.SelStart, 0&)
        sLine = Space(iLen)
        Call SendMessage(Text1.hwnd, EM_GETLINE, iLine, ByVal sLine)
        MsgBox "Spaces in this line: " & (iLen - Len(Replace(sLine, " ", "")))
    End Sub
    Otherwise you have to count them yourself..
    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const EM_GETLINE = &HC4
    Private Const EM_LINEFROMCHAR = &HC9
    Private Const EM_LINELENGTH = &HC1
    
    Private Sub Command1_Click()
        Dim iLen As Long
        Dim iLine As Long
        Dim sLine As String
        Dim iSpaces As Long
        
        iLine = SendMessage(Text1.hwnd, EM_LINEFROMCHAR, -1, 0&)
        iLen = SendMessage(Text1.hwnd, EM_LINELENGTH, Text1.SelStart, 0&)
        sLine = Space(iLen)
        Call SendMessage(Text1.hwnd, EM_GETLINE, iLine, ByVal sLine)
        iLen = 0
        Do
          iLen = InStr(iLen + 1, sLine, " ")
          If iLen Then iSpaces = iSpaces + 1
        Loop While iLen
        MsgBox "Spaces in this line: " & iSpaces
    End Sub

    ------------------
    Aaron Young
    Analyst Programmer
    aarony@redwingsoftware.com
    adyoung@win.bright.net

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