Results 1 to 4 of 4

Thread: Hmmm...TextReader suddenly stopped working...

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Hmmm...TextReader suddenly stopped working...

    I had this piece of code working just fine:

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                
                StreamReader sr = new StreamReader("file.txt");
                while (sr.Peek() >=0)
    
                {
    
                    comboBox1.Items.Add(sr.ReadLine());
                    sr.Close();
                }
     
                
            }
    But then suddenly, it starts giving me this error:

    Cannot read from a closed TextReader.

    Why this all of a sudden???

    Complete code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    
    namespace TrialAndError
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                
                StreamReader sr = new StreamReader("file.txt");
                while (sr.Peek() >=0)
    
                {
    
                    comboBox1.Items.Add(sr.ReadLine());
                    sr.Close();
                }
     
                
            }
    
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                MessageBox.Show("Selected text: " + comboBox1.Text);
            }
    
           
            private void button2_Click(object sender, EventArgs e)
            {
                StreamWriter sw = File.AppendText("file.txt");
                sw.Write(textBox1.Text);
                sw.Close();
                
            }
        }
    }
    Last edited by carstenht; Feb 26th, 2007 at 03:54 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width