Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reading a text file into a textBox

Reading a text file into a textBox

Scheduled Pinned Locked Moved C#
question
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simplicitylabs
    wrote on last edited by
    #1

    This doesn't throw any errors, but it also doesn't put the contents of my file into the textBox...

        private void ReadFromFile(string\[\] filename)
        {
            // create reader & open file
            TextReader tr = new StreamReader(openFileDialog1.FileName);
    
            // read a line of text
            this.textBox1.Text = tr.ReadLine();
    
            // close the stream
            tr.Close();
        }
    

    Where am I going wrong?

    X C 2 Replies Last reply
    0
    • S simplicitylabs

      This doesn't throw any errors, but it also doesn't put the contents of my file into the textBox...

          private void ReadFromFile(string\[\] filename)
          {
              // create reader & open file
              TextReader tr = new StreamReader(openFileDialog1.FileName);
      
              // read a line of text
              this.textBox1.Text = tr.ReadLine();
      
              // close the stream
              tr.Close();
          }
      

      Where am I going wrong?

      X Offline
      X Offline
      xibeifeijian
      wrote on last edited by
      #2

      try tr.ReadToEnd method please. I think the first line is a blank string in your file.

      :^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)

      S 1 Reply Last reply
      0
      • X xibeifeijian

        try tr.ReadToEnd method please. I think the first line is a blank string in your file.

        :^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)

        S Offline
        S Offline
        simplicitylabs
        wrote on last edited by
        #3

        When I saw your suggestion, I got excited. But, alas, there was no happy ending. To put it in context, I'm including the openfiledialog as well.

            private void openToolStripButton\_Click(object sender, EventArgs e)
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.openFileDialog1.InitialDirectory = @"c:\\";
                }
            }
        
            private void ReadFromFile(string filename)
            {
                // create reader & open file
                TextReader tr = new System.IO.StreamReader(this.openFileDialog1.FileName);
        
                // read a line of text
                this.textBox1.Text = tr.ReadToEnd();
        
                // close the stream
                tr.Close();
            }
        

        If you have any more suggestions, I'm all ears.

        D 1 Reply Last reply
        0
        • S simplicitylabs

          When I saw your suggestion, I got excited. But, alas, there was no happy ending. To put it in context, I'm including the openfiledialog as well.

              private void openToolStripButton\_Click(object sender, EventArgs e)
              {
                  if (openFileDialog1.ShowDialog() == DialogResult.OK)
                  {
                      this.openFileDialog1.InitialDirectory = @"c:\\";
                  }
              }
          
              private void ReadFromFile(string filename)
              {
                  // create reader & open file
                  TextReader tr = new System.IO.StreamReader(this.openFileDialog1.FileName);
          
                  // read a line of text
                  this.textBox1.Text = tr.ReadToEnd();
          
                  // close the stream
                  tr.Close();
              }
          

          If you have any more suggestions, I'm all ears.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Soooo....where do you call ReadFromFile???

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          S 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Soooo....where do you call ReadFromFile???

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            S Offline
            S Offline
            simplicitylabs
            wrote on last edited by
            #5

            Oops, missed that. Thanks for the catch. Added it...now it just locks up. By the way, did I say or do something wrong???

            X 1 Reply Last reply
            0
            • S simplicitylabs

              This doesn't throw any errors, but it also doesn't put the contents of my file into the textBox...

                  private void ReadFromFile(string\[\] filename)
                  {
                      // create reader & open file
                      TextReader tr = new StreamReader(openFileDialog1.FileName);
              
                      // read a line of text
                      this.textBox1.Text = tr.ReadLine();
              
                      // close the stream
                      tr.Close();
                  }
              

              Where am I going wrong?

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              If you're using .NET 2.0, System.IO.File.ReadAllText(openfileDialog1.FileName) should do it ( or something similar )

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              S 1 Reply Last reply
              0
              • C Christian Graus

                If you're using .NET 2.0, System.IO.File.ReadAllText(openfileDialog1.FileName) should do it ( or something similar )

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                S Offline
                S Offline
                simplicitylabs
                wrote on last edited by
                #7

                Thank you so much. I love you , man...

                1 Reply Last reply
                0
                • S simplicitylabs

                  Oops, missed that. Thanks for the catch. Added it...now it just locks up. By the way, did I say or do something wrong???

                  X Offline
                  X Offline
                  xibeifeijian
                  wrote on last edited by
                  #8

                  I sugguest you check the value of the openFileDialog1.FileName.Make sure that's a file's real path. If this way cannot help you try these codes: //First ,create a txt file at C:\ // create reader & open file TextReader tr = new StreamReader(@"c:\1.txt"); // read a line of text this.textBox1.Text = tr.ReadLine(); // close the stream tr.Close(); Good luck.

                  :^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups