I had this piece of code working just fine:
But then suddenly, it starts giving me this error: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(); } }
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(); } } }




Reply With Quote