-
Jun 18th, 2021, 12:06 PM
#1
Thread Starter
PowerPoster
[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
-
Jun 18th, 2021, 01:46 PM
#2
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
-
Jun 18th, 2021, 02:34 PM
#3
Thread Starter
PowerPoster
Re: richtexbox get row and column on click event
Originally Posted by jpbro
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!
-
Jun 18th, 2021, 03:35 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|