Results 1 to 6 of 6

Thread: RTFbox: get the cursor position..

  1. #1
    donW
    Guest

    RTFbox: get the cursor position..

    thanks everyone who has helped me with RTXbox control.
    I can now get the text line at the cursor using GETLINETEXT and GETLINEFROMCHAR

    BUT how can I find where the cursor is in terms of the character position from the start of the current cursor text line?
    .SelStart returns the position from the start of the text, NOT the start of the current line.

  2. #2
    Matthew Gates
    Guest
    Using the old code I gave from your previous thread, you can use a textbox to get where the cursor is from the start of the current line.


    Code:
    Private Function GetLineText(rtb As RichTextBox, iLine As Integer)
        Dim vArray As Variant
        vArray = Split(rtb, vbCrLf)
        GetLineText = vArray(iLine)
    End Function
    
    
    Private Sub RichTextBox1_Change()
        Text1.Text = GetLineText(RichTextBox1, RichTextBox1.GetLineFromChar(RichTextBox1.SelStart) + 1)
        Text1.Text = Mid$(Text1.Text, InStr(1, Text1.Text, Chr(32)))
        Text1.SelStart = RichTextBox1.SelStart
        Caption = Text1.SelStart
    End Sub

  3. #3
    donW
    Guest
    Matthew:
    what is the Text1 variable type? I am using a string or a label, but it dosn't have the .selstart property.
    Can you explain how the RTF's .selstart (which = char position for start of file) be used to get char position from start of current line?

  4. #4
    donW
    Guest
    Matthew:
    what is the Text1 variable type? I am using a string or a label, but it dosn't have the .selstart property.
    Can you explain how the RTF's .selstart (which = char position for start of file) be used to get char position from start of current line?

  5. #5
    Matthew Gates
    Guest
    Use the code above, and make the textbox invisible if you wish to use a Label instead.

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Try this code:
    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_LINEINDEX = &HBB
    
    Public Function GetColPos(rtb As RichTextBox)
        Dim nPos As Long
        
        nPos = SendMessage(rtb.hwnd, EM_LINEINDEX, -1, 0)
        GetColPos = rtb.SelStart - nPos + 1
    End Function
    You can simply call the GetColPos in the SelChange event and assign the return value to a label or whatever:
    Code:
    Private Sub RichTextBox1_SelChange()
        Label1.Caption = "Col position: " & GetColPos(RichTextBox1)
    End Sub
    Best regards

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