How to display non-english chars in textbox?

K

kimiraikkonen

Hi,
I have a simple text viewer but the problem is:

When i load a text document using streamReader into my textbox with
non-english latin chars like Turkish chars (ð, Ý), the non-English
chars are not displayed. That part is seemed blank.
But Windows's default Notepad works fine and my textbox's font types
and properties are the same with Notepad's.

How can i overcome?

Thanks!
 
T

Teemu

Hi,
I have a simple text viewer but the problem is:

When i load a text document using streamReader into my textbox with
non-english latin chars like Turkish chars (g, I), the non-English
chars are not displayed. That part is seemed blank.
But Windows's default Notepad works fine and my textbox's font types
and properties are the same with Notepad's.

How can i overcome?

Check out this StreamReader's constructor:

Public Sub New(ByVal path As String, ByVal encoding As System.Text.Encoding)

Use correct encoding to load your file.

-Teemu
 
K

kimiraikkonen

Check out this StreamReader's constructor:

Public Sub New(ByVal path As String, ByVal encoding As System.Text.Encoding)

Use correct encoding to load your file.

-Teemu

Using myStreamReader As System.IO.StreamReader =
System.IO.File.OpenText(opendlg.FileName)
TextBox1.Text = myStreamReader.ReadToEnd()
myStreamReader.Dispose()
End Using

My code was this, could you help to to modify this code to display non-
english chars with no problem like Notepad?

Thanks!
 
T

Teemu

Using myStreamReader As System.IO.StreamReader =
System.IO.File.OpenText(opendlg.FileName)
TextBox1.Text = myStreamReader.ReadToEnd()
myStreamReader.Dispose()
End Using

My code was this, could you help to to modify this code to display non-
english chars with no problem like Notepad?

Using myStreamReader As System.IO.StreamReader = _
New System.IO.StreamReader(opendlg.FileName,
System.Text.Encoding.UTF8)
TextBox1.Text = myStreamReader.ReadToEnd()
myStreamReader.Dispose()
End Using

Check out with Object Browser that Encoding class to select the right
option.

-Teemu
 
T

Teemu

Using myStreamReader As System.IO.StreamReader = _
New System.IO.StreamReader(opendlg.FileName,
System.Text.Encoding.UTF8)
TextBox1.Text = myStreamReader.ReadToEnd()
myStreamReader.Dispose()
End Using

And that Dispose should be removed, because the StreamReader is initialized
inside Using structure.

-Teemu
 
K

kimiraikkonen

And that Dispose should be removed, because the StreamReader is initialized
inside Using structure.

-Teemu

Thanks! But what should i use instead of "dispose" in order to close
stream for the problem of being used before saving.
 
K

kimiraikkonen

That "End Using" will close and dispose the stream.

-Teemu

Teemu, thanks for your help so far, however i display Turkish chars
normally but after i re-save same file with Turkish chars, they
displayed strange (garbage) on next open.

I tried "default" and some other streamwriter associated
system.text.encoding parameters with no help.

What do you suggest to syncroize the display of all the chars 1:1 same
after re-saving and opening?
 
T

Teemu

Teemu, thanks for your help so far, however i display Turkish chars
normally but after i re-save same file with Turkish chars, they
displayed strange (garbage) on next open.

Use the same encoding when saving with StreamWriter.

Using myStreamWriter As IO.StreamWriter = _
New IO.StreamWriter(filename, System.Text.Encoding.UTF8)
myStreamWriter.Write(TextBox1.Text)
End Using

-Teemu
 
T

Teemu

Use the same encoding when saving with StreamWriter.

Using myStreamWriter As IO.StreamWriter = _
New IO.StreamWriter(filename, System.Text.Encoding.UTF8)
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Close()
End Using

I forgot to call Close()-function.

-Teemu
 
T

Teemu

Testing for somethime with "flush" instead of "close" as somebody
suggested with no being sure.

Documentation states:
Flushing the stream will not flush its underlying encoder unless you
explicitly call Close.

So Close will flush the contents.

-Teemu
 
K

kimiraikkonen

Testing for somethime with "flush" instead of "close" as somebody
suggested with no being sure.

There's another problem which occurs sometimes but not "always":

I'm using UTF8 encoding for reading and saving text files through my
application with no problem (Turkish Chars) but if i save file using
Windows Notepad with some Turkish chars, this time after i open if
using my application Turkish chars part is seemed blank(empty) in my
application.
 
T

Teemu

There's another problem which occurs sometimes but not "always":

I'm using UTF8 encoding for reading and saving text files through my
application with no problem (Turkish Chars) but if i save file using
Windows Notepad with some Turkish chars, this time after i open if
using my application Turkish chars part is seemed blank(empty) in my
application.

You could try this:

Using myStreamReader As System.IO.StreamReader = _
New System.IO.StreamReader(opendlg.FileName,
System.Text.Encoding.UTF8, True)
TextBox1.Text = myStreamReader.ReadToEnd()
End Using

Now StreamReader will try to find out the correct encoding. Maybe that could
help.

-Teemu
 
F

Family Tree Mike

There's another problem which occurs sometimes but not "always":

I'm using UTF8 encoding for reading and saving text files through my
application with no problem (Turkish Chars) but if i save file using
Windows Notepad with some Turkish chars, this time after i open if
using my application Turkish chars part is seemed blank(empty) in my
application.

I cant find the post now. Did you have a similar problem a couple of weeks
ago, where the file sometimes was not correctly saved?
 
K

kimiraikkonen

I cant find the post now. Did you have a similar problem a couple of weeks
ago, where the file sometimes was not correctly saved?

You remember right but this problem isn't same with that one. No need
to mention about it in that topic.
 
H

Herfried K. Wagner [MVP]

kimiraikkonen said:
I'm using UTF8 encoding for reading and saving text files through my
application with no problem (Turkish Chars) but if i save file using
Windows Notepad with some Turkish chars, this time after i open if
using my application Turkish chars part is seemed blank(empty) in my
application.

Notepad uses the system's default Windows ANSI code page by default. You
may want to use 'System.Text.Encoding.Default' instead of
'System.Text.Encoding.UTF8' to imitate the behavior of notepad.
 
K

kimiraikkonen

I cant find the post now. Did you have a similar problem a couple of weeks
ago, where the file sometimes was not correctly saved?

I think i have found the problem, if you save your file with Notepad
with pressing CTRL + S, file is saved in ANSI encoding which is
Notepad's default encoding mode, then after you open the text with my
app in UTF-8 encoding results with Turkish char loss.

To avoid the problem, i need to save text in UTF-8 in Notepad with
specifying "save as".Or how can i change Notepad's default saving text
encoding to UTF-8?
 
K

kimiraikkonen

Notepad uses the system's default Windows ANSI code page by default. You
may want to use 'System.Text.Encoding.Default' instead of
'System.Text.Encoding.UTF8' to imitate the behavior of notepad.

You posted earlier than i find out my solution :)

Yes, it's true however, i added "default" encoding parameter to my
app's streamWriter and StreamReader to have full compatibillity.
Thanks all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top