Show RTF File content with formatting on Text Area

Hello Guys,

Im trying to put the text that i have saved in a .RTF document on a text area for license purposes and it seems that i cannot make it , i guess im doing something wrong.

On the text Area MultiLine is enabled, Styled text as well enabled.

Code is the following :

[code]dim ti As TextInputStream
dim RecordText As New StyledText

dim fi As FolderItem
dim fiPath As String

fi = app.ExecutableFile.Parent.Parent.Child(“Resources”).Child(“eula_fr.rtf”)

If fi.Exists AND fi <> Nil Then
fiPath = fi.NativePath

dim tString As String

ti = TextInputStream.Open(fi)

tString = ti.ReadAll

RecordText.RTFData = tString

ti.Close

me.StyledText.RTFData = RecordText

End If[/code]

So far for test purposes i get the data on the tString , with all the RTF Encodings but nothing on the Text Area Text , it comes empty .

Thanks in advance.

Why do you go through a convoluted system of separate styled Text ?

This should work just fine :

me.StyledText.RTFData = ti.ReadAll

It could also work like this :

[code]dim ti As TextInputStream

dim fi As FolderItem
dim fiPath As String

fi = app.ExecutableFile.Parent.Parent.Child(“Resources”).Child(“eula_fr.rtf”)

If fi.Exists AND fi <> Nil Then
fiPath = fi.NativePath

dim tString As String

ti = TextInputStream.Open(fi)

tString = ti.ReadAll

ti.Close

me.StyledText.RTFData = tString

End If[/code]

Thank you Michel but i dont get anything from there honestly . I looked on the forum for samples and ways to read .rtf files and to show them styled on the text area as i read that it is possible but no luck, i guess if i still dont manage to get it then i will do a .html document and an html viewer and that`s it .

Thanks again .

Look. I have been using that technique for years. Your error is to insist on a separate styledtext variable. Just shove the RTFData string from the disk into your TextArea.StyledText.RTFData. BTW are you sure of eula_fr.rtf ? Have you opened it with a word processor ? Does it actually contain what you think it does ?

Keep it simple.

You should really look at http://documentation.xojo.com/index.php/StyledText.RTFData

That said, an HTMLViewer is what I have been using for quite a while for my apps help.

Well i did and still the Text Area is empty

[code]dim ti As TextInputStream

dim fi As FolderItem
dim fiPath As String

fi = app.ExecutableFile.Parent.Parent.Child(“Resources”).Child(“eula_fr.rtf”)

If fi.Exists AND fi <> Nil Then

ti = TextInputStream.Open(fi)

me.StyledText.RTFData = ti.ReadAll

ti.Close

End If[/code]

So maybe i`m getting this wrong, if i throw the ti.readall data into a string i get the data, if i put it into the me.StyledText.RTFData it comes empty on the form.

me.StyledText.RTFData = ti.ReadAll

where is this code located?
are you sure the reference to “ME” is the correct textarea? (because that is the first place I’d look)

and otherwise, Michel is quite correct, there is no reason to jump through hoops…
but you need to insure the textarea you are “loading” is the same textarea you expect to see
AND there is nothing in between that UNDOES what you did (that has bitten me an unknown number of times)

[quote=329485:@Dave S] me.StyledText.RTFData = ti.ReadAll
where is this code located?
are you sure the reference to “ME” is the correct textarea? (because that is the first place I’d look)

and otherwise, Michel is quite correct, there is no reason to jump through hoops…
but you need to insure the textarea you are “loading” is the same textarea you expect to see
AND there is nothing in between that UNDOES what you did (that has bitten me an unknown number of times)[/quote]
Hi Dave, Into the Open Event of the TextArea

From where does the RTF comes ?

A file located on the disk,

If i do ti.ReadAll and put all that into a String i see all the RTF Data , so the data comes and formatted.

The issue is on Displaying that on the Text Area and correctly .

That is not what I mean. Which program generated that RTF ?

TextEdit, MAC

Here is what I did in a small test project, with my own User license file created in TextEDit. Works flawlessly.

[code]Sub Open() Handles Open
dim f as folderItem = SpecialFolder.Documents.child(“User_License.rtf”)

If f <> Nil Then
If f.Exists Then
system.DebugLog “loading”
// Be aware that TextInputStream.Open coud raise an exception
Dim t As TextInputStream
Try
t = TextInputStream.Open(f)
t.Encoding = Encodings.MacRoman
me.styledText.RTFData = t.ReadAll
Catch e As IOException
t.Close
MsgBox(“Error accessing file.”)
End Try
End If
End If
End Sub
[/code]

All I did was to modify the first example at http://documentation.xojo.com/index.php/Textinputstream

No complicated styledText value and intermediary variables. Plain, simple.

BTW, you should really do yourself a favor and get TPSF from Tim Parnell, that will let you access files in the bundle in a much easier way.

https://forum.xojo.com/15318-modules-for-you-updated-12-17/0

Regarding another issue in your code…

If fi.Exists AND fi <> Nil Then

…is incorrect. It should be

If fi <> Nil and fi.Exists Then

The reason is that, if fi is, in fact, nil your code will already have crashed with a NilObjectException on fi.Exists and never get to fi<> Nil.