Results 1 to 11 of 11

Thread: Reversing Selected Text in a RTB

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    10

    Question

    Hello,

    I need to able to reverse the selected text in a RTb, but line by line. I looked into the StrReverse function, but this reverses it character by character, whereas i need it to be reversed line by line. Any help please?

    Regards,

    --David Perry
    --VB 6 Ent SP4

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    I seem to remember answering a similar question some time ago.

    See this thread
    Iain, thats with an i by the way!

  3. #3
    Guest
    You could also use the Seltext property.

    Code:
    'Using lain17's reverse function:
    
    Function myRev(stString As String) As String
        Dim i As Integer
    
        For i = Len(stString) To 1 Step -1
          myRev = myRev & Mid$(stString, i, 1)
        Next i
    
    End Function
    
    
    Private Sub Command1_Click()
    Dim x
    x = myRev(RichTextBox1.SelText)
    MsgBox "Selected Reversed Text:  " & x
    End Sub

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    This is my fastrev function, for reversing large strings
    Code:
    Function fastRev(text As String) As String
    Dim a() As Byte, b() As Byte, c As Long
        a = StrConv(text, vbFromUnicode)
        c = UBound(a)
        ReDim b(c)
        For n = 0 To c
            b(c - n) = a(n)
        Next n
        fastRev = StrConv(b, vbUnicode)
    End Function
    Code:
    a=timer:b=fastrev(space(320000)):?timer-a
     0,328125 
    a=timer:b=myrev(space(32000)):?timer-a
     1,429688
    Testing in immediate window fastrev is more than 40 times faster
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    10

    Wink Almost what I want...

    Thanks for the replies, maybe i should have been a bit more descriptive in my post...

    I want it to change it like this...

    Hello me
    Hello you
    Hello all
    Hello no-one

    When selected and reversed becomes...

    Hello no-one
    Hello all
    Hello you
    Hello me

    As you can see, I don't want the characters reversed at all, more the order of the lines. Very similar to the first reply Iain17 posted at the above quoted Thread, only i need to keep the characters in original order.

    Thanks for any help.

    --David Perry
    --VB6 Ent SP4

    [Edited by DaveP on 08-11-2000 at 05:02 PM]

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Ok, should be easy:
    Code:
    Function LineRev(text As String) As String
    Dim a
        a=split(Text)
        For n=Ubound(a) to 0
           Linerev=Linerev & a(n)
        Next n
    End Function
    I guess you could use Iains mysplit as well..
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    10

    Question Ummm...

    Sorry Kedaman, I tried your example using 2 TextBoxes and a command button, all that happened was the text was copied exactly the same to the second textbox, instead of being in reverse line order.

    Appreciate your time though

    Anyone else have any ideas please?

    --David Perry
    --VB6 Ent SP4

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    10

    Thumbs up Alright then...

    I managed to get it working so-so.

    I used a StrReverse function on all the selected text, then used Iain17's Code from the above quoted thred to reverse it line by line, thus giving it as i want it.

    For some reason though, the StrReverse call places an xtra blank line in between each typed line, anyone know how to get rid of this please?

    Eg.

    Hello1
    Hello2
    Hello3

    Become...

    Hello3

    Hello2

    Hello1

    Any thoughts?

    Thanks again.

    --David Perry
    --VB6 Ent SP4

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Sorry the code had some bugs, this should work
    Code:
    Function LineRev(text As String) As String
    Dim a
        a = Split(text, vbCrLf)
        For n = UBound(a) To 0 Step -1
           LineRev = LineRev & a(n) & vbCrLf
        Next n
    End Function
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    You can try something like this:
    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
                '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)
                'Reverse text
                strBuffer = StrReverse(strBuffer)
                'Append to whole text
                strRichText = strRichText & strBuffer & vbCrLf
            Next
            'rewrite text in RichTextBox
            .Text = strRichText
        End With
        
    End Sub
    
    Private Sub Form_Load()
        With RichTextBox1
            .Text = "line one" & vbCrLf
            .Text = .Text & "line two" & vbCrLf
            .Text = .Text & "line three"
        End With
    End Sub

  11. #11

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    10

    Talking Thanks Kedaman!

    Thanks a bunch Kedaman, 5 lines of code and It does exactly what I wanted it too.

    Your a star!

    --David Perry
    --VB6 Ent SP4

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