|
-
Aug 10th, 2000, 07:21 PM
#1
Thread Starter
New Member
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
-
Aug 11th, 2000, 03:29 AM
#2
Fanatic Member
I seem to remember answering a similar question some time ago.
See this thread
Iain, thats with an i by the way!
-
Aug 11th, 2000, 12:59 PM
#3
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
-
Aug 11th, 2000, 03:45 PM
#4
transcendental analytic
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.
-
Aug 11th, 2000, 03:59 PM
#5
Thread Starter
New Member
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]
-
Aug 11th, 2000, 06:56 PM
#6
transcendental analytic
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.
-
Aug 11th, 2000, 10:23 PM
#7
Thread Starter
New Member
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
-
Aug 11th, 2000, 11:06 PM
#8
Thread Starter
New Member
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
-
Aug 12th, 2000, 06:11 AM
#9
transcendental analytic
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.
-
Aug 12th, 2000, 12:41 PM
#10
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
-
Aug 12th, 2000, 06:51 PM
#11
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|