locked
Text not showing up RRS feed

  • Question

  • I am working on a program that needs to display things like ñ, á, é, í, ó, ú, ¿, and ¡ (upside down exclamation mark) in a text box.  However, when the program reads characters like that from a file and tries to display them, where there should be a wierd character, I get a little empty box showing up.  (When I pasted it into this post for an example, it showed as this: �).  How do I get it to display as (in this case) an á?

    Oh, and when I open the file in a text editor (notepad) it shows the accented characters fine.
    Saturday, March 28, 2009 3:31 PM

Answers

  • You're probably not reading a file using the proper encoding.  Try reading the files using different encodings.  I had a similar issue earlier, and found that the ANSI encoding worked for my purposes. 

    The encoding classes are found at System.Text.Encoding.

    When creating the StreamReader to read the text, try this:

    using (StreamReader sr = new StreamReader(filename, Encoding.GetEncoding(1252)))
    {
        // read the file here.
    }
    David Morton - http://blog.davemorton.net/
    • Marked as answer by Bin-ze Zhao Wednesday, April 1, 2009 8:15 AM
    Saturday, March 28, 2009 3:44 PM
  • David, I suspect he may have some trouble with your code.

    I suggest isntead two variants.

                FileStream fs = new FileStream ( "filePath", FileMode.Open, FileAccess.Read  );
                System.Windows.Forms.Binding bnd = new System.Windows.Forms.Binding ( "Text", fs, "Text" );
                txt.DataBindings.BindableComponent.DataBindings.Add ( bnd );
    



    StreamReader sr = new StreamReader ( "fileName", Encoding.Unicode, true ); System.Windows.Forms.Binding bnd2 = new System.Windows.Forms.Binding ( "Text", sr, "Text" ); txt.DataBindings.Add ( bnd2 );

    Where txt:

    System.Windows.Forms.TextBox txt = new TextBox ( );

    AlexB
    • Marked as answer by Bin-ze Zhao Wednesday, April 1, 2009 8:15 AM
    Sunday, March 29, 2009 1:46 AM

All replies

  • You're probably not reading a file using the proper encoding.  Try reading the files using different encodings.  I had a similar issue earlier, and found that the ANSI encoding worked for my purposes. 

    The encoding classes are found at System.Text.Encoding.

    When creating the StreamReader to read the text, try this:

    using (StreamReader sr = new StreamReader(filename, Encoding.GetEncoding(1252)))
    {
        // read the file here.
    }
    David Morton - http://blog.davemorton.net/
    • Marked as answer by Bin-ze Zhao Wednesday, April 1, 2009 8:15 AM
    Saturday, March 28, 2009 3:44 PM
  • David, I suspect he may have some trouble with your code.

    I suggest isntead two variants.

                FileStream fs = new FileStream ( "filePath", FileMode.Open, FileAccess.Read  );
                System.Windows.Forms.Binding bnd = new System.Windows.Forms.Binding ( "Text", fs, "Text" );
                txt.DataBindings.BindableComponent.DataBindings.Add ( bnd );
    



    StreamReader sr = new StreamReader ( "fileName", Encoding.Unicode, true ); System.Windows.Forms.Binding bnd2 = new System.Windows.Forms.Binding ( "Text", sr, "Text" ); txt.DataBindings.Add ( bnd2 );

    Where txt:

    System.Windows.Forms.TextBox txt = new TextBox ( );

    AlexB
    • Marked as answer by Bin-ze Zhao Wednesday, April 1, 2009 8:15 AM
    Sunday, March 29, 2009 1:46 AM
  • Thanks a lot.  This worked fine.
    Friday, May 15, 2009 11:36 PM