Replace styledtext in a TextArea

In a TextArea, you can call TextArea1.ReplaceAllStyledText(TextArea1,“old text”, "new text).

This function acts on the TextArea instantly.

When I try to restore the TextArea back to its original state, like this:

dim stText as StyledText
stText = TextArea1.StyledText
TextArea1.ReplaceAllStyledText(TextArea1,"old text", "new text)  //changes the text area instantly

TextArea1.StyledText = stText
TextArea1.Invalidate  //text area never changes back, even after the method has ended

the change never occurs.

Is there something obvious here that I am missing?

why not use ReplaceAllStyledText on the restore?

[quote=296744:@Dave S]why not use ReplaceAllStyledText on the restore?

[/quote]
This might work. I noticed that replacing across multiple style runs sets the format of the entire document to the first format style. Is there an example of how to try this piece by piece?

http://documentation.xojo.com/index.php/TextArea.StyledText

[quote] The object returned by StyledText is not a copy, so subsequent changes to it will affect the contents of the TextArea. See the example in the section “Using the StyledText Class” in the Notes for the TextArea class.
[/quote]

s is not a copy, it is a reference to TextArea.StyledText. You are not saving anything, just pointing to a unique StyledText.

Save StyledText.RTFData to a string, if you want to restore it later with StyledText.RTFData = s

[quote=296758:@Michel Bujardet]Save StyledText.RTFData to a string, if you want to restore it later with StyledText.RTFData = s

[/quote]
That was it! Thanks a bunch!