Dear All,
How to remove a line from the richtextbox, by passing the line number in API Sendmessage?
Printable View
Dear All,
How to remove a line from the richtextbox, by passing the line number in API Sendmessage?
Here's something from the API guide that I modified to do what you want.
VB 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 SendMessageStr 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_GETLINECOUNT = &HBA Private Const EM_LINEINDEX = &HBB Private Const EM_LINELENGTH = &HC1 Private Sub Command1_Click() Dim lngCount As Long Dim lngLineIndex As Long Dim lngLength As Long Dim strBuffer As String Dim strRichText As String Dim i As Integer 'Get Line count lngCount = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0) With RichTextBox1 For i = 0 To lngCount - 1 If i <> 1 Then ' If you wanted to get rid of the second line 'Get line index lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0) 'get line length lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0) 'resize buffer strBuffer = Space(lngLength) 'get line text Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer) 'Accumulate the text to be kept strRichText = strRichText & strBuffer & vbCrLf End If Next 'rewrite text in RichTextBox .Text = strRichText End With End Sub
My version
VB 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 LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long Private Const EM_SETSEL = &HB1 Private Const EM_GETLINECOUNT = &HBA Private Const EM_LINEINDEX = &HBB Private Sub DeleteLine(pLineNumber As Long) Dim chrsToStart As Long, chrsToEnd As Long, lineCount As Long With RichTextBox1 lineCount = SendMessage(.hwnd, EM_GETLINECOUNT, 0, ByVal 0&) Select Case True Case pLineNumber > lineCount - 1 Exit Sub Case pLineNumber = 0 And lineCount = 1 chrsToStart = 0 chrsToEnd = Len(.Text) Case pLineNumber = lineCount - 1 chrsToStart = SendMessage(.hwnd, EM_LINEINDEX, pLineNumber, ByVal 0&) - 1 chrsToEnd = Len(.Text) - chrsToStart + 1 Case Else chrsToStart = SendMessage(.hwnd, EM_LINEINDEX, pLineNumber, ByVal 0&) chrsToEnd = SendMessage(.hwnd, EM_LINEINDEX, pLineNumber + 1, ByVal 0&) - chrsToStart End Select .SetFocus Call SendMessage(.hwnd, EM_SETSEL, chrsToStart, ByVal chrsToEnd) LockWindowUpdate .hwnd .SelStart = chrsToStart .SelLength = chrsToEnd .SelText = vbNullString LockWindowUpdate False End With End Sub Private Sub Command1_Click() DeleteLine 2 'Deletes line 2 (its the third line, because first line is line 0) End Sub
Dear Martin,
Will this support a datasize of 4MB.In the meanwhile I am trying JCIS code :)
Sorry but I have no idea.Quote:
Originally Posted by danasegarane
Great stuff guys. The only problem now is that it changes all the color.
now what could be a good work around for that one?