Link to home
Start Free TrialLog in
Avatar of Natavia Finnie
Natavia FinnieFlag for United States of America

asked on

vb.net RTF textbox to save formatting for word

I have a textbox that is RTF and I have some formatting that I would like to save.

example: H<subscript>2</subscript>0

Problem is is there maybe more then 1 sub or superscript in my rtf textbox

I want to be able to save the text from the rtf textbox to notepad as:
H{%format sub%}2{%endformat%}0

µmol × minute{%format super%}-1{%endformat%} × mg{%format super%}-5{%endformat%}
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Natavia Finnie

ASKER

I might not have been clear about my question. The H2O was another example....
the rtf = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}" & vbCrLf & "\viewkind4\uc1\pard\b\f0\fs20 U/mg \up7 2\up0\par" & vbCrLf & "}" & vbCrLf & ""

and

I want the actual text  or the position of where I can put: U/mg{%format sub%}7{%endformat%}

I want to strip the rtf stuff and still have a formatted string...
First of all sorry, I made a mistake in my initial post. You should read Rtf property in Text if you want to "play" with the codes.

Maybe it's me, but I not, I do not think you were clear. And this time either.

You say that you want "the actual text  or the position". Which one?

How can you "still have a formatted string" if you remove the tags? Wasn't the deal to send the text to Notepad? You can't have formatted text in Notepad.

What are these {%format sub%} and {%endformat%}, but placeholders of some kind. I thought that you wanted to replace the rtf tags by these.
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim myFont As String = activityUnit.Font.Name

        Dim thisString As String = rtftotext(activityUnit.Rtf)

        If activityUnit.Rtf.Contains(vbCrLf) Then
            Dim removeLineFeed = activityUnit.Rtf.Replace(vbCrLf, "")
            Dim rtfHeader As String = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 " & myFont & ";}}\viewkind4\uc1\pard\b\f0\fs20"
            Dim rtfHeader2 As String = "{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 " & myFont & ";}}\viewkind4\uc1\pard\lang1033\b\f0\fs20"

            If removeLineFeed.Contains(rtfHeader) Or removeLineFeed.Contains(rtfHeader2) Then
                Dim formattedText = removeLineFeed.Replace(rtfHeader, "")
                formattedText = formattedText.Substring(0, formattedText.Length - 1) 'removes last bracket
                Dim removeHangIndent = formattedText.Replace("\par", "")
            End If
        End If

    End Sub

Open in new window


My final string is = "U/mg\dn7 7\up0"
then I can save it as: U/mg {%format sub%} 7 {%endformat%}

Now... How can I read U/mg {%format sub%} 7 {%endformat%} and convert it back to RTF?!?!?!
I tore this string apart. Is there a more simple way to do this? My text file needs the format of this example U/mg {%format sub%} 7 {%endformat%} but I need the formatting of the RTB. However, I need to be able to read this U/mg {%format sub%} 7 {%endformat%} back but get the RTB text markup....
I still have problem understanding what you are trying to achieve, and for what purpose (which could explain a lot of things).

Why not keep a copy of the rtf in one file (RichTextBox.SaveFile) and the text in another?