Page 1 of 1

How do I put escape codes in a text file read by wxtextfile?

Posted: Sun Jul 22, 2018 9:14 am
by davidlondonuk
Hi,

I'm writing a flash-card program using wxtextfile to read in questions and answers(each on a line) - this is output to a textctrl. My problem is I can't find a way to format the text, whatever I put in the text file is output, as is, to the textctrl e.g if I put an escape code like "\n" in the file to get a new line I get \n in the textctrl as text instead.

I want to be able to have new paragraphs and italics in the text that gets read from the file, I just can't see how to format the text that gets displayed in the textctrl.

Thanks for any help.

Re: How do I put escape codes in a text file read by wxtextfile?

Posted: Sun Jul 22, 2018 10:13 am
by PB
You seem to mix two different things, C++ escape codes (end of the lines) and formatting (bold, italics)?

If you write "\n" to a C++ string in code, it gets translated to a actual platform dependent end of the line characters. I.e, if you create a string like this

Code: Select all

wxString s = "Question\nAnswer";
and write it to a text file, it will contain two words separated by new a line.

If the file really contains literal "\n" text, then it gets read into a string as it is, no translation to C++ escape code is being done.

Text formatting in the wxTextCtrl is achieved by applying wxTextAtrr.

Perhaps you could look into wxRichTextCtrl, this control can save and load formatted text (in its own XML-based format).

Re: How do I put escape codes in a text file read by wxtextfile?

Posted: Mon Jul 23, 2018 10:58 am
by davidlondonuk
Thanks for that.
As wxtextfile reads in text as wxstrings, I assumed escape codes would work - which they do, but not from a file.

I have to code an edit dialog so rich text is the way to go. C++ escapes was the simplest solution, but ultimately too simple.