Results 1 to 6 of 6

Thread: Delete Line

  1. #1

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Cool Delete Line

    Dear All,
    How to remove a line from the richtextbox, by passing the line number in API Sendmessage?
    Please mark you thread resolved using the Thread Tools as shown

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Delete Line

    Here's something from the API guide that I modified to do what you want.

    VB Code:
    1. 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
    2. 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
    3. Private Const EM_GETLINE = &HC4
    4. Private Const EM_GETLINECOUNT = &HBA
    5. Private Const EM_LINEINDEX = &HBB
    6. Private Const EM_LINELENGTH = &HC1
    7. Private Sub Command1_Click()
    8.     Dim lngCount As Long
    9.     Dim lngLineIndex As Long
    10.     Dim lngLength As Long
    11.     Dim strBuffer As String
    12.     Dim strRichText As String
    13.     Dim i As Integer
    14.     'Get Line count
    15.     lngCount = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0)
    16.     With RichTextBox1
    17.         For i = 0 To lngCount - 1
    18.             If i <> 1 Then ' If you wanted to get rid of the second line
    19.                 'Get line index
    20.                 lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0)
    21.                 'get line length
    22.                 lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0)
    23.                 'resize buffer
    24.                 strBuffer = Space(lngLength)
    25.                 'get line text
    26.                 Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer)
    27.                 'Accumulate the text to be kept
    28.                 strRichText = strRichText & strBuffer & vbCrLf
    29.             End If
    30.         Next
    31.         'rewrite text in RichTextBox
    32.         .Text = strRichText
    33.     End With
    34. End Sub

  3. #3
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Delete Line

    My version
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    2.             (ByVal hwnd As Long, ByVal wMsg As Long, _
    3.              ByVal wParam As Long, lParam As Any) As Long
    4.  
    5. Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    6.  
    7. Private Const EM_SETSEL = &HB1
    8. Private Const EM_GETLINECOUNT = &HBA
    9. Private Const EM_LINEINDEX = &HBB
    10.  
    11. Private Sub DeleteLine(pLineNumber As Long)
    12.     Dim chrsToStart As Long, chrsToEnd As Long, lineCount As Long
    13.    
    14.     With RichTextBox1
    15.         lineCount = SendMessage(.hwnd, EM_GETLINECOUNT, 0, ByVal 0&)
    16.        
    17.         Select Case True
    18.             Case pLineNumber > lineCount - 1
    19.                 Exit Sub
    20.             Case pLineNumber = 0 And lineCount = 1
    21.                 chrsToStart = 0
    22.                 chrsToEnd = Len(.Text)
    23.             Case pLineNumber = lineCount - 1
    24.                 chrsToStart = SendMessage(.hwnd, EM_LINEINDEX, pLineNumber, ByVal 0&) - 1
    25.                 chrsToEnd = Len(.Text) - chrsToStart + 1
    26.             Case Else
    27.                 chrsToStart = SendMessage(.hwnd, EM_LINEINDEX, pLineNumber, ByVal 0&)
    28.                 chrsToEnd = SendMessage(.hwnd, EM_LINEINDEX, pLineNumber + 1, ByVal 0&) - chrsToStart
    29.         End Select
    30.                              
    31.         .SetFocus
    32.         Call SendMessage(.hwnd, EM_SETSEL, chrsToStart, ByVal chrsToEnd)
    33.        
    34.         LockWindowUpdate .hwnd
    35.             .SelStart = chrsToStart
    36.             .SelLength = chrsToEnd
    37.             .SelText = vbNullString
    38.         LockWindowUpdate False
    39.     End With
    40. End Sub
    41.  
    42. Private Sub Command1_Click()
    43.     DeleteLine 2 'Deletes line 2 (its the third line, because first line is line 0)
    44. End Sub

  4. #4

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Delete Line

    Dear Martin,
    Will this support a datasize of 4MB.In the meanwhile I am trying JCIS code
    Please mark you thread resolved using the Thread Tools as shown

  5. #5

  6. #6
    Junior Member
    Join Date
    Aug 2008
    Posts
    21

    Re: Delete Line

    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?

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