How to trim a richtextbox

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 5022 Views - Last Post: 28 February 2011 - 01:32 PM Rate Topic: -----

#1 HRT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-February 11

How to trim a richtextbox

Posted 23 February 2011 - 07:44 PM

Hi,
I am trying to remove vbcrlf and blank spaces from the end of text in a richtextbox (rtf) control. The rtf is formatted with different fontsizes. I tried the Trim function below which works for text box control but it did not work for rtf:
Text1.TextRTF=Trim(Text1.TextRTF)

What is it that I am doing wrong?
Is This A Good Question/Topic? 0
  • +

Replies To: How to trim a richtextbox

#2 Elda  Icon User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Re: How to trim a richtextbox

Posted 23 February 2011 - 08:12 PM

Follow this http://www.vbforums....ad.php?t=504704 maybe this could help...
Was This Post Helpful? 0
  • +
  • -

#3 HRT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-February 11

Re: How to trim a richtextbox

Posted 23 February 2011 - 08:51 PM

That seems to be related to vb.net.
Do you have any suggestions for vb6?
Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 Elda  Icon User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Re: How to trim a richtextbox

Posted 23 February 2011 - 11:11 PM

http://www.dreaminco...93-string-type/ else google some... :)
Was This Post Helpful? 0
  • +
  • -

#5 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,768
  • Joined: 26-March 09

Re: How to trim a richtextbox

Posted 24 February 2011 - 01:23 AM

Trim will only remove spaces from the end of a string, if they are at the end of the string (i.e. "Hello " will become "Hello", but if the string is "Hello " & CRLF, then it will remain the same).

You could use the INSTRREV function to search backwards from the end to see where the CRLF character is, then remove it, then do your trim. It really depends on what will be at the end of the text. Will it always be the same???
Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: How to trim a richtextbox

Posted 24 February 2011 - 11:34 AM

View Postmaj3091, on 24 February 2011 - 08:23 AM, said:

Trim will only remove spaces from the end of a string[...]
Let me clarify that a bit. Trim trims spaces from both ends, LTrim from the left end, and RTrim from the right end.
debug.print len(trim (space(4) & "ABC" & space(3)))
debug.print len(ltrim (space(4) & "ABC" & space(3)))
debug.print len(rtrim (space(4) & "ABC" & space(3)))
returns 3, 6, and 7.

This post has been edited by BobRodes: 24 February 2011 - 11:35 AM

Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: How to trim a richtextbox

Posted 24 February 2011 - 11:48 AM

also,
debug.print len(trim(left("ABCDE     " & vbcrlf, len("ABCDE" & space(4) & vbcrlf)-2)))
returns 5. So, given a string Text1.TextRTF that ends in VBCrLf, you could do this to trim spaces in front of the carriage return:
x = Trim(Left(Text1.TextRTF, len(Text1.TextRTF) - 2)) & VBCrLf
Note that the length of VBCrLf is 2, because it's both a carriage return and a line feed, and note also that the code puts the CrLf back in after stripping out trailing spaces.
Was This Post Helpful? 0
  • +
  • -

#8 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,768
  • Joined: 26-March 09

Re: How to trim a richtextbox

Posted 24 February 2011 - 01:58 PM

View PostBobRodes, on 24 February 2011 - 11:34 AM, said:

View Postmaj3091, on 24 February 2011 - 08:23 AM, said:

Trim will only remove spaces from the end of a string[...]
Let me clarify that a bit. Trim trims spaces from both ends, LTrim from the left end, and RTrim from the right end.
debug.print len(trim (space(4) & "ABC" & space(3)))
debug.print len(ltrim (space(4) & "ABC" & space(3)))
debug.print len(rtrim (space(4) & "ABC" & space(3)))
returns 3, 6, and 7.


ooops - thanks for the correction Bob.
Was This Post Helpful? 0
  • +
  • -

#9 HRT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-February 11

Re: How to trim a richtextbox

Posted 24 February 2011 - 02:23 PM

Thank you BobRodes. I tried your suggestion and it works for removing the extra vbcrlf from the end of the text box if I use the code like this:
Text1.Text = Trim(Left(Text1.Text, Len(Text1.Text) - 16))

But it resets all font formattings to default.
If I use the code like this:
Text1.TextRTF = Trim(Left(Text1.TextRTF, Len(Text1.TextRTF) - 16))

The formatting of the RTF box changes to XML and all vbcrlf appear in the RTF box as this:
\par
\par
\par \ulnone\b0
\par
\par
\par
\par \ul\b
\par
\par
\par

View Postmaj3091, on 24 February 2011 - 01:23 AM, said:

Trim will only remove spaces from the end of a string, if they are at the end of the string (i.e. "Hello " will become "Hello", but if the string is "Hello " & CRLF, then it will remain the same).

You could use the INSTRREV function to search backwards from the end to see where the CRLF character is, then remove it, then do your trim. It really depends on what will be at the end of the text. Will it always be the same???

Hi maj3091,
Yes the number of vbcrlf and spaces at the end of the RTF box are always the same. The text inside the RTF box has different font formatting for different lines (some lines are bolded and some are larger fontsizes). So I don't want to loose the formatting of the rtf box after triming or removing the extra vbcrlf and spaces.
Was This Post Helpful? 0
  • +
  • -

#10 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: How to trim a richtextbox

Posted 24 February 2011 - 04:16 PM

I'm not sure exactly what characters the RTF string is using for formatting. If you were able to print the entire RTF string out verbatim that might help us work out a way to bypass them.
Was This Post Helpful? 0
  • +
  • -

#11 HRT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-February 11

Re: How to trim a richtextbox

Posted 24 February 2011 - 06:46 PM

View PostBobRodes, on 24 February 2011 - 04:16 PM, said:

I'm not sure exactly what characters the RTF string is using for formatting. If you were able to print the entire RTF string out verbatim that might help us work out a way to bypass them.

Hi BobRodes,
Here is the code I am using:
Private Sub Command1_Click()
Text1(12).SelBold = True
Text1(12).SelUnderline = True
Text1(12).TextRTF = "TITLE 1 HERE" & vbCrLf
Text1(12).SelStart = Len(Text1(12).Text)
Text1(12).SelText = Text1(0).TextRTF & vbCrLf      'add the text for Title 1
Text1(12).SelStart = Len(Text1(12).Text)
Text1(12).SelText = vbCrLf & "TITLE 2 HERE" & vbCrLf
Text1(12).SelStart = Len(Text1(12).Text)
Text1(12).SelText = Text1(1).TextRTF & vbCrLf      'add the text for Title 2
Text1(12).SelText = vbCrLf & "TITLE 3 HERE" & vbCrLf
Text1(12).SelStart = Len(Text1(12).Text)
Text1(12).SelText = Text1(2).TextRTF & vbCrLf     'add the text for Title 3
' Add a vbCrLf.
Text1(12).SelStart = Len(Text1(12).Text)
Text1(12).SelLength = 0
Text1(12).SelText = vbCrLf & "TITLE 4 HERE" & vbCrLf

Text1(12).SelStart = Len(Text1(12).Text)
Text1(12).SelText = Text1(3).TextRTF                    'add the text (part 1) for Title 4

Text1(12).SelStart = Len(Text1(12).Text) - 1
Text1(12).SelText = Text1(6).TextRTF                    'add the text (part 2) for Title 4
Text1(12).SelStart = Len(Text1(12).Text) - 3
Text1(12).SelText = Text1(16).TextRTF                 'add the text (part 3) for Title 4
Text1(12).SelStart = Len(Text1(12).Text) - 5
Text1(12).SelText = Text1(9).TextRTF                   'add the text (part 4) for Title 4

' Add a vbCrLf for the new paragraph
Text1(12).SelStart = Len(Text1(12).Text)
Text1(12).SelLength = 0
Text1(12).SelText = vbCrLf & vbCrLf

Text1(12).SelStart = Len(Text1(12).Text) - 7
Text1(12).SelText = Text1(8).TextRTF                    'add the text (part 5) for Title 4
Text1(12).SelStart = Len(Text1(12).Text) - 9

' Add a vbCrLf for the new paragraph
Text1(12).SelStart = Len(Text1(12).Text)
Text1(12).SelLength = 0
Text1(12).SelText = vbCrLf & vbCrLf

Text1(12).SelStart = Len(Text1(12).Text) - 11
Text1(12).SelText = Text1(11).TextRTF                 'add the text (part 6) for Title 4
Text1(12).SelStart = Len(Text1(12).Text) - 13
Text1(12).SelText = Text1(10).TextRTF                 'add the text (part 7) for Title 4

Text1(12).SelStart = Len(Text1(12).Text) - 15
Text1(12).SelBold = False
Text1(12).SelUnderline = False
Text1(12).SelText = "This is the part 8 of the Title 4. " & vbCrLf            'add the text (part 8) for Title 4

Text1(12).SelBold = True
Text1(12).SelUnderline = True
Text1(12).SelText = vbCrLf & "TITLE 5" & vbCrLf
Text1(12).SelStart = Len(Text1(12).Text) - 15
Text1(12).SelBold = False
Text1(12).SelUnderline = False
Text1(12).SelText = "This is the text for Title 5." & vbCrLf     'add the text for Title 5

Text1(12).SelBold = True
Text1(12).SelUnderline = True
Text1(12).SelText = vbCrLf & "TITLE 6" & vbCrLf
Text1(12).SelStart = Len(Text1(12).Text) - 15
Text1(12).SelBold = False
Text1(12).SelUnderline = False
Text1(12).SelText = "This is the text for Title 6." & vbCrLf    'add the text for Title 6

Text1(12).SelBold = True
Text1(12).SelUnderline = True
Text1(12).SelText = vbCrLf & "DATE:" & vbCrLf
Text1(12).SelStart = Len(Text1(12).Text) - 15
Text1(12).SelBold = False
Text1(12).SelUnderline = False
Text1(12).SelText = Format(Now, "long date") & "  at " & Time   ' add the Date and Time
End Sub


It seems there are 8 vbcrlf that I have been adding to the text1(12) and they all appear at the end of the texts inside the Text1(12). The font property of the Text1(12).TextRTF is Arial, FontSize=12, FontUnderline=True
Thanks again for taking the time to help me out.
Was This Post Helpful? 0
  • +
  • -

#12 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: How to trim a richtextbox

Posted 24 February 2011 - 07:53 PM

Scary! :) Ok, looks like this could be simpler than you're making it. If you could give a synopsis of what you're trying to do, we can probably considerably shorten your code. Suppose you take your code up to line 17 and tell me exactly what you are trying to do formattingwise, and we'll see if we can do something a bit more concise.
Was This Post Helpful? 0
  • +
  • -

#13 HRT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-February 11

Re: How to trim a richtextbox

Posted 24 February 2011 - 11:39 PM

View PostBobRodes, on 24 February 2011 - 07:53 PM, said:

Scary! :) Ok, looks like this could be simpler than you're making it. If you could give a synopsis of what you're trying to do, we can probably considerably shorten your code. Suppose you take your code up to line 17 and tell me exactly what you are trying to do formattingwise, and we'll see if we can do something a bit more concise.

Yes it looks scary but each text box contains some information entered by users and the Text1(12) is the summary (report) of the all text boxes in one page which can be printed or saved as a word document. The titles are being formatted with Bold and Underline and the content of each text box comes under each title and the vbcrlf is to create paragraphs. If you run the code you will see that there are about 8 vbcrlf at the end of text1(12). I want to get rid of these unwanted vbcrlfs.
Was This Post Helpful? 0
  • +
  • -

#14 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,768
  • Joined: 26-March 09

Re: How to trim a richtextbox

Posted 25 February 2011 - 01:00 AM

I'm not quite sure why you end up with so many CRLF's at the end of the text...maybe it's something to do with all these type of statements

Text1(12).SelStart = Len(Text1(12).Text) - 15



Is it possible when you're doing the - X part, that you're placing the start position, before the CRLF on the line and therefore pushing the CRLF's to the end??

My understanding was that you have to add the text, then select it to apply formatting??

Forgive me if this is utter nonsense, as I'm not that familiar with the RTF textbox, just trying to offer some suggestions.
Was This Post Helpful? 1
  • +
  • -

#15 Guest_HRT*


Reputation:

Re: How to trim a richtextbox

Posted 25 February 2011 - 01:02 PM

View Postmaj3091, on 25 February 2011 - 01:00 AM, said:

I'm not quite sure why you end up with so many CRLF's at the end of the text...maybe it's something to do with all these type of statements

Text1(12).SelStart = Len(Text1(12).Text) - 15



Is it possible when you're doing the - X part, that you're placing the start position, before the CRLF on the line and therefore pushing the CRLF's to the end??

My understanding was that you have to add the text, then select it to apply formatting??

Forgive me if this is utter nonsense, as I'm not that familiar with the RTF textbox, just trying to offer some suggestions.

Thanks for your thoughts. You may be right about the start position and pushing the crlf to the end. But there must be a way to remove it afterwards. The texts are getting reformatted with the code I provided. I just want the final page (Text1(12) looks very consistent no matter who is entering the information and in what format. There must be a code to use to recognize these vbcrlfs and then remove them. The trim function works for regular text boxes but I don't know how to use it for this rtf box.
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2