-
RTFbox text again..
I am still confused about RTFbox (got no reply to my last reply to last thread, so here it is again):
Aten: Meagtron & Gates
.getlinefromchar
tells me the line # of the current edit point,
and
.selstart
tells me the character position on the line...
but how do get the current line's TEXT as each key is pressed? (trapping in the keypress event)
-
I forgot i had this code.
Code:
Option Explicit
Private Declare Function SendMessageLong Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Long) As Long
Private Const EM_LINEFROMCHAR = &HC9
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Public Sub GetEditStatus()
Dim lLine As Long, lCol As Long
Dim cCol As Long, lChar As Long, i As Long
lChar = RichTextBox1.SelStart + 1
' Get the line number
lLine = 1 + SendMessageLong(RichTextBox1.hWnd, EM_LINEFROMCHAR, _
rtf1.SelStart, 0&)
' Get the Character Position
cCol = SendMessageLong(RichTextBox1.hWnd, EM_LINELENGTH, lChar - 1, 0&)
i = SendMessageLong(RichTextBox1.hWnd, EM_LINEINDEX, lLine - 1, 0&)
lCol = lChar - i
' Caption of Label1 is set to Cursor Position.
' This could also be a panel in a StatusBar.
Label1.Caption = lLine & ", " & lCol
End Sub
In the SelChange Event add this code:
Code:
Private Sub RichTextBox1_SelChange()
GetEditStatus
End Sub
-
Thanks.. But even your code doesn't get me the current line of TEXT
(only the line #, character position)...
-
ATommasi, that's the same thing as using the GetLineFromChar and SelStart properties. I misunderstood you at first DonW, but now I see it much clearer.
Code:
Private Function GetLineText(rtb As RichTextBox, iLine As Integer)
Dim vArray As Variant
vArray = Split(rtb, vbCrLf)
GetLineText = vArray(iLine)
End Function
Usage
'RichTextBox1 contains text like this (line numbers are not really visible):
'0) VB-World
'1) Hello
'2) World
'And the function above along with the example below will retrieve "World"
Private Sub Command1_Click()
MsgBox GetLineText(RichTextBox1, 2)
End Sub
-
OK thanks for that.. BUT this must be VERY SLOW if I have to do split of all of the RTFbox text on EVERY keypress (so as to know the latest chars on the edited line).
Surely there is another way (help!)
OR do I have to get fancy in code an keep a track of the latest line using split & what has been editied on it by keypress... ulk!
-
Actually, it's quite fast, it splits the RichTextBox up, putting each line into an array so that you can retrieve the line's text by stating which line you want.
Isn't that what you wanted?
-
Thanks Matthew,
I just thought there would have been a simple way to get the current editing line of text!. Surely a function of even more use than knowing the current line#, etc!
Useing the SPLIT I have to be very careful to kep the array in sync with the text lines: I have to do a split on every KeyPress, since the # of lines could have changed or the cursor may have moved the edit point to another line.
My text will be 200K chars long, so speed of the split may be a worry?
-
I assume you are trying to display the current line in a Label or something like that. Using the function that I posted above, it doesn't seem to be slow at all. Here is the code:
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()
Label1.Caption = GetLineText(RichTextBox1, RichTextBox1.GetLineFromChar(RichTextBox1.SelStart) + 1)
Label1.Caption = Mid$(Label1.Caption, InStr(1, Label1.Caption, Chr(32)))
End Sub