How would I extract 1 line from a rich text box, make some changes to it using a sub that i already wrote, and then put it back where it was with the changes.
Printable View
How would I extract 1 line from a rich text box, make some changes to it using a sub that i already wrote, and then put it back where it was with the changes.
1.
Find the line by splitting the text property between crlf's
Then change the item in the splitted array and join them again and put them into the text.
2.
Or jump with instr to the right position, and extract the text with mid, while you have the start and end position stored. Then change the text and use left and mid to connect the left part, the text and the right part of the rtf.
Could you write some code for it to help me on my way? Thanks...
Ok the the split-join is probably easy, but i don't konw about the performance. I write the other later if you need it instead
If this doesn't work then it's because i use vb5, and i haven't seen how to use split and join functions, but they probably are used like this:)Code:Dim a
a=split(rtfbox.text,vbcrlf)
a(yourlinenumber)=Yourtext
rtfbox.text=Join(a)
Actually, the right way to do it is to use APIs to get the line from the RichTextBox or TextBox:
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 Declare Function SendMessageString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
Private Const EM_GETLINE = &HC4
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Private Sub Command1_Click()
Dim strBuffer As String
Dim lngLineLength As Long
Dim lngIndex As Long
'Get the Index for the first line
'Third parameter in the function specifies the line number (zero based)
lngIndex = SendMessage(RichTextBox1.hwnd, EM_LINEINDEX, 0, 0)
'Get the length of the line
lngLineLength = SendMessage(RichTextBox1.hwnd, EM_LINELENGTH, lngIndex, 0)
'Prepare buffer
strBuffer = Space(lngLineLength)
'Get the line from RichTextBox
Call SendMessageString(RichTextBox1.hwnd, EM_GETLINE, i, ByVal strBuffer)
End Sub