Results 1 to 4 of 4

Thread: [RESOLVED] richtexbox get row and column on click event

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Location
    Italy-Napoli
    Posts
    2,171

    Resolved [RESOLVED] richtexbox get row and column on click event

    How to print in label1 the current position of cursor in a filled richtextbox, similar wordpad

    label1="col" & cl & "-" & "row" & rw

  2. #2
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,786

    Re: richtexbox get row and column on click event

    I assume you mean the Caret (insertion point) not the Cursor (mouse pointer), correct? If so, this should do the trick:

    Code:
    Option Explicit
    
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal p_Hwnd As Long, ByVal p_Msg As Long, ByVal p_LParam As Long, ByVal p_WParam As Long) As Long
    
    Private Const EM_LINEINDEX As Long = 187
    
    Private Sub RichTextBox1_SelChange()
       Dim l_Line As Long
       Dim l_Char As Long
       
       If Me.RichTextBox1.SelLength = 0 Then
          l_Line = Me.RichTextBox1.GetLineFromChar(Me.RichTextBox1.SelStart)
          l_Char = Me.RichTextBox1.SelStart - SendMessage(Me.RichTextBox1.hWnd, EM_LINEINDEX, l_Line, 0)
          
          Debug.Print "Row: " & l_Line + 1 & ", Col: " & l_Char + 1
       Else
          Debug.Print "RTB has selection, Skipping Row/Col calcs."
       End If
    End Sub

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Location
    Italy-Napoli
    Posts
    2,171

    Re: richtexbox get row and column on click event

    Quote Originally Posted by jpbro View Post
    I assume you mean the Caret (insertion point) not the Cursor (mouse pointer), correct? If so, this should do the trick:

    Code:
    Option Explicit
    
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal p_Hwnd As Long, ByVal p_Msg As Long, ByVal p_LParam As Long, ByVal p_WParam As Long) As Long
    
    Private Const EM_LINEINDEX As Long = 187
    
    Private Sub RichTextBox1_SelChange()
       Dim l_Line As Long
       Dim l_Char As Long
       
       If Me.RichTextBox1.SelLength = 0 Then
          l_Line = Me.RichTextBox1.GetLineFromChar(Me.RichTextBox1.SelStart)
          l_Char = Me.RichTextBox1.SelStart - SendMessage(Me.RichTextBox1.hWnd, EM_LINEINDEX, l_Line, 0)
          
          Debug.Print "Row: " & l_Line + 1 & ", Col: " & l_Char + 1
       Else
          Debug.Print "RTB has selection, Skipping Row/Col calcs."
       End If
    End Sub
    work tks!

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,501

    Re: [RESOLVED] richtexbox get row and column on click event

    Another approach:

    Code:
    Option Explicit
    
    'At design-time: Picture1.Align = vbAlignBottom, .AutoRedraw = True
    '                but this might also be a Label or a StatusBar, etc.
    
    'Reference to: tom
    
    Private Const WM_USER As Long = &H400&
    Private Const EM_GETOLEINTERFACE As Long = WM_USER + 60&
    
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" ( _
        ByVal hWnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        ByVal lParam As Long) As Long
    
    Private SelectionRange As tom.ITextRange 'Range interface of the visible Selection.
    
    Private Sub Form_Load()
        Dim IUnknown As stdole.IUnknown
        Dim TextDocument1 As tom.ITextDocument
    
        SendMessage RichTextBox1.hWnd, EM_GETOLEINTERFACE, 0, VarPtr(IUnknown)
        Set TextDocument1 = IUnknown
        Set SelectionRange = TextDocument1.Selection
        RichTextBox1.LoadFile "Sample.rtf"
        RichTextBox1_SelChange 'Display initial position.
    End Sub
    
    Private Sub Form_Resize()
        If WindowState <> vbMinimized Then
            RichTextBox1.Move 0, 0, ScaleWidth, Picture1.Top
        End If
    End Sub
    
    Private Sub RichTextBox1_SelChange()
        Dim LineRange As tom.ITextRange
        
        With SelectionRange
            Picture1.Cls
            Picture1.Print " Row"; .GetIndex(tomLine);
            Set LineRange = .Duplicate 'Clone the Range of the current Selection.
            LineRange.Expand tomLine   'Expand it to find the character index of the start
                                       'of the line without disturbing the visible Selection.
            Picture1.Print " Col"; .GetIndex(tomCharacter) - LineRange.GetIndex(tomCharacter) + 1
        End With
    End Sub

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