-
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
-
I seem to remember answering a similar question some time ago.
See this thread
-
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
-
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
-
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]
-
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..
-
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
-
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
-
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
-
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
-
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