Connecting Tech Pros Worldwide Forums | Help | Site Map

German Characters in textbox

Martin
Guest
 
Posts: n/a
#1: Jun 2 '07
Hi all,

When a user enters a german character in a textbox (such as ö ä ü ß) and I
try to save it, these characters get converted to a different character.
How can I prevent this?

Tia,
Martin


=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
 
Posts: n/a
#2: Jun 2 '07

re: German Characters in textbox


Martin wrote:
Quote:
Hi all,
>
When a user enters a german character in a textbox (such as ö ä ü ß) and
I try to save it, these characters get converted to a different
character. How can I prevent this?
>
Tia,
Martin
It sounds like it's a problem with encoding. You are probably either
using an encoding that doesn't support the characters, or using
different encodings for saving and retrieveing the text.

Where do you save it? Text file? Database? Left pocket?

Does the value change when you save it or when you retrieve it?

--
Göran Andersson
_____
http://www.guffa.com
Martin
Guest
 
Posts: n/a
#3: Jun 2 '07

re: German Characters in textbox


Hi Göran,

Thanks for your reply. I'm saving the texts to a text (ascii) file. I'm not
using any special encoding, in fact I don't have any experience using other
character sets, so I am not aware of how to choose a special kind of
encoding... Any help would be greatly appreciated.

Tia,
Martin


"Göran Andersson" <guffa@guffa.comwrote in message
news:evZFJIQpHHA.3952@TK2MSFTNGP03.phx.gbl...
Quote:
Martin wrote:
Quote:
>Hi all,
>>
>When a user enters a german character in a textbox (such as ö ä ü ß) and
>I try to save it, these characters get converted to a different
>character. How can I prevent this?
>>
>Tia,
>Martin
>
It sounds like it's a problem with encoding. You are probably either using
an encoding that doesn't support the characters, or using different
encodings for saving and retrieveing the text.
>
Where do you save it? Text file? Database? Left pocket?
>
Does the value change when you save it or when you retrieve it?
>
--
Göran Andersson
_____
http://www.guffa.com

=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
 
Posts: n/a
#4: Jun 2 '07

re: German Characters in textbox


Martin wrote:
Quote:
Hi Göran,
>
Thanks for your reply. I'm saving the texts to a text (ascii) file. I'm not
using any special encoding, in fact I don't have any experience using other
character sets, so I am not aware of how to choose a special kind of
encoding... Any help would be greatly appreciated.
>
A file doesn't contain characters, it contains bytes, so every text file
uses some encoding to represent the characters as bytes.

The ASCII encoding doesn't support any special characters. You should
use the UTF-8 encoding. This is however the default for the methods in
the framework when you don't specify any encoding, so you have to have
done something to use some other encoding.

What does your code look like?

--
Göran Andersson
_____
http://www.guffa.com
Martin
Guest
 
Posts: n/a
#5: Jun 2 '07

re: German Characters in textbox


Hi again,

The code Im using to read the file is I think pretty straightforward:

Private Sub DoOpenFile(ByVal ThisFile As String)
Dim Line As String

Try
Dim sr As System.IO.StreamReader = New
System.IO.StreamReader(ThisFile, True)
Line = sr.ReadLine()
sr.Close()
Catch Ex As Exception
MsgBox("Error reading file" & Chr(13) & Chr(13) & Ex.Message,
MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "Error")
Exit Sub
End Try

Line = DoFormatLine(Line)
FillEditor(Line)
FileName = ThisFile
Editor.ReadOnly = False
Me.Text = "Editing " & FileName
End Sub

The text is created in an editor (MultiEdit) and looks like this:

FTX+AAK+1++Es bestehen Vereinbarungen, aus denen sich nachträgliche
Minderungen :des Entgelts ergeben können.+DE

When I open this in my app, it suddenly looks like this:

FTX+AAK+1++Es bestehen Vereinbarungen, aus denen sich nachtr�gliche
Minderungen :des Entgelts ergeben k�nnen.+DE

After your message I added the TRUE parameter in the streamreader (detect
encoding) and set the form's language to German... To no effect.

Tia,
Martin




"Göran Andersson" <guffa@guffa.comwrote in message
news:%23p7b6eTpHHA.2156@TK2MSFTNGP03.phx.gbl...
Quote:
Martin wrote:
Quote:
>Hi Göran,
>>
>Thanks for your reply. I'm saving the texts to a text (ascii) file. I'm
>not using any special encoding, in fact I don't have any experience using
>other character sets, so I am not aware of how to choose a special kind
>of encoding... Any help would be greatly appreciated.
>>
>
A file doesn't contain characters, it contains bytes, so every text file
uses some encoding to represent the characters as bytes.
>
The ASCII encoding doesn't support any special characters. You should use
the UTF-8 encoding. This is however the default for the methods in the
framework when you don't specify any encoding, so you have to have done
something to use some other encoding.
>
What does your code look like?
>
--
Göran Andersson
_____
http://www.guffa.com
Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#6: Jun 2 '07

re: German Characters in textbox


"Martin" <x@y.comschrieb:
Quote:
Thanks for your reply. I'm saving the texts to a text (ascii) file.
ASCII is a 7-bit US encoding that doesn't support any umlauts.

Thus you have to use another encoding. 'StreamWriter' uses UTF-8 by default
which can contain umlauts. So, do not pass an ASCII encoding object to the
constructor.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#7: Jun 2 '07

re: German Characters in textbox


"Martin" <x@y.comschrieb:
Quote:
The code Im using to read the file is I think pretty straightforward:
>
Private Sub DoOpenFile(ByVal ThisFile As String)
Dim Line As String
>
Try
Dim sr As System.IO.StreamReader = New
System.IO.StreamReader(ThisFile, True)
Line = sr.ReadLine()
sr.Close()
Catch Ex As Exception
MsgBox("Error reading file" & Chr(13) & Chr(13) & Ex.Message,
MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "Error")
Exit Sub
End Try
>
Line = DoFormatLine(Line)
FillEditor(Line)
FileName = ThisFile
Editor.ReadOnly = False
Me.Text = "Editing " & FileName
End Sub
>
The text is created in an editor (MultiEdit) and looks like this:
>
FTX+AAK+1++Es bestehen Vereinbarungen, aus denen sich nachträgliche
Minderungen :des Entgelts ergeben können.+DE
>
When I open this in my app, it suddenly looks like this:
>
FTX+AAK+1++Es bestehen Vereinbarungen, aus denen sich nachtr�gliche
Minderungen :des Entgelts ergeben k�nnen.+DE
So, you are not writing the data using .NET's 'StreamWriter' at all. Most
likely the other application stores the file using the Windows ANSI encoding
with the system's default codepage. In order to read the file, pass
'System.Text.Encoding.Default' to the 'StreamReader''s constructor or
determine the encoding for a certain codepage using 'Encoding.GetEncoding'.
Quote:
After your message I added the TRUE parameter in the streamreader (detect
encoding) and set the form's language to German... To no effect.
Well, detecting the encoding isn't always possible because byte sequences
contained in the file may be valid for different encodings.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Martin
Guest
 
Posts: n/a
#8: Jun 2 '07

re: German Characters in textbox


Hi Herfried,

Thanks a lot. Adding the parameter System.Text.Encoding.Default did the
trick!
However, I also do a rewrite with the SreamWriter. I assume I need to add
the same parameter there?

Martin


"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrote in message
news:uY70IzTpHHA.1776@TK2MSFTNGP05.phx.gbl...
Quote:
"Martin" <x@y.comschrieb:
Quote:
>The code Im using to read the file is I think pretty straightforward:
>>
>Private Sub DoOpenFile(ByVal ThisFile As String)
> Dim Line As String
>>
> Try
> Dim sr As System.IO.StreamReader = New
>System.IO.StreamReader(ThisFile, True)
> Line = sr.ReadLine()
> sr.Close()
> Catch Ex As Exception
> MsgBox("Error reading file" & Chr(13) & Chr(13) & Ex.Message,
>MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "Error")
> Exit Sub
> End Try
>>
> Line = DoFormatLine(Line)
> FillEditor(Line)
> FileName = ThisFile
> Editor.ReadOnly = False
> Me.Text = "Editing " & FileName
>End Sub
>>
>The text is created in an editor (MultiEdit) and looks like this:
>>
>FTX+AAK+1++Es bestehen Vereinbarungen, aus denen sich nachträgliche
>Minderungen :des Entgelts ergeben können.+DE
>>
>When I open this in my app, it suddenly looks like this:
>>
>FTX+AAK+1++Es bestehen Vereinbarungen, aus denen sich nachtr�gliche
>Minderungen :des Entgelts ergeben k�nnen.+DE
>
So, you are not writing the data using .NET's 'StreamWriter' at all. Most
likely the other application stores the file using the Windows ANSI
encoding with the system's default codepage. In order to read the file,
pass 'System.Text.Encoding.Default' to the 'StreamReader''s constructor or
determine the encoding for a certain codepage using
'Encoding.GetEncoding'.
>
Quote:
>After your message I added the TRUE parameter in the streamreader (detect
>encoding) and set the form's language to German... To no effect.
>
Well, detecting the encoding isn't always possible because byte sequences
contained in the file may be valid for different encodings.
>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#9: Jun 2 '07

re: German Characters in textbox


"Martin" <x@y.comschrieb:
Quote:
However, I also do a rewrite with the SreamWriter. I assume I need to add
the same parameter there?
Yes.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

=?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=
Guest
 
Posts: n/a
#10: Jun 2 '07

re: German Characters in textbox


Martin wrote:
Quote:
Hi again,
>
The code Im using to read the file is I think pretty straightforward:
>
Private Sub DoOpenFile(ByVal ThisFile As String)
Dim Line As String
>
Try
Dim sr As System.IO.StreamReader = New
System.IO.StreamReader(ThisFile, True)
Line = sr.ReadLine()
sr.Close()
Catch Ex As Exception
MsgBox("Error reading file" & Chr(13) & Chr(13) & Ex.Message,
MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "Error")
Exit Sub
End Try
>
Line = DoFormatLine(Line)
FillEditor(Line)
FileName = ThisFile
Editor.ReadOnly = False
Me.Text = "Editing " & FileName
End Sub
>
The text is created in an editor (MultiEdit) and looks like this:
>
FTX+AAK+1++Es bestehen Vereinbarungen, aus denen sich nachträgliche
Minderungen :des Entgelts ergeben können.+DE
>
When I open this in my app, it suddenly looks like this:
>
FTX+AAK+1++Es bestehen Vereinbarungen, aus denen sich nachtr�gliche
Minderungen :des Entgelts ergeben k�nnen.+DE
>
After your message I added the TRUE parameter in the streamreader
(detect encoding) and set the form's language to German... To no effect.
>
Tia,
Martin
>
Do you specify any encoding when you save the file? In the Windows
Notepad you do, I doubt that a more advanced editor would completely
lack this feature.

Save the file as UTF-8, and you should have no problems reading it.

--
Göran Andersson
_____
http://www.guffa.com
Cor Ligthert [MVP]
Guest
 
Posts: n/a
#11: Jun 3 '07

re: German Characters in textbox


Martin,

I have made a simple program to test it. (Dutch settings)

Imports System.io
Public Class Form1
Private Sub Form1_Load(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "ö ä ü ß"
Using sw As New StreamWriter("C:\TestFile.txt")
sw.Write(TextBox1.Text)
sw.Close()
End Using
Try
Using sr As New StreamReader("C:\TestFile.txt")
TextBox2.Text = sr.ReadToEnd
sr.Close()
End Using
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
End Class

This gives in textbox2 the same characters as in textbox1

Probably have you played a little to much with the language settings of your
system.

Cor


"Martin" <x@y.comschreef in bericht
news:OwpQTAPpHHA.4496@TK2MSFTNGP06.phx.gbl...
Quote:
Hi all,
>
When a user enters a german character in a textbox (such as ö ä ü ß) and I
try to save it, these characters get converted to a different character.
How can I prevent this?
>
Tia,
Martin

Closed Thread