PDA

View Full Version : [Newbie alert] TextFile -> RichTextBox


Wedge
07-30-2007, 06:32 PM
From TextFile into a RichTextBox, how can you make it (or if it's the
streamreader thingy that is to blame?) recognize ANSI text? My
foreign characters å, ä, and ö are missing from the text.

Scenario:
I type some text in Notepad. Save the text to disk, using the ANSI
character encoding setting in Notepad's Save as dialog box. Presto,
å, ä, and ö are missing from the text when I open it in a simple
RichEditBox on a form.
If I on the other hand save the text using UTF-8 encoding in Notepad,
I can open the text in the RichTextBox and the characters I'm after
are intact.

Here's the code I use to read the text (found on-line):

[imports excluded!]

If File.Exists(sFileName) Then
Dim TextFileStream As TextReader
TextFileStream = File.OpenText(sFileName)
edtEditor.Text = TextFileStream.ReadToEnd
TextFileStream.Close()
End If

Here's the code I use to write the text (parts of it found on-line):

[imports excluded!]

Private Sub SaveIt(ByRef what As String, ByVal where As String)
Dim fileWriter As New StreamWriter(where)
fileWriter.WriteLine(what)
fileWriter.Close()
End Sub

TIA for any suggestions.

Oh, and I use this VB .NET 2005 Express Edition, if it matters.
--
Wedge

Wedge
08-01-2007, 08:23 AM
On 30 jul 2007, Wedge<wedge@nut.net>, had the following to say to
the folks in comp.lang.visual.basic:

> From TextFile into a RichTextBox, how can you make it (or if it's

Never mind. Figured it out.

Private Sub FileInOut(ByVal where As String, ByVal atype As
Integer, ByVal save As Boolean)
Dim aRichType As New RichTextBoxStreamType

Select Case atype
Case 1
aRichType = RichTextBoxStreamType.UnicodePlainText
Case 2
aRichType = RichTextBoxStreamType.PlainText
Case 3
aRichType = RichTextBoxStreamType.RichText
End Select
If save Then
edtEditor.SaveFile(where, aRichType)
Else
edtEditor.LoadFile(where, aRichType)
End If
End Sub

Seems to be working. If it breaks, I'll be back for more talking to
myself... maybe I'll figure it out the next time, too... ;-)
--
Wedge