|
-
Apr 14th, 2011, 05:40 PM
#1
Thread Starter
Junior Member
Special characters in string text
I'm working on a form in which strings of text are being imported.
A lot of these strings contain special characters, like é, ö, ü, &, ï, etc...
When I convert these strings to text, so they can be shown as labels, the special characters either disappear (like the '&' sign) or they are shown as some sort of icons.
How can I let VB handle these strings, so the special characters are shown the way they should, without having to change the (many hundreds) original strings of text?
Thanks for your help!
-
Apr 14th, 2011, 05:45 PM
#2
Hyperactive Member
Re: Special characters in string text
 Originally Posted by Ivo77
I'm working on a form in which strings of text are being imported.
A lot of these strings contain special characters, like é, ö, ü, &, ï, etc...
When I convert these strings to text, so they can be shown as labels, the special characters either disappear (like the '&' sign) or they are shown as some sort of icons.
How can I let VB handle these strings, so the special characters are shown the way they should, without having to change the (many hundreds) original strings of text?
Thanks for your help!
i think you can shown a string in a label.. string and text is almost the same so you dont need to convert the string into text...
-
Apr 14th, 2011, 05:51 PM
#3
Thread Starter
Junior Member
Re: Special characters in string text
Well, what I mean is, for instance:
text file contains "This is an éxample text & this is anöther line."
I import this line of text as a string (String1)
Then I want to show the text in a label:
Label1.text = String1
When I run the form, Label1 shows: "This is an *xample text this is an*ther line", where the * are sort of icons in stead of é and ö, and the &-sign disappears.
-
Apr 14th, 2011, 06:04 PM
#4
Hyperactive Member
Re: Special characters in string text
lol i try also the text and it is working.. ?
screen shot..
-
Apr 15th, 2011, 02:18 AM
#5
Thread Starter
Junior Member
Re: Special characters in string text
 Originally Posted by marniel647
lol i try also the text and it is working.. ?
Thanks for the test,
however, this is not what I mean.
You typed the text directly into the textbox code. But in my case, I have to import the text from .TXT or .CSV files:
Dim Import As New System.IO.StreamReader("C:\file.txt")
Textstring = Import.ReadLine()
Label1.text = Textstring
This is where I'm unable to keep the special characters the way they are in the TXT file.
-
Apr 15th, 2011, 02:27 AM
#6
Re: Special characters in string text
You will presumably need to specify the encoding when creating the StreamReader. You could try Encoding.UTF7 and .Unicode first and then try others if they don't work.
-
Apr 15th, 2011, 02:32 AM
#7
Thread Starter
Junior Member
Re: Special characters in string text
 Originally Posted by jmcilhinney
You will presumably need to specify the encoding when creating the StreamReader. You could try Encoding.UTF7 and .Unicode first and then try others if they don't work.
Thanks for the suggestion!
Could you help me out a little? I've never specified encoding before. Can you give me an example on how to do this?
-
Apr 15th, 2011, 02:46 AM
#8
Re: Special characters in string text
Check out the documentation for the StreamReader class. Examples provided there.
-
Apr 15th, 2011, 05:24 AM
#9
Thread Starter
Junior Member
Re: Special characters in string text
 Originally Posted by jmcilhinney
Check out the documentation for the StreamReader class. Examples provided there.
Thanks,
I've managed to get the é, ö, ü, etc. characters imported correctly by using:
Dim Importstring As New System.IO.StreamReader("C:\File.txt", System.Text.Encoding.GetEncoding(1252))
However, I still haven't figured out how to prevent the ampersand (&) character from disappearing when I import a string of text and put it into a label.
With the code above I get the following result:
Text in file =
"Thïs is ä liné of têxt & this is anöther one."
Text after importing with streamreader, using system.text.encoding.getencoding(1252):
"Thïs is ä liné of têxt this is anöther one."
So, the '&' (ampersand sign) keeps disappearing.
-
Apr 15th, 2011, 08:52 AM
#10
Re: Special characters in string text
That "issue" with the ampersand is by design. That's how you create an accelerator key. For instance, let's say that you have a TextBox for the user's first name and you have a Label before it with the text "First Name:". If you actually set the Text of the Label to "&First Name:" then the ampersand is not displayed and the "F" character will be underlined. That indicates that the user can press Alt+F to set focus to that TextBox. If you want an ampersand to appear in such cases, you need to escape it with another ampersand, e.g. "This && That".
-
Apr 15th, 2011, 10:45 AM
#11
Re: Special characters in string text
 Originally Posted by jmcilhinney
If you want an ampersand to appear in such cases, you need to escape it with another ampersand, e.g. "This && That".
Or you could just set the controls UseMnemonic Property to False.
-
Apr 15th, 2011, 05:47 PM
#12
Thread Starter
Junior Member
Re: Special characters in string text
 Originally Posted by Edgemeal
Thanks so much, that's what I was looking for!
-
May 27th, 2011, 05:36 AM
#13
Thread Starter
Junior Member
Re: Special characters in string text
Now I'm facing a new, related problem.
This all works fine as long as I'm using streamreader to import text.
However, if I try to write this text to a CSV file by using streamwriter, the special characters like é, ü, ö, etc. get all scrambled again.
-
May 27th, 2011, 05:41 AM
#14
Thread Starter
Junior Member
Re: Special characters in string text
 Originally Posted by Ivo77
Now I'm facing a new, related problem.
This all works fine as long as I'm using streamreader to import text.
However, if I try to write this text to a CSV file by using streamwriter, the special characters like é, ü, ö, etc. get all scrambled again.
Never mind, I asked for help too quickly. I could use the System.Text.Encoding.GetEncoding(1252) code for streamwriter as well.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|