CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Sep 2003
    Posts
    10

    Removing the last two lines of a RichTextBox

    Does anyone know how to remove the last two lines of a RichTextBox?

  2. #2
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    25

    Re: Removing the last two lines of a RichTextBox

    Using normal textbox:

    PHP Code:

    Private Sub Command1_Click()
        
    UBound(Split(Text1.TextvbCr))
        
    '
        If i >= 1 Then
            texto = Text1.Text
            '
            
    0
            
    For Len(textoTo 0 Step -1
                
    If Mid(textot1) = vbCr Then
                    c 
    1
                    texto 
    Mid(texto11)
                
    End If
                
    '
                If c = 2 Then GoTo endf
            Next
            '
    endf:
            
    Text1.Text texto
        End 
    If
        
    '
    End Sub 

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Removing the last two lines of a RichTextBox

    or RTB: although it doesn't remove lines, it lets you find them and edit.


    Code:
    Option Explicit
    
    Private Declare Function SendMessage 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_LINELENGTH = &HC1
    
    
    Private Function ColorWords(ByRef rtb As RichTextBox, _
                                ByRef sFindString As String, _
                                ByVal lColor As Long) As Integer
    On Error GoTo ErrHandler
        Dim lFindLength         As Long
        Dim lFoundPos           As Long
        Dim lTempPos            As Long
        Dim iOptions            As Integer
        Dim iMatchCount         As Integer
        
        ' Set search options
        iOptions = rtfNoHighlight 'used by default here
        If Check1(0).Value = 1 Then iOptions = iOptions + rtfWholeWord
        If Check1(1).Value = 1 Then iOptions = iOptions + rtfMatchCase
        
        ' Save the string-length
        lFindLength = Len(sFindString)
        
        ' Search for a single match. Find method returns
        ' the position of the first character of the item,
        ' or -1 if no matches are found.
        lFoundPos = rtb.Find(sFindString, 0, , iOptions)
        
        ' Loop until all occurences are found
        Do Until lFoundPos < 0
            iMatchCount = iMatchCount + 1
            rtb.SelStart = lFoundPos
            rtb.SelLength = lFindLength
            rtb.SelColor = lColor
            lTempPos = lFoundPos + lFindLength
            lFoundPos = rtb.Find(sFindString, lTempPos, , iOptions)
        Loop
            
        ' Return the number of matches
        ColorWords = iMatchCount
        Exit Function
        
    ErrHandler:
        MsgBox "Unexpected error number-" & _
            CStr(Err.Number) & _
            " occurred in " & Err.Source & _
            ":" & vbCrLf & vbCrLf & Err.Description
    End Function
    
    Private Sub Command1_Click()
       ColorWords RichTextBox1, txtSearch.Text, vbRed
    End Sub
    
    
    Private Sub RichTextBox1_Click()
    
    
        Dim strBuffer As String
        Dim lngLength As Long
        Dim intCurrentLine As Integer
        Dim lngLineNumber As Long
            With RichTextBox1
            intCurrentLine = .GetLineFromChar(.SelStart)
            'get line length
            lngLength = SendMessage(.hwnd, EM_LINELENGTH, intCurrentLine, 0)
            'resize buffer
            strBuffer = Space(lngLength)
            'get line text
            Call SendMessage(.hwnd, EM_GETLINE, intCurrentLine, ByVal strBuffer)
            
            lngLineNumber = .GetLineFromChar(.SelStart)
            MsgBox "You selected line number " & lngLineNumber + 1 & " which says " & strBuffer
        End With
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Apr 2009
    Posts
    394

    Re: Removing the last two lines of a RichTextBox

    see my answer to your question at this/in this thread...


    http://www.xtremevbtalk.com/showthre...32#post1336232



    Good Luck

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Removing the last two lines of a RichTextBox

    If I may add a contribution to this:
    Code:
    Private RemoveLast2Lines(TBox as control)
      Dim p&#37;, i%, a$
      If Right$(TBox.Text, 2) = vbCrLf Then a$ = Left$(TBox.Text, Len(TBox.Text) - 2) Else a$ = TBox.Text
      i = InStrRev(a$, vbCrLf)
      If i Then i = InStrRev(a$, vbCrLf, i)
      If i > 1 Then TBox.Text = Left$(a$, i - 1)
    End Sub
    This takes in account, that the last line might be terminated already with a CrLf.
    @Ash: Your code could become slow if the box contains a rather long text.
    It would work better if you avoid the texto = Mid(texto, 1, t - 1)
    It causes a lot of unnecessary string copy operations
    better determine the position only and then do one copy at the end:
    Code:
           For t = Len(texto) To 0 Step -1
                If Mid(texto, t, 1) = vbCr Then
                    c = c + 1
                    'texto = Mid(texto, 1, t - 1) don't
                End If
                '
                If c = 2 Then Exit For
            Next
    
            Text1.Text = left$(texto,t-1)

  6. #6
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    25

    Re: Removing the last two lines of a RichTextBox

    Quote Originally Posted by WoF View Post
    @Ash: Your code could become slow if the box contains a rather long text.
    It would work better if you avoid the texto = Mid(texto, 1, t - 1)
    It causes a lot of unnecessary string copy operations
    better determine the position only and then do one copy at the end:
    Code:
           For t = Len(texto) To 0 Step -1
                If Mid(texto, t, 1) = vbCr Then
                    c = c + 1
                    'texto = Mid(texto, 1, t - 1) don't
                End If
                '
                If c = 2 Then Exit For
            Next
    
            Text1.Text = left$(texto,t-1)
    Really, your way is faster.
    Thanks for the tip
    And i forgot to use "Exit For" because my first idea was with While/Wend.

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Removing the last two lines of a RichTextBox

    Yeah, old While...Wend has no Exit While
    If you need to exit While...wend on early condition you can use the Do While... Exit Do... Loop construct.

    The first sample using 2 consecutive InstrRev() calls should even be faster than the character oriented loop.

  8. #8
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    25

    Re: Removing the last two lines of a RichTextBox

    Quote Originally Posted by WoF View Post
    Yeah, old While...Wend has no Exit While
    If you need to exit While...wend on early condition you can use the Do While... Exit Do... Loop construct.

    The first sample using 2 consecutive InstrRev() calls should even be faster than the character oriented loop.
    Thanks, but i know about Do Loop. I just use While because everyone does where i work (i really can't understand, but this is the template....).

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Removing the last two lines of a RichTextBox

    hmm I can't even remember the last time I used a while wend loop but it was likely in the gosub days.

    I always use for next, do loop, do while or loop while loops depending on the objective at hand. While wend is basically obsolete and is very limited compared to the do construct.

  10. #10
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Removing the last two lines of a RichTextBox

    Mainly because it has no Exit While.
    The Do loop incorporates all possibilities including Loop Until

  11. #11
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Removing the last two lines of a RichTextBox

    Quote Originally Posted by WoF View Post
    Mainly because it has no Exit While.
    The Do loop incorporates all possibilities including Loop Until
    GOTO doesn't count?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  12. #12
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Removing the last two lines of a RichTextBox


    Do YOU consider exiting a While Wend with a Goto Label as good programming manners?

    I use Goto only with On Error

  13. #13
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    106

    Re: Removing the last two lines of a RichTextBox

    Why not split on crlf, redim the upperbound-2 and join on crlf again ?
    Would be a lot faster

  14. #14
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Removing the last two lines of a RichTextBox

    Quote Originally Posted by WoF View Post

    Do YOU consider exiting a While Wend with a Goto Label as good programming manners?

    I use Goto only with On Error
    Never said that. Just commenting on your post...

    Quote Originally Posted by WoF View Post
    Mainly because it has no Exit While
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  15. #15
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Removing the last two lines of a RichTextBox

    @Teranoz:
    If you consider a large amount of lines (coupla 1000) it is surely faster, to step back through the text character by character to find the CrLf you want.

    Splitting will cause the creation of a huge string array, duplicate all string data from .Text, and in the end you put everything back together. Might be a little faster, if a .Text contains only some 20 or 30 lines, but will cause huge string copying operations with larger amounts of text. The other method needs only one string copy operation.
    Nevertheless, splitting is certainly one more approach for this, as we often find more'n one solution for a problem.


    @David:
    Quote Originally Posted by dglienna View Post
    Never said that. Just commenting on your post...
    Ok. Then the answer is: Goto doesn't count. Not for me.

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

Featured