Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Backwards text boxes??? 1

Status
Not open for further replies.

goatsaregreat

Programmer
Mar 21, 2001
82
GB
Is there some way to change the text in a text box to read the words backwards? For instance, I want to be able to put ,"Hello" in a text box, click a command buttton, and have the box read ,"olleH". I also would like to use another button to change it back to normal. Is there some semi-simple way to do this, or it is almost impossible. Thanks for the help.
 
Just a quick idea...

You could use the Right function and read it from the right one character at a time then each time you read a char, reassign the variable holding the value in the text box to be all but the last char you just read, and keep going until you are at the beginning of the text.

Make sense?


 
Try using

StrReverse(Text1)

It will return an error if the string is NULL.
 
I had no idea there was a function that did that... Thanks for the info!
 
Hi,

I remember the good old days when you had to program code. Very soon they'll be a command for everything :) .

10 SWITCHON computer(supress user input)
20 DESIGNPROGRAM (game,original,platform,level,lives=3)
30 MAKEEXE (install, selfextract,burn-cd)
40 SWITCHOFF computer(no prompts)

In the olden days to reverse a text box we would have done this

A$=textbox.text
textbox.text=""
for cnt= len(A$) to 1 step -1
textbox.text = textbox.text + mid$(a$,cnt,1)
next cnt

Ah the good old days !
Any Help, Yes/No let me know

Regards

________________________________________
Is not a fool a wise man in his own eyes - Proverbs
 
I still have one problem. None of these seem to work in a multiline textbox. Is there some way to correct that? It soes work, but it shows two dark vertical lines wherever there should be a new line. Any thoughts?
 
Hi,

Ah good old fashioned programming problems.

Your lines appear because the carriage return, line feed at the end of the line are being reversed.

Try this modification to the code, I tried it here and it works ok.

a$ = Text1.Text
Text1.Text = ""

For cnt = Len(a$) To 1 Step -1

B$ = Mid$(a$, cnt, 1)
If B$ = Chr$(10) Then B$ = Chr$(13) + Chr$(10)
If B$ = Chr$(13) Then B$ = ""
Text1.Text = Text1.Text + B$

Next cnt

Any Help, Yes/No let me know

Regards

________________________________________
Is not a fool a wise man in his own eyes - Proverbs
 
If you have VB6, you can use the very nifty
Replace function

e.g.

Private Sub Command1_Click()
Dim strString As String

strString = Text1.Text
strString = Replace(strString, Chr(13), " ")
strString = Replace(strString, Chr(10), " ")
strString = StrReverse(strString)
Text1.Text = strString
End Sub

:)
 
Actually, Laffalots code is much better than mine
forget I spoke.

lol :~/
 
Multline text can be reversed by the following one-liner:

Private Sub Command1_Click()
Text1.Text = StrReverse$(Replace$(Text1.Text, vbCrLf, vbNullString))
End Sub
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Ya, but Laffalots code is still nicer,
since it puts the carriage return, line feeds
back in.

 
Hi,

Why thank you nonguru I'm pleased you think so.

Also on another point, as this is a VB5 & 6 forum I
wrote it for VB 5.

Reason being it should work in both. The Replace & StrReverse commands kick out errors in my old version of VB5 and the original post never said what his VB version was.

(Thanks again nonguru)
Any Help, Yes/No let me know

Regards

________________________________________
Is not a fool a wise man in his own eyes - Proverbs
 
You can step foreward by prepending the new data to the string...



Wil Mead
wmead@optonline.net

 
Thanks alot "laffalot"! It works perfect! BTW, I have VB6 Prof. Ed.
 
nonguru et. al : it really depends on what you want to achieve:

If you want to reverse the text as a whole, whilst keeping the NewLines, you can try this:
Private Sub Command1_Click()
' Reverses a Multiline text in its entirety
' with respect for vbCrLf's


Dim vbLfCr As String
vbLfCr = StrReverse(vbCrLf)
Text2.Text = Replace(StrReverse(Text1.Text), vbLfCr, vbCrLf)

End Sub


If to the contrary, you want to reverse the text line by line, then this is a fast and valid (VB6) alternative:
Private Sub Command2_Click()
' Replaces a multline text, line by line
' If vbCrLf's are present


Dim a_str() As String
a_str = Split(Text1.Text, vbCrLf)

Dim i As Integer
For i = 0 To UBound(a_str)
a_str(i) = StrReverse(a_str(i))
Next

Text2.Text = Join(a_str, vbCrLf)

End Sub

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top